refactor: add Position struct

This commit is contained in:
Luke Meyers 2020-02-07 19:31:58 -08:00
parent 9c5fc0b08f
commit 464ffa1ed1
5 changed files with 47 additions and 34 deletions

View File

@ -3,8 +3,8 @@ package game
func chooseCommand(s State, teamID int) command { func chooseCommand(s State, teamID int) command {
t := s.Teams[teamID] t := s.Teams[teamID]
h := t.BatonHolder() h := t.BatonHolder()
if collide(h.Pos+1, h.Lane, s) != nil { if collide(h.Position.Pos+1, h.Position.Lane, s) != nil {
if h.Lane <= t.Lane && h.Lane < NumLanes-1 { if h.Position.Lane <= t.Lane && h.Position.Lane < NumLanes-1 {
return left return left
} }
return right return right
@ -19,8 +19,8 @@ func chooseCommand(s State, teamID int) command {
} }
if nextBot != nil { if nextBot != nil {
if h.Lane != nextBot.Lane { if h.Position.Lane != nextBot.Position.Lane {
if abs(nextBot.Pos-h.Pos) < h.v { if abs(nextBot.Position.Pos-h.Position.Pos) < h.v {
return slowDown return slowDown
} }
} }

View File

@ -26,9 +26,9 @@ func doCommand(cmd command, s State, teamID int) State {
b.a -= da b.a -= da
*b = accelerate(*b) *b = accelerate(*b)
case left: case left:
b.Lane++ b.Position.Lane++
case right: case right:
b.Lane-- b.Position.Lane--
} }
s = updateBot(s, teamID, *b) s = updateBot(s, teamID, *b)

View File

@ -29,10 +29,10 @@ func maybePassBaton(s State, teamID int) State {
} }
for i, b := range t.Bots { 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 continue
} }
if abs(b.Pos-h.Pos) <= passDistance { if abs(b.Position.Pos-h.Position.Pos) <= passDistance {
h.v = 0 h.v = 0
h.a = 0 h.a = 0
s = updateBot(s, teamID, *h) s = updateBot(s, teamID, *h)
@ -82,7 +82,7 @@ func updateTeam(s State, t Team) State {
} }
func won(b Bot, s State) bool { func won(b Bot, s State) bool {
return b.Pos >= Steps return b.Position.Pos >= Steps
} }
func gameOver(s State) bool { func gameOver(s State) bool {
@ -126,19 +126,22 @@ func (t Team) BatonHolder() *Bot {
type Bot struct { type Bot struct {
ID int ID int
Lane int Position Position
Pos int
v int v int
a int a int
} }
type Position struct {
Lane int
Pos int
}
type Baton struct { type Baton struct {
HolderID int HolderID int
} }
type Obstacle struct { type Obstacle struct {
Lane int Position Position
Pos int
} }
func NewState() State { func NewState() State {
@ -148,8 +151,10 @@ func NewState() State {
for j := 0; j < numBots; j++ { for j := 0; j < numBots; j++ {
b := Bot{ b := Bot{
ID: i*NumTeams + j, ID: i*NumTeams + j,
Position: Position{
Lane: i, Lane: i,
Pos: j * (Steps / numBots), Pos: j * (Steps / numBots),
},
} }
bots = append(bots, b) bots = append(bots, b)
} }
@ -165,22 +170,30 @@ func NewState() State {
Teams: teams, Teams: teams,
Obstacles: []Obstacle{ Obstacles: []Obstacle{
{ {
Position: Position{
Lane: 0, Lane: 0,
Pos: Steps / 3, Pos: Steps / 3,
}, },
},
{ {
Position: Position{
Lane: 1, Lane: 1,
Pos: Steps * 2 / 3, Pos: Steps * 2 / 3,
}, },
},
{ {
Position: Position{
Lane: 2, Lane: 2,
Pos: Steps / 2, Pos: Steps / 2,
}, },
},
{ {
Position: Position{
Lane: 3, Lane: 3,
Pos: Steps * 3 / 4, Pos: Steps * 3 / 4,
}, },
}, },
},
} }
} }

View File

@ -23,11 +23,11 @@ func accelerate(b Bot) Bot {
func moveBot(s State, teamID int, b Bot) State { func moveBot(s State, teamID int, b Bot) State {
for i := 0; i < b.v; i++ { 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) log.Printf("bot %d crashed into %#v!", b.ID, o)
break break
} else { } 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{} { func collide(pos, lane int, s State) interface{} {
for _, o := range s.Obstacles { for _, o := range s.Obstacles {
if o.Pos == pos && o.Lane == lane { if o.Position.Pos == pos && o.Position.Lane == lane {
return o return o
} }
} }
for _, t := range s.Teams { for _, t := range s.Teams {
for _, b := range t.Bots { for _, b := range t.Bots {
if b.Pos == pos && b.Lane == lane { if b.Position.Pos == pos && b.Position.Lane == lane {
return b return b
} }
} }

View File

@ -42,8 +42,8 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors
im.Color = c im.Color = c
oldBot := sOld.Teams[i].Bots[j] oldBot := sOld.Teams[i].Bots[j]
oldPos := lanePos(oldBot.Pos, oldBot.Lane, botWidth, bounds) oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, bounds)
newPos := lanePos(bot.Pos, bot.Lane, botWidth, bounds) newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, bounds)
pos := pixel.Vec{ pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X), 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]) oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i])
oldPos := lanePos(oldHolder.Pos, oldHolder.Lane, botWidth, bounds) oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, bounds)
newPos := lanePos(newHolder.Pos, newHolder.Lane, botWidth, bounds) newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, bounds)
pos := pixel.Vec{ pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X), 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 { for _, o := range s.Obstacles {
im.Color = pixel.RGB(0.1, 0.1, 0.2) 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) im.Push(pos)