factor out gfx context struct

This commit is contained in:
Luke Meyers 2020-02-07 23:10:06 -08:00
parent 1f8607a902
commit b97d6c8d5d
1 changed files with 25 additions and 14 deletions

View File

@ -16,13 +16,24 @@ type RenderState struct {
Frame int Frame int
} }
type context struct {
sOld game.State
sNew game.State
tween float64
w *pixelgl.Window
}
func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderState { func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderState {
w.Clear(colornames.Olivedrab) w.Clear(colornames.Olivedrab)
tween := float64(rs.Frame) / float64(rs.Frames)
colors := teamColors(sNew.Teams) colors := teamColors(sNew.Teams)
renderBots(sOld, sNew, tween, w, colors) ctx := context{
sOld: sOld,
sNew: sNew,
tween: float64(rs.Frame) / float64(rs.Frames),
w: w,
}
renderBots(ctx, colors)
renderObstacles(sNew, w) renderObstacles(sNew, w)
rs.Frame++ rs.Frame++
@ -32,23 +43,23 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat
return rs return rs
} }
func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors map[*game.Team]pixel.RGBA) { func renderBots(ctx context, colors map[*game.Team]pixel.RGBA) {
for i, t := range sNew.Teams { for i, t := range ctx.sNew.Teams {
c := colors[&sNew.Teams[i]] c := colors[&ctx.sNew.Teams[i]]
for j, bot := range t.Bots { for j, bot := range t.Bots {
oldBot := sOld.Teams[i].Bots[j] oldBot := ctx.sOld.Teams[i].Bots[j]
renderBot(oldBot, bot, sOld, sNew, w, c, tween) renderBot(oldBot, bot, ctx.sOld, ctx.sNew, ctx.w, c, ctx.tween)
} }
oldHolder, newHolder := game.ActiveBot(sOld.Teams[i]), game.ActiveBot(sNew.Teams[i]) oldHolder, newHolder := game.ActiveBot(ctx.sOld.Teams[i]), game.ActiveBot(ctx.sNew.Teams[i])
oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, w.Bounds()) oldPos := lanePos(oldHolder.Position.Pos, oldHolder.Position.Lane, botWidth, ctx.w.Bounds())
newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, w.Bounds()) newPos := lanePos(newHolder.Position.Pos, newHolder.Position.Lane, botWidth, ctx.w.Bounds())
pos := pixel.Vec{ pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X), X: oldPos.X + ctx.tween*(newPos.X-oldPos.X),
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), Y: oldPos.Y + ctx.tween*(newPos.Y-oldPos.Y),
} }
renderBaton(pos, w) renderBaton(pos, ctx.w)
} }
} }