diff --git a/game/ai.go b/game/ai.go index 77070f6..60018a0 100644 --- a/game/ai.go +++ b/game/ai.go @@ -3,8 +3,8 @@ package game func chooseCommand(s State, teamID int) command { t := s.Teams[teamID] h := t.BatonHolder() - if collide(h.Pos+1, h.Lane, s) != nil { - if h.Lane <= t.Lane && h.Lane < NumLanes-1 { + if collide(h.Position.Pos+1, h.Position.Lane, s) != nil { + if h.Position.Lane <= t.Lane && h.Position.Lane < NumLanes-1 { return left } return right @@ -19,8 +19,8 @@ func chooseCommand(s State, teamID int) command { } if nextBot != nil { - if h.Lane != nextBot.Lane { - if abs(nextBot.Pos-h.Pos) < h.v { + if h.Position.Lane != nextBot.Position.Lane { + if abs(nextBot.Position.Pos-h.Position.Pos) < h.v { return slowDown } } diff --git a/game/commands.go b/game/commands.go index e8f30c6..19b0ca5 100644 --- a/game/commands.go +++ b/game/commands.go @@ -26,9 +26,9 @@ func doCommand(cmd command, s State, teamID int) State { b.a -= da *b = accelerate(*b) case left: - b.Lane++ + b.Position.Lane++ case right: - b.Lane-- + b.Position.Lane-- } s = updateBot(s, teamID, *b) diff --git a/game/game.go b/game/game.go index 53749dd..5d6f14d 100644 --- a/game/game.go +++ b/game/game.go @@ -29,10 +29,10 @@ func maybePassBaton(s State, teamID int) State { } for i, b := range t.Bots { - if h.ID >= b.ID || h.Lane != b.Lane { + if h.ID >= b.ID || h.Position.Lane != b.Position.Lane { continue } - if abs(b.Pos-h.Pos) <= passDistance { + if abs(b.Position.Pos-h.Position.Pos) <= passDistance { h.v = 0 h.a = 0 s = updateBot(s, teamID, *h) @@ -82,7 +82,7 @@ func updateTeam(s State, t Team) State { } func won(b Bot, s State) bool { - return b.Pos >= Steps + return b.Position.Pos >= Steps } func gameOver(s State) bool { @@ -125,11 +125,15 @@ func (t Team) BatonHolder() *Bot { } type Bot struct { - ID int + ID int + Position Position + v int + a int +} + +type Position struct { Lane int Pos int - v int - a int } type Baton struct { @@ -137,8 +141,7 @@ type Baton struct { } type Obstacle struct { - Lane int - Pos int + Position Position } func NewState() State { @@ -147,9 +150,11 @@ func NewState() State { var bots []Bot for j := 0; j < numBots; j++ { b := Bot{ - ID: i*NumTeams + j, - Lane: i, - Pos: j * (Steps / numBots), + ID: i*NumTeams + j, + Position: Position{ + Lane: i, + Pos: j * (Steps / numBots), + }, } bots = append(bots, b) } @@ -165,20 +170,28 @@ func NewState() State { Teams: teams, Obstacles: []Obstacle{ { - Lane: 0, - Pos: Steps / 3, + Position: Position{ + Lane: 0, + Pos: Steps / 3, + }, }, { - Lane: 1, - Pos: Steps * 2 / 3, + Position: Position{ + Lane: 1, + Pos: Steps * 2 / 3, + }, }, { - Lane: 2, - Pos: Steps / 2, + Position: Position{ + Lane: 2, + Pos: Steps / 2, + }, }, { - Lane: 3, - Pos: Steps * 3 / 4, + Position: Position{ + Lane: 3, + Pos: Steps * 3 / 4, + }, }, }, } diff --git a/game/physics.go b/game/physics.go index 907c066..994517f 100644 --- a/game/physics.go +++ b/game/physics.go @@ -23,11 +23,11 @@ func accelerate(b Bot) Bot { func moveBot(s State, teamID int, b Bot) State { for i := 0; i < b.v; i++ { - if o := collide(b.Pos+1, b.Lane, s); o != nil { + if o := collide(b.Position.Pos+1, b.Position.Lane, s); o != nil { log.Printf("bot %d crashed into %#v!", b.ID, o) break } else { - b.Pos++ + b.Position.Pos++ } } @@ -37,13 +37,13 @@ func moveBot(s State, teamID int, b Bot) State { func collide(pos, lane int, s State) interface{} { for _, o := range s.Obstacles { - if o.Pos == pos && o.Lane == lane { + if o.Position.Pos == pos && o.Position.Lane == lane { return o } } for _, t := range s.Teams { for _, b := range t.Bots { - if b.Pos == pos && b.Lane == lane { + if b.Position.Pos == pos && b.Position.Lane == lane { return b } } diff --git a/gfx/gfx.go b/gfx/gfx.go index 3f9311d..1625966 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -42,8 +42,8 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors im.Color = c oldBot := sOld.Teams[i].Bots[j] - oldPos := lanePos(oldBot.Pos, oldBot.Lane, botWidth, bounds) - newPos := lanePos(bot.Pos, bot.Lane, botWidth, bounds) + oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, bounds) + newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, bounds) pos := pixel.Vec{ X: oldPos.X + tween*(newPos.X-oldPos.X), @@ -57,8 +57,8 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors } oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i]) - oldPos := lanePos(oldHolder.Pos, oldHolder.Lane, botWidth, bounds) - newPos := lanePos(newHolder.Pos, newHolder.Lane, botWidth, bounds) + oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, bounds) + newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, bounds) pos := pixel.Vec{ X: oldPos.X + tween*(newPos.X-oldPos.X), @@ -92,7 +92,7 @@ func renderObstacles(s game.State, w *pixelgl.Window) { for _, o := range s.Obstacles { im.Color = pixel.RGB(0.1, 0.1, 0.2) - pos := lanePos(o.Pos, o.Lane, botWidth, b) + pos := lanePos(o.Position.Pos, o.Position.Lane, botWidth, b) im.Push(pos)