collapse main loops, making quit work better
This commit is contained in:
parent
ab7be24d0a
commit
84aa1f8cbe
|
@ -1,5 +1,7 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
func UpdateState(s State, sOld State) State {
|
func UpdateState(s State, sOld State) State {
|
||||||
for i := range s.Teams {
|
for i := range s.Teams {
|
||||||
s = doCommand(chooseCommand(s, i), s, i)
|
s = doCommand(chooseCommand(s, i), s, i)
|
||||||
|
@ -11,6 +13,7 @@ func UpdateState(s State, sOld State) State {
|
||||||
|
|
||||||
for _, t := range s.Teams {
|
for _, t := range s.Teams {
|
||||||
if b := activeBot(t); b != nil && won(*b, s) {
|
if b := activeBot(t); b != nil && won(*b, s) {
|
||||||
|
log.Printf("team %d won", t.id)
|
||||||
s.GameOver = true
|
s.GameOver = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,10 +185,10 @@ func NewState() State {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Steps = 50
|
Steps = 60
|
||||||
numBots = 5
|
numBots = 5
|
||||||
NumTeams = 4
|
NumTeams = 4
|
||||||
NumLanes = 6
|
NumLanes = 6
|
||||||
maxA = 3
|
maxA = 2
|
||||||
maxV = 10
|
maxV = 8
|
||||||
)
|
)
|
||||||
|
|
29
main.go
29
main.go
|
@ -30,29 +30,34 @@ func run() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
w.Clear(colornames.Peachpuff)
|
w.Clear(colornames.Peachpuff)
|
||||||
|
|
||||||
|
rs := gfx.RenderState{
|
||||||
|
Animating: false,
|
||||||
|
Frames: 20,
|
||||||
|
}
|
||||||
|
|
||||||
|
sOld := s
|
||||||
|
|
||||||
for !w.Closed() && !s.GameOver {
|
for !w.Closed() && !s.GameOver {
|
||||||
sOld := s
|
|
||||||
|
|
||||||
rs := gfx.RenderState{
|
|
||||||
Animating: false,
|
|
||||||
Frames: 20,
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case w.Pressed(pixelgl.KeyQ):
|
case w.Pressed(pixelgl.KeyQ):
|
||||||
return
|
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.Animating = true
|
||||||
|
rs.Frame = 0
|
||||||
s = game.UpdateState(s, sOld)
|
s = game.UpdateState(s, sOld)
|
||||||
if s.GameOver {
|
if s.GameOver {
|
||||||
s = game.NewState()
|
s = game.NewState()
|
||||||
sOld = s
|
sOld = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for rs.Animating {
|
|
||||||
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
|
w.Update()
|
||||||
w.Update()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue