Add phantom obstacle.
This commit is contained in:
parent
f1a8c48b13
commit
33650b2e39
18
game.go
18
game.go
|
@ -6,8 +6,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
teams []team
|
teams []team
|
||||||
gameOver bool
|
obstacles []obstacle
|
||||||
|
gameOver bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newState() state {
|
func newState() state {
|
||||||
|
@ -29,6 +30,12 @@ func newState() state {
|
||||||
|
|
||||||
return state{
|
return state{
|
||||||
teams: teams,
|
teams: teams,
|
||||||
|
obstacles: []obstacle{
|
||||||
|
{
|
||||||
|
lane: 0,
|
||||||
|
pos: steps / 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +56,11 @@ type baton struct {
|
||||||
holder *bot
|
holder *bot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type obstacle struct {
|
||||||
|
lane int
|
||||||
|
pos int
|
||||||
|
}
|
||||||
|
|
||||||
func updateState(sOld state) state {
|
func updateState(sOld state) state {
|
||||||
s := sOld
|
s := sOld
|
||||||
|
|
||||||
|
@ -120,7 +132,7 @@ func gameOver(s state) bool {
|
||||||
const (
|
const (
|
||||||
steps = 400
|
steps = 400
|
||||||
numBots = 10
|
numBots = 10
|
||||||
numTeams = 1
|
numTeams = 2
|
||||||
maxA = 3
|
maxA = 3
|
||||||
maxV = 20
|
maxV = 20
|
||||||
)
|
)
|
||||||
|
|
44
render.go
44
render.go
|
@ -11,11 +11,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func render(s state, w *pixelgl.Window, d time.Duration, colors map[*team]pixel.RGBA) {
|
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()
|
b := w.Bounds()
|
||||||
im := imdraw.New(nil)
|
im := imdraw.New(nil)
|
||||||
hOffset := b.Size().X / steps
|
|
||||||
vOffset := b.Size().Y / (numTeams + 1)
|
|
||||||
width := 20
|
|
||||||
|
|
||||||
for i, t := range s.teams {
|
for i, t := range s.teams {
|
||||||
for j, bot := range t.bots {
|
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 {
|
} else {
|
||||||
im.Color = colors[&s.teams[i]]
|
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.Push(pos)
|
||||||
|
|
||||||
im.Clear()
|
im.Clear()
|
||||||
im.Circle(float64(width), 0)
|
im.Circle(float64(botWidth), 0)
|
||||||
|
|
||||||
im.Draw(w)
|
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 {
|
func teamColors(ts []team) map[*team]pixel.RGBA {
|
||||||
m := make(map[*team]pixel.RGBA)
|
m := make(map[*team]pixel.RGBA)
|
||||||
for i := range ts {
|
for i := range ts {
|
||||||
|
@ -55,3 +83,7 @@ func teamColors(ts []team) map[*team]pixel.RGBA {
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
botWidth float64 = 20
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue