From 9c5fc0b08f61463edf0f823589dc98f1bfd3e805 Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Fri, 7 Feb 2020 16:24:23 -0800 Subject: [PATCH] throw batons with animation --- game/commands.go | 2 +- game/game.go | 8 ++++---- game/physics.go | 2 +- gfx/gfx.go | 19 ++++++++++++------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/game/commands.go b/game/commands.go index 55f0d68..e8f30c6 100644 --- a/game/commands.go +++ b/game/commands.go @@ -13,7 +13,7 @@ func doCommand(cmd command, s State, teamID int) State { da := 1 //da += rand.Intn(3) - 1 - b := activeBot(s.Teams[teamID]) + b := ActiveBot(s.Teams[teamID]) if b == nil { return s } diff --git a/game/game.go b/game/game.go index 6d9ce54..53749dd 100644 --- a/game/game.go +++ b/game/game.go @@ -5,14 +5,14 @@ import "log" func UpdateState(s State, sOld State) State { for i := range s.Teams { s = doCommand(chooseCommand(s, i), s, i) - if b := activeBot(s.Teams[i]); b != nil { + if b := ActiveBot(s.Teams[i]); b != nil { s = moveBot(s, i, *b) } s = maybePassBaton(s, i) } for _, t := range s.Teams { - if b := activeBot(t); b != nil && won(*b, s) { + if b := ActiveBot(t); b != nil && won(*b, s) { log.Printf("team %d won", t.id) s.GameOver = true } @@ -23,7 +23,7 @@ func UpdateState(s State, sOld State) State { func maybePassBaton(s State, teamID int) State { t := s.Teams[teamID] - h := activeBot(t) + h := ActiveBot(t) if h == nil { return s } @@ -47,7 +47,7 @@ func maybePassBaton(s State, teamID int) State { return s } -func activeBot(t Team) *Bot { +func ActiveBot(t Team) *Bot { for _, b := range t.Bots { if b.ID == t.Baton.HolderID { return &b diff --git a/game/physics.go b/game/physics.go index ade2d89..907c066 100644 --- a/game/physics.go +++ b/game/physics.go @@ -52,6 +52,6 @@ func collide(pos, lane int, s State) interface{} { } const ( - passDistance = 1 + passDistance = 3 baseAccel = 1 ) diff --git a/gfx/gfx.go b/gfx/gfx.go index 38a90ee..3f9311d 100644 --- a/gfx/gfx.go +++ b/gfx/gfx.go @@ -17,7 +17,7 @@ type RenderState struct { } func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderState { - w.Clear(colornames.Peru) + w.Clear(colornames.Olivedrab) tween := float64(rs.Frame) / float64(rs.Frames) @@ -51,15 +51,20 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors } im.Push(pos) - im.Clear() im.Circle(botWidth, 0) - im.Draw(w) - if t.Bots[j].ID == t.Baton.HolderID { - renderBaton(pos, w) - } } + + 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) + + pos := pixel.Vec{ + X: oldPos.X + tween*(newPos.X-oldPos.X), + Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), + } + renderBaton(pos, w) } } @@ -110,7 +115,7 @@ func teamColors(ts []game.Team) map[*game.Team]pixel.RGBA { case 2: c = colornames.Lavender case 3: - c = colornames.Maroon + c = colornames.Rosybrown } m[&ts[i]] = pixel.ToRGBA(c) }