Adding collisions and acceleration. Fixing splosions and projectiles.

This commit is contained in:
Fraser Graham 2013-09-27 01:30:07 -07:00
parent 472e2011b9
commit 92a2040d70
2 changed files with 80 additions and 7 deletions

10
game.go
View File

@ -92,7 +92,7 @@ func (g *game) run() {
robots_remaining++ robots_remaining++
// TODO: measure if this would be better to go and wait ... // TODO: measure if this would be better to go and wait ...
p.scan(g.players) p.scan(g.players)
p.nudge() p.nudge(g)
if p.Robot.FireAt != nil { if p.Robot.FireAt != nil {
proj := p.fire(g.projectiles) proj := p.fire(g.projectiles)
if proj != nil { if proj != nil {
@ -109,7 +109,10 @@ func (g *game) run() {
) )
for s := range g.splosions { for s := range g.splosions {
// XXX: s.tick() s.Tick()
if !s.Alive() {
delete(g.splosions, s)
}
payload.Splosions = append(payload.Splosions, *s) payload.Splosions = append(payload.Splosions, *s)
} }
@ -148,6 +151,7 @@ func (g *game) run() {
} }
func (g *game) nudgeProjectiles() (rprojectiles []bot.Projectile) { func (g *game) nudgeProjectiles() (rprojectiles []bot.Projectile) {
rprojectiles = make([]bot.Projectile, 0)
for p := range g.projectiles { for p := range g.projectiles {
newPos := v.Move(p.Position, p.MoveTo, float64(p.Speed), delta) newPos := v.Move(p.Position, p.MoveTo, float64(p.Speed), delta)
@ -191,6 +195,8 @@ func (g *game) nudgeProjectiles() (rprojectiles []bot.Projectile) {
} }
p.Position.X = newPos.X p.Position.X = newPos.X
p.Position.Y = newPos.Y p.Position.Y = newPos.Y
rprojectiles = append(rprojectiles, *p)
} }
return return
} }

View File

@ -5,6 +5,7 @@ import (
v "bitbucket.org/hackerbots/vector" v "bitbucket.org/hackerbots/vector"
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
"log" "log"
"math"
"math/rand" "math/rand"
) )
@ -45,20 +46,86 @@ func (p *player) recv() {
if msg.FireAt != nil { if msg.FireAt != nil {
p.Robot.FireAt = msg.FireAt p.Robot.FireAt = msg.FireAt
} }
p.Robot.TargetSpeed = p.Robot.Stats.Speed
if msg.Stats.Speed > 0 { if msg.Stats.Speed > 0 {
p.Robot.Stats = msg.Stats p.Robot.Stats = msg.Stats
log.Printf("%v\n", msg.Stats)
p.Robot.Health = p.Robot.Stats.Hp p.Robot.Health = p.Robot.Stats.Hp
p.Robot.Speed = 0
p.Robot.TargetSpeed = p.Robot.Stats.Speed
} }
} }
log.Printf("player %s: recv close", p.Robot.Id) log.Printf("player %s: recv close", p.Robot.Id)
p.ws.Close() p.ws.Close()
} }
func (p *player) nudge() { func (p *player) check_collisions(g *game, move_vector v.Vector2d) (bool, v.Point2d) {
// XXX: we must take into account going past our p.Robot.MoveTo ... collision := false
newPos := v.Move(p.Robot.Position, *p.Robot.MoveTo, p.Robot.Stats.Speed, delta) intersection_point := v.Point2d{X: 0, Y: 0}
p.Robot.Position.X = newPos.X
p.Robot.Position.Y = newPos.Y // Check Walls
r_walls := v.Rect2d{A: v.Point2d{X: 0, Y: 0}, B: v.Point2d{X: g.width, Y: g.height}}
collision, _, pos := v.RectIntersection(r_walls, p.Robot.Position, move_vector)
if collision {
return collision, pos
}
// Check Other Bots
// Check Terrain
// TBD
return collision, intersection_point
}
func (p *player) nudge(g *game) {
// Adjust Speed
if p.Robot.Speed < p.Robot.TargetSpeed {
p.Robot.Speed += (p.Robot.Stats.Acceleration * delta)
} else if (p.Robot.Speed - p.Robot.TargetSpeed) > v.Epsilon {
p.Robot.Speed -= (p.Robot.Stats.Acceleration * delta)
} else {
p.Robot.Speed = p.Robot.TargetSpeed
}
// Adjust Heading
current_heading := p.Robot.Heading
if current_heading.Mag() == 0 {
// We may have been stopped before this and had no heading
current_heading = p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
}
new_heading := p.Robot.MoveTo.Sub(p.Robot.Position).Normalize()
// Is our direction change too much? Hard coding to 5 degrees/s for now
angle := v.Angle(current_heading, new_heading) * v.Rad2deg
dir := 1.0
if angle < 0 {
dir = -1.0
}
if math.Abs(angle) > (100 * delta) {
// New heading should be a little less, take current heading and
// rotate by the max turn radius per frame.
rot := (100 * delta) * v.Deg2rad
new_heading = current_heading.Rotate(rot * dir)
}
move_vector := new_heading.Scale(p.Robot.Speed * delta)
collision, _ := p.check_collisions(g, move_vector)
if collision {
// p.Robot.Position = intersection_point
p.Robot.Speed = 0
p.Robot.MoveTo = &p.Robot.Position
p.Robot.Health -= 5
p.Robot.Heading = v.Vector2d{X: 0, Y: 0}
} else {
p.Robot.Position = p.Robot.Position.Add(move_vector)
p.Robot.Heading = new_heading
}
} }
func (p *player) scan(players map[*player]bool) { func (p *player) scan(players map[*player]bool) {