collapse main loops, making quit work better

This commit is contained in:
Luke Meyers 2020-02-07 15:25:34 -08:00
parent ab7be24d0a
commit 84aa1f8cbe
2 changed files with 23 additions and 15 deletions

View File

@ -1,5 +1,7 @@
package game
import "log"
func UpdateState(s State, sOld State) State {
for i := range s.Teams {
s = doCommand(chooseCommand(s, i), s, i)
@ -11,6 +13,7 @@ func UpdateState(s State, sOld State) State {
for _, t := range s.Teams {
if b := activeBot(t); b != nil && won(*b, s) {
log.Printf("team %d won", t.id)
s.GameOver = true
}
}
@ -182,10 +185,10 @@ func NewState() State {
}
const (
Steps = 50
Steps = 60
numBots = 5
NumTeams = 4
NumLanes = 6
maxA = 3
maxV = 10
maxA = 2
maxV = 8
)

29
main.go
View File

@ -30,29 +30,34 @@ func run() {
start := time.Now()
w.Clear(colornames.Peachpuff)
rs := gfx.RenderState{
Animating: false,
Frames: 20,
}
sOld := s
for !w.Closed() && !s.GameOver {
sOld := s
rs := gfx.RenderState{
Animating: false,
Frames: 20,
}
switch {
case w.Pressed(pixelgl.KeyQ):
return
case w.JustPressed(pixelgl.KeySpace) || true:
case rs.Animating:
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
if !rs.Animating {
sOld = s
}
default:
rs.Animating = true
rs.Frame = 0
s = game.UpdateState(s, sOld)
if s.GameOver {
s = game.NewState()
sOld = s
}
}
for rs.Animating {
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
w.Update()
}
w.Update()
}
}