refactor: add Position struct
This commit is contained in:
parent
9c5fc0b08f
commit
464ffa1ed1
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
51
game/game.go
51
game/game.go
|
@ -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 {
|
||||||
|
@ -125,11 +125,15 @@ func (t Team) BatonHolder() *Bot {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
ID int
|
ID int
|
||||||
|
Position Position
|
||||||
|
v int
|
||||||
|
a int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Position struct {
|
||||||
Lane int
|
Lane int
|
||||||
Pos int
|
Pos int
|
||||||
v int
|
|
||||||
a int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Baton struct {
|
type Baton struct {
|
||||||
|
@ -137,8 +141,7 @@ type Baton struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Obstacle struct {
|
type Obstacle struct {
|
||||||
Lane int
|
Position Position
|
||||||
Pos int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState() State {
|
func NewState() State {
|
||||||
|
@ -147,9 +150,11 @@ func NewState() State {
|
||||||
var bots []Bot
|
var bots []Bot
|
||||||
for j := 0; j < numBots; j++ {
|
for j := 0; j < numBots; j++ {
|
||||||
b := Bot{
|
b := Bot{
|
||||||
ID: i*NumTeams + j,
|
ID: i*NumTeams + j,
|
||||||
Lane: i,
|
Position: Position{
|
||||||
Pos: j * (Steps / numBots),
|
Lane: i,
|
||||||
|
Pos: j * (Steps / numBots),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
bots = append(bots, b)
|
bots = append(bots, b)
|
||||||
}
|
}
|
||||||
|
@ -165,20 +170,28 @@ func NewState() State {
|
||||||
Teams: teams,
|
Teams: teams,
|
||||||
Obstacles: []Obstacle{
|
Obstacles: []Obstacle{
|
||||||
{
|
{
|
||||||
Lane: 0,
|
Position: Position{
|
||||||
Pos: Steps / 3,
|
Lane: 0,
|
||||||
|
Pos: Steps / 3,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Lane: 1,
|
Position: Position{
|
||||||
Pos: Steps * 2 / 3,
|
Lane: 1,
|
||||||
|
Pos: Steps * 2 / 3,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Lane: 2,
|
Position: Position{
|
||||||
Pos: Steps / 2,
|
Lane: 2,
|
||||||
|
Pos: Steps / 2,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Lane: 3,
|
Position: Position{
|
||||||
Pos: Steps * 3 / 4,
|
Lane: 3,
|
||||||
|
Pos: Steps * 3 / 4,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
gfx/gfx.go
10
gfx/gfx.go
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue