working animation!

This commit is contained in:
Luke Meyers 2020-02-07 14:55:25 -08:00
parent a915310274
commit a632810d39
4 changed files with 25 additions and 21 deletions

View File

@ -35,5 +35,6 @@ func doCommand(cmd command, s State, sOld State, teamID int) State {
b.Lane--
}
return updateBot(s, sOld, teamID, *b)
s = updateBot(s, sOld, teamID, *b)
return s
}

View File

@ -6,7 +6,7 @@ func UpdateState(s State, sOld State) State {
if b := activeBot(s.Teams[i]); b != nil {
s = moveBot(s, i, *b)
}
s = maybePassBaton(s, i)
s = maybePassBaton(s, sOld, i)
}
for _, t := range s.Teams {
@ -18,7 +18,7 @@ func UpdateState(s State, sOld State) State {
return s
}
func maybePassBaton(s State, teamID int) State {
func maybePassBaton(s State, sOld State, teamID int) State {
t := s.Teams[teamID]
h := activeBot(t)
if h == nil {
@ -36,8 +36,8 @@ func maybePassBaton(s State, teamID int) State {
newH := t.Bots[i]
newH.a = baseAccel
t.Baton.HolderID = newH.ID
s = updateTeam(s, t)
return updateBot(s, s, teamID, newH)
s = updateTeam(s, sOld, t)
return updateBot(s, sOld, teamID, newH)
}
}
@ -65,12 +65,16 @@ func updateBot(s State, sOld State, teamID int, b Bot) State {
}
}
s = updateTeam(s, t)
s = updateTeam(s, sOld, t)
return s
}
func updateTeam(s State, t Team) State {
s.Teams = append(s.Teams[:t.id], append([]Team{t}, s.Teams[t.id+1:]...)...)
func updateTeam(s State, sOld State, t Team) State {
teams := append([]Team{}, s.Teams[:t.id]...)
teams = append(teams, t)
teams = append(teams, s.Teams[t.id+1:]...)
s.Teams = teams
return s
}

View File

@ -2,6 +2,7 @@ package gfx
import (
"image/color"
"log"
"relay/game"
"time"
@ -14,22 +15,22 @@ import (
type RenderState struct {
Animating bool
Frames int
frame int
Frame int
}
func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, d time.Duration) RenderState {
//log.Println("render")
// log.Printf("ENTER render sOld: %+v", sOld)
// log.Printf("ENTER render sNew: %+v", sNew)
w.Clear(colornames.Peru)
tween := float64(rs.frame) / float64(rs.Frames)
tween := float64(rs.Frame) / float64(rs.Frames)
colors := teamColors(sNew.Teams)
renderBots(sOld, sNew, tween, w, d, colors)
renderObstacles(sNew, w)
rs.frame++
//log.Println("frame", rs.frame)
if rs.frame >= rs.Frames {
rs.Frame++
if rs.Frame >= rs.Frames {
rs.Animating = false
}
return rs
@ -49,13 +50,13 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, d time.
im.Color = c
oldBot := sOld.Teams[i].Bots[j]
// log.Println("oldBot:", oldBot)
// log.Println("bot:", bot)
//log.Printf("oldBot: %+v", oldBot)
//log.Printf("newBot: %+v", bot)
oldPos := lanePos(oldBot.Pos, oldBot.Lane, botWidth, bounds)
newPos := lanePos(bot.Pos, bot.Lane, botWidth, bounds)
// log.Println("oldPos:", oldPos)
// log.Println("newPos:", newPos)
log.Println("oldPos:", oldPos)
log.Println("newPos:", newPos)
pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X),

View File

@ -1,7 +1,6 @@
package main
import (
"log"
"math/rand"
"relay/game"
"relay/gfx"
@ -36,7 +35,7 @@ func run() {
rs := gfx.RenderState{
Animating: false,
Frames: 3,
Frames: 20,
}
switch {
@ -47,7 +46,6 @@ func run() {
s = game.UpdateState(s, sOld)
}
for rs.Animating {
log.Println("anim loop")
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
w.Update()
}