working animation!
This commit is contained in:
parent
a915310274
commit
a632810d39
|
@ -35,5 +35,6 @@ func doCommand(cmd command, s State, sOld State, teamID int) State {
|
||||||
b.Lane--
|
b.Lane--
|
||||||
}
|
}
|
||||||
|
|
||||||
return updateBot(s, sOld, teamID, *b)
|
s = updateBot(s, sOld, teamID, *b)
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
18
game/game.go
18
game/game.go
|
@ -6,7 +6,7 @@ func UpdateState(s State, sOld State) State {
|
||||||
if b := activeBot(s.Teams[i]); b != nil {
|
if b := activeBot(s.Teams[i]); b != nil {
|
||||||
s = moveBot(s, i, *b)
|
s = moveBot(s, i, *b)
|
||||||
}
|
}
|
||||||
s = maybePassBaton(s, i)
|
s = maybePassBaton(s, sOld, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range s.Teams {
|
for _, t := range s.Teams {
|
||||||
|
@ -18,7 +18,7 @@ func UpdateState(s State, sOld State) State {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func maybePassBaton(s State, teamID int) State {
|
func maybePassBaton(s State, sOld State, teamID int) State {
|
||||||
t := s.Teams[teamID]
|
t := s.Teams[teamID]
|
||||||
h := activeBot(t)
|
h := activeBot(t)
|
||||||
if h == nil {
|
if h == nil {
|
||||||
|
@ -36,8 +36,8 @@ func maybePassBaton(s State, teamID int) State {
|
||||||
newH := t.Bots[i]
|
newH := t.Bots[i]
|
||||||
newH.a = baseAccel
|
newH.a = baseAccel
|
||||||
t.Baton.HolderID = newH.ID
|
t.Baton.HolderID = newH.ID
|
||||||
s = updateTeam(s, t)
|
s = updateTeam(s, sOld, t)
|
||||||
return updateBot(s, s, teamID, newH)
|
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
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateTeam(s State, t Team) State {
|
func updateTeam(s State, sOld State, t Team) State {
|
||||||
s.Teams = append(s.Teams[:t.id], append([]Team{t}, s.Teams[t.id+1:]...)...)
|
teams := append([]Team{}, s.Teams[:t.id]...)
|
||||||
|
teams = append(teams, t)
|
||||||
|
teams = append(teams, s.Teams[t.id+1:]...)
|
||||||
|
s.Teams = teams
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
gfx/gfx.go
21
gfx/gfx.go
|
@ -2,6 +2,7 @@ package gfx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"log"
|
||||||
"relay/game"
|
"relay/game"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -14,22 +15,22 @@ import (
|
||||||
type RenderState struct {
|
type RenderState struct {
|
||||||
Animating bool
|
Animating bool
|
||||||
Frames int
|
Frames int
|
||||||
frame int
|
Frame int
|
||||||
}
|
}
|
||||||
|
|
||||||
func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, d time.Duration) RenderState {
|
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)
|
w.Clear(colornames.Peru)
|
||||||
|
|
||||||
tween := float64(rs.frame) / float64(rs.Frames)
|
tween := float64(rs.Frame) / float64(rs.Frames)
|
||||||
|
|
||||||
colors := teamColors(sNew.Teams)
|
colors := teamColors(sNew.Teams)
|
||||||
renderBots(sOld, sNew, tween, w, d, colors)
|
renderBots(sOld, sNew, tween, w, d, colors)
|
||||||
renderObstacles(sNew, w)
|
renderObstacles(sNew, w)
|
||||||
|
|
||||||
rs.frame++
|
rs.Frame++
|
||||||
//log.Println("frame", rs.frame)
|
if rs.Frame >= rs.Frames {
|
||||||
if rs.frame >= rs.Frames {
|
|
||||||
rs.Animating = false
|
rs.Animating = false
|
||||||
}
|
}
|
||||||
return rs
|
return rs
|
||||||
|
@ -49,13 +50,13 @@ func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, d time.
|
||||||
im.Color = c
|
im.Color = c
|
||||||
|
|
||||||
oldBot := sOld.Teams[i].Bots[j]
|
oldBot := sOld.Teams[i].Bots[j]
|
||||||
// log.Println("oldBot:", oldBot)
|
//log.Printf("oldBot: %+v", oldBot)
|
||||||
// log.Println("bot:", bot)
|
//log.Printf("newBot: %+v", bot)
|
||||||
oldPos := lanePos(oldBot.Pos, oldBot.Lane, botWidth, bounds)
|
oldPos := lanePos(oldBot.Pos, oldBot.Lane, botWidth, bounds)
|
||||||
newPos := lanePos(bot.Pos, bot.Lane, botWidth, bounds)
|
newPos := lanePos(bot.Pos, bot.Lane, botWidth, bounds)
|
||||||
|
|
||||||
// log.Println("oldPos:", oldPos)
|
log.Println("oldPos:", oldPos)
|
||||||
// log.Println("newPos:", newPos)
|
log.Println("newPos:", newPos)
|
||||||
|
|
||||||
pos := pixel.Vec{
|
pos := pixel.Vec{
|
||||||
X: oldPos.X + tween*(newPos.X-oldPos.X),
|
X: oldPos.X + tween*(newPos.X-oldPos.X),
|
||||||
|
|
4
main.go
4
main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"relay/game"
|
"relay/game"
|
||||||
"relay/gfx"
|
"relay/gfx"
|
||||||
|
@ -36,7 +35,7 @@ func run() {
|
||||||
|
|
||||||
rs := gfx.RenderState{
|
rs := gfx.RenderState{
|
||||||
Animating: false,
|
Animating: false,
|
||||||
Frames: 3,
|
Frames: 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -47,7 +46,6 @@ func run() {
|
||||||
s = game.UpdateState(s, sOld)
|
s = game.UpdateState(s, sOld)
|
||||||
}
|
}
|
||||||
for rs.Animating {
|
for rs.Animating {
|
||||||
log.Println("anim loop")
|
|
||||||
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
|
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
|
||||||
w.Update()
|
w.Update()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue