log crashes

This commit is contained in:
Luke Meyers 2020-02-07 15:52:50 -08:00
parent ed6718bd0b
commit 10ffba1cdf
4 changed files with 14 additions and 13 deletions

View File

@ -3,7 +3,7 @@ package game
func chooseCommand(s State, teamID int) command {
t := s.Teams[teamID]
h := t.BatonHolder()
if collide(h.Pos+1, h.Lane, s) {
if collide(h.Pos+1, h.Lane, s) != nil {
if h.Lane <= t.Lane && h.Lane < NumLanes-1 {
return left
}

View File

@ -1,5 +1,7 @@
package game
import "log"
func accelerate(b Bot) Bot {
if b.a < -maxA {
b.a = -maxA
@ -21,7 +23,9 @@ func accelerate(b Bot) Bot {
func moveBot(s State, teamID int, b Bot) State {
for i := 0; i < b.v; i++ {
if !collide(b.Pos+1, b.Lane, s) {
if o := collide(b.Pos+1, b.Lane, s); o != nil {
log.Printf("bot %d crashed into %#v!", b.ID, o)
} else {
b.Pos++
}
}
@ -30,20 +34,20 @@ func moveBot(s State, teamID int, b Bot) State {
return s
}
func collide(pos, lane int, s State) bool {
func collide(pos, lane int, s State) interface{} {
for _, o := range s.Obstacles {
if o.Pos == pos && o.Lane == lane {
return true
return o
}
}
for _, t := range s.Teams {
for _, b := range t.Bots {
if b.Pos == pos && b.Lane == lane {
return true
return b
}
}
}
return false
return nil
}
const (

View File

@ -3,7 +3,6 @@ package gfx
import (
"image/color"
"relay/game"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
@ -17,13 +16,13 @@ type RenderState struct {
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) RenderState {
w.Clear(colornames.Peru)
tween := float64(rs.Frame) / float64(rs.Frames)
colors := teamColors(sNew.Teams)
renderBots(sOld, sNew, tween, w, d, colors)
renderBots(sOld, sNew, tween, w, colors)
renderObstacles(sNew, w)
rs.Frame++
@ -33,7 +32,7 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, d time.Dur
return rs
}
func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, _ time.Duration, colors map[*game.Team]pixel.RGBA) {
func renderBots(sOld, sNew game.State, tween float64, w *pixelgl.Window, colors map[*game.Team]pixel.RGBA) {
bounds := w.Bounds()
im := imdraw.New(nil)

View File

@ -27,8 +27,6 @@ func run() {
s := game.NewState()
start := time.Now()
w.Clear(colornames.Peachpuff)
rs := gfx.RenderState{
@ -42,7 +40,7 @@ func run() {
case w.Pressed(pixelgl.KeyQ):
return
case rs.Animating:
rs = gfx.Render(rs, sOld, s, w, time.Since(start))
rs = gfx.Render(rs, sOld, s, w)
if !rs.Animating {
sOld = s
}