From 33650b2e39a51cf3f1b130a0ef5ee28f71cd07b0 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Wed, 5 Feb 2020 19:26:53 -0800 Subject: [PATCH] Add phantom obstacle. --- game.go | 18 +++++++++++++++--- render.go | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/game.go b/game.go index 2de213e..6650a2b 100644 --- a/game.go +++ b/game.go @@ -6,8 +6,9 @@ import ( ) type state struct { - teams []team - gameOver bool + teams []team + obstacles []obstacle + gameOver bool } func newState() state { @@ -29,6 +30,12 @@ func newState() state { return state{ teams: teams, + obstacles: []obstacle{ + { + lane: 0, + pos: steps / 3, + }, + }, } } @@ -49,6 +56,11 @@ type baton struct { holder *bot } +type obstacle struct { + lane int + pos int +} + func updateState(sOld state) state { s := sOld @@ -120,7 +132,7 @@ func gameOver(s state) bool { const ( steps = 400 numBots = 10 - numTeams = 1 + numTeams = 2 maxA = 3 maxV = 20 ) diff --git a/render.go b/render.go index 4f39f54..88858e2 100644 --- a/render.go +++ b/render.go @@ -11,11 +11,13 @@ import ( ) func render(s state, w *pixelgl.Window, d time.Duration, colors map[*team]pixel.RGBA) { + renderBots(s, w, d, colors) + renderObstacles(s, w) +} + +func renderBots(s state, w *pixelgl.Window, d time.Duration, colors map[*team]pixel.RGBA) { b := w.Bounds() im := imdraw.New(nil) - hOffset := b.Size().X / steps - vOffset := b.Size().Y / (numTeams + 1) - width := 20 for i, t := range s.teams { for j, bot := range t.bots { @@ -24,19 +26,45 @@ func render(s state, w *pixelgl.Window, d time.Duration, colors map[*team]pixel. } else { im.Color = colors[&s.teams[i]] } - from := pixel.V(b.Min.X+float64(width)/2, b.Min.Y+float64(i+1)*vOffset) - pos := from.Add(pixel.V(float64(bot.pos)*hOffset, 0)) + + pos := lanePos(bot.pos, i, botWidth, b) im.Push(pos) im.Clear() - im.Circle(float64(width), 0) + im.Circle(float64(botWidth), 0) im.Draw(w) } } } +func lanePos(pos, lane int, width float64, bounds pixel.Rect) pixel.Vec { + hOffset := bounds.Size().X / steps + vOffset := bounds.Size().Y / (numTeams + 1) + + return pixel.V(bounds.Min.X+width/2+float64(pos)*hOffset, + bounds.Min.Y+float64(lane+1)*vOffset) +} + +func renderObstacles(s state, w *pixelgl.Window) { + b := w.Bounds() + im := imdraw.New(nil) + + for _, o := range s.obstacles { + im.Color = pixel.RGB(1, 0, 1) + + pos := lanePos(o.pos, o.lane, botWidth, b) + + im.Push(pos) + + im.Clear() + im.Circle(float64(botWidth), 0) + + im.Draw(w) + } +} + func teamColors(ts []team) map[*team]pixel.RGBA { m := make(map[*team]pixel.RGBA) for i := range ts { @@ -55,3 +83,7 @@ func teamColors(ts []team) map[*team]pixel.RGBA { } return m } + +const ( + botWidth float64 = 20 +)