diff --git a/game/commands.go b/game/commands.go index 4e7c164..81eb390 100644 --- a/game/commands.go +++ b/game/commands.go @@ -1,5 +1,9 @@ package game +import ( + "github.com/faiface/pixel/pixelgl" +) + type Command int const ( @@ -13,12 +17,45 @@ const ( 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)) for i := range s.Teams { - cmd := chooseCommand(s, i) - //log.Printf("team %d chose to %v", i, cmd) - cmds[i] = cmd + cmds[i] = chooseCommand(s, i) } return cmds } diff --git a/game/env.go b/game/env.go index 291a714..6200a74 100644 --- a/game/env.go +++ b/game/env.go @@ -55,7 +55,7 @@ func randomOpenPosition(ts []Team, os []Obstacle) Position { func randomObstacles(teams []Team) []Obstacle { var os []Obstacle - const numObstacles = 2 * NumTeams + const numObstacles = 3 * NumTeams for i := 0; i < numObstacles; i++ { os = append(os, Obstacle{ Position: randomOpenPosition(teams, os), diff --git a/game/game.go b/game/game.go index 6b3304f..4a341b9 100644 --- a/game/game.go +++ b/game/game.go @@ -222,5 +222,5 @@ const ( numRacers = 3 NumTeams = 8 NumLanes = NumTeams - baseCharge = 16 + baseCharge = 14 ) diff --git a/gfx/gfx.go b/gfx/gfx.go index d9d8ef8..a6bad2a 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -16,12 +16,13 @@ import ( "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 ( frames = 0 second = time.Tick(time.Second) 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.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) { diff --git a/main.go b/main.go index a1772c5..2e9cf0f 100644 --- a/main.go +++ b/main.go @@ -31,39 +31,13 @@ func run() error { if err != nil { return err } - sOld := s - turn := 1 - cmdC := make(chan []game.Command) - go func() { cmdC <- game.PollCommands(s) }() + stateC := make(chan game.State) - stateCA := make(chan game.State) - stateCB := make(chan game.State) - - go gfx.RenderLoop(w, s, sOld, stateCA, sb) + go gfx.RenderLoop(w, s, stateC, sb) + go game.CommandLoop(w, s, stateC) 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 }