extract command loop

This commit is contained in:
Luke Meyers 2020-02-08 23:37:55 -08:00
parent 5bc4fa00bb
commit 688e922d29
5 changed files with 50 additions and 38 deletions

View File

@ -1,5 +1,9 @@
package game package game
import (
"github.com/faiface/pixel/pixelgl"
)
type Command int type Command int
const ( const (
@ -13,12 +17,45 @@ const (
var validCommands = []Command{coast, speedUp, slowDown, left, right, clearObstacle} var validCommands = []Command{coast, speedUp, slowDown, left, right, clearObstacle}
func PollCommands(s State) []Command { func CommandLoop(w *pixelgl.Window, s State, stateCA chan<- State) {
cmdC := make(chan []Command)
go func() { cmdC <- pollCommands(s) }()
stateCB := make(chan State)
turn := 1
sOld := s
for !w.Closed() {
switch {
case w.Pressed(pixelgl.KeyQ):
w.SetClosed(true)
return
case w.JustPressed(pixelgl.KeyEnter) || w.Pressed(pixelgl.KeySpace):
cmds := <-cmdC
s = UpdateState(s, sOld, cmds)
turn++
if s.GameOver {
s = NewState()
sOld = s
turn = 1
}
go func() {
s := <-stateCB
cmdC <- pollCommands(s)
}()
stateCA <- s
stateCB <- s
}
w.UpdateInput()
}
}
func pollCommands(s State) []Command {
cmds := make([]Command, len(s.Teams)) cmds := make([]Command, len(s.Teams))
for i := range s.Teams { for i := range s.Teams {
cmd := chooseCommand(s, i) cmds[i] = chooseCommand(s, i)
//log.Printf("team %d chose to %v", i, cmd)
cmds[i] = cmd
} }
return cmds return cmds
} }

View File

@ -55,7 +55,7 @@ func randomOpenPosition(ts []Team, os []Obstacle) Position {
func randomObstacles(teams []Team) []Obstacle { func randomObstacles(teams []Team) []Obstacle {
var os []Obstacle var os []Obstacle
const numObstacles = 2 * NumTeams const numObstacles = 3 * NumTeams
for i := 0; i < numObstacles; i++ { for i := 0; i < numObstacles; i++ {
os = append(os, Obstacle{ os = append(os, Obstacle{
Position: randomOpenPosition(teams, os), Position: randomOpenPosition(teams, os),

View File

@ -222,5 +222,5 @@ const (
numRacers = 3 numRacers = 3
NumTeams = 8 NumTeams = 8
NumLanes = NumTeams NumLanes = NumTeams
baseCharge = 16 baseCharge = 14
) )

View File

@ -16,12 +16,13 @@ import (
"golang.org/x/image/colornames" "golang.org/x/image/colornames"
) )
func RenderLoop(w *pixelgl.Window, s game.State, sOld game.State, stateC <-chan game.State, sb *SpriteBank) { func RenderLoop(w *pixelgl.Window, s game.State, stateC <-chan game.State, sb *SpriteBank) {
sOld := s
var ( var (
frames = 0 frames = 0
second = time.Tick(time.Second) second = time.Tick(time.Second)
rs = renderState{ rs = renderState{
Frames: 15, Frames: 30,
} }
) )
@ -194,7 +195,7 @@ func renderRacer(ctx context, batch *pixel.Batch, oldRacer, racer game.Racer, ac
sprite := pixel.NewSprite(pic, bounds) sprite := pixel.NewSprite(pic, bounds)
sprite.DrawColorMask(batch, pixel.IM.Moved(pos).ScaledXY(pos, pixel.Vec{1.7, 1.7}), c) sprite.DrawColorMask(batch, pixel.IM.Moved(pos).ScaledXY(pos, pixel.Vec{1.7, 1.7}), c)
renderFuelGuage(batch, pos, racer.Battery) //renderFuelGuage(batch, pos, racer.Battery)
} }
func renderFuelGuage(b *pixel.Batch, pos pixel.Vec, batt game.Battery) { func renderFuelGuage(b *pixel.Batch, pos pixel.Vec, batt game.Battery) {

32
main.go
View File

@ -31,39 +31,13 @@ func run() error {
if err != nil { if err != nil {
return err return err
} }
sOld := s
turn := 1
cmdC := make(chan []game.Command) stateC := make(chan game.State)
go func() { cmdC <- game.PollCommands(s) }()
stateCA := make(chan game.State) go gfx.RenderLoop(w, s, stateC, sb)
stateCB := make(chan game.State) go game.CommandLoop(w, s, stateC)
go gfx.RenderLoop(w, s, sOld, stateCA, sb)
for !w.Closed() { for !w.Closed() {
switch {
case w.Pressed(pixelgl.KeyQ):
return nil
case w.JustPressed(pixelgl.KeySpace) || true:
cmds := <-cmdC
s = game.UpdateState(s, sOld, cmds)
turn++
if s.GameOver {
s = game.NewState()
sOld = s
turn = 1
}
go func() {
s := <-stateCB
cmdC <- game.PollCommands(s)
}()
stateCA <- s
stateCB <- s
}
w.UpdateInput()
} }
return nil return nil
} }