2013-10-20 20:54:19 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
v "bitbucket.org/hackerbots/vector"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Projectile struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Position v.Point2d `json:"position"`
|
|
|
|
MoveTo v.Point2d `json:"-"`
|
|
|
|
Radius int `json:"radius"`
|
|
|
|
Speed float32 `json:"-"`
|
|
|
|
Damage int `json:"-"`
|
2013-10-25 22:51:41 -07:00
|
|
|
Owner *player `json:"-"`
|
2013-10-20 20:54:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Projectile) Tick(g *game) {
|
|
|
|
vec := p.MoveTo.Sub(p.Position)
|
|
|
|
v_norm := vec.Normalize()
|
|
|
|
v_scaled := v_norm.Scale(p.Speed * delta)
|
|
|
|
newPos := p.Position.Add(v_scaled)
|
|
|
|
|
|
|
|
hit_player := false
|
|
|
|
for player := range g.players {
|
|
|
|
if player.Robot.Id == p.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dist := v.Distance(player.Robot.Position, p.Position)
|
|
|
|
if dist < 5.0 {
|
|
|
|
hit_player = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we going to intersect the destination zone in this tick?
|
2013-10-21 07:49:56 -07:00
|
|
|
r_dest := v.RectFromPoint(p.MoveTo, 3.0)
|
2013-10-20 20:54:19 -07:00
|
|
|
|
|
|
|
travel := newPos.Sub(p.Position)
|
2013-10-28 07:54:49 -07:00
|
|
|
arrived, _, pos := v.RectIntersection(r_dest, p.Position, travel)
|
2013-10-25 22:30:15 -07:00
|
|
|
|
|
|
|
if !arrived {
|
|
|
|
for _, obj := range g.obstacles {
|
2013-10-28 07:54:49 -07:00
|
|
|
collision, _, pos := v.RectIntersection(obj.Bounds, p.Position, travel)
|
2013-10-25 22:30:15 -07:00
|
|
|
if collision {
|
|
|
|
arrived = true
|
2013-10-28 07:54:49 -07:00
|
|
|
p.Position = pos
|
2013-10-25 22:30:15 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 07:54:49 -07:00
|
|
|
} else {
|
|
|
|
p.Position = pos
|
2013-10-25 22:30:15 -07:00
|
|
|
}
|
|
|
|
|
2013-10-20 20:54:19 -07:00
|
|
|
if arrived || hit_player {
|
|
|
|
delete(g.projectiles, p)
|
|
|
|
|
|
|
|
// Spawn a splosion
|
|
|
|
splo := &Splosion{
|
|
|
|
Id: p.Id,
|
|
|
|
Position: p.Position,
|
|
|
|
Radius: p.Radius,
|
|
|
|
MaxDamage: 10,
|
|
|
|
MinDamage: 5,
|
|
|
|
Lifespan: 8,
|
|
|
|
}
|
|
|
|
g.splosions[splo] = true
|
|
|
|
|
|
|
|
for player := range g.players {
|
|
|
|
dist := v.Distance(player.Robot.Position, p.Position)
|
|
|
|
if dist < float32(p.Radius) {
|
|
|
|
|
|
|
|
// TODO map damage Max to Min based on distance from explosion
|
|
|
|
if player.Robot.Health > 0 {
|
|
|
|
player.Robot.Health -= p.Damage
|
|
|
|
if player.Robot.Health <= 0 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.Position.X = newPos.X
|
|
|
|
p.Position.Y = newPos.Y
|
|
|
|
}
|
|
|
|
}
|