-
Notifications
You must be signed in to change notification settings - Fork 2
/
tick.go
49 lines (42 loc) · 1.1 KB
/
tick.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
// Update the start menu state every tick
func (g *Game) PerformStartMenuTick() {
defer g.drawStartGameMenu()
g.ui.ball.Move()
g.checkCollision(true)
}
// Update the game state every tick.
func (g *Game) PerformGameTick() {
// update the screen.
// this isn't expensive since it just checks for changes on the canvas
// and if there aren't any, nothing will be updated therefore no bloat.
// the terminal is updated after everything has been drawn.
// this should be called after every tick, thats why it's defered
defer g.drawGameTick()
if g.state == gameStatePaused || g.state == gameStateHardPaused {
return
}
// keys that don't persist when game is paused
for _, event := range *g.activeEvents {
switch event {
case eventP1Up:
g.movePlayerUp(g.players.P1)
case eventP1Down:
g.movePlayerDown(g.players.P1)
case eventP2Up:
if !g.aiActive {
g.movePlayerUp(g.players.P2)
}
case eventP2Down:
if !g.aiActive {
g.movePlayerDown(g.players.P2)
}
}
}
if g.aiActive {
g.aiMove()
}
// move the ball for 1 tick
g.ui.ball.Move()
g.checkCollision(false)
}