split splosions and projectiles into separate files
This commit is contained in:
parent
541e213c66
commit
776a7eee98
59
game.go
59
game.go
@ -163,9 +163,10 @@ func (g *game) run() {
|
||||
}
|
||||
sort.Sort(RobotSorter{Robots: payload.Robots})
|
||||
|
||||
payload.Projectiles = append(payload.Projectiles,
|
||||
g.nudgeProjectiles()...,
|
||||
)
|
||||
g.nudgeProjectiles()
|
||||
for p := range g.projectiles {
|
||||
payload.Projectiles = append(payload.Projectiles, *p)
|
||||
}
|
||||
|
||||
for s := range g.splosions {
|
||||
s.Tick()
|
||||
@ -234,57 +235,7 @@ func (g *game) run() {
|
||||
func (g *game) nudgeProjectiles() (rprojectiles []Projectile) {
|
||||
rprojectiles = make([]Projectile, 0)
|
||||
for p := range g.projectiles {
|
||||
newPos := move(p.Position, p.MoveTo, float32(p.Speed), delta)
|
||||
|
||||
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?
|
||||
r_dest := v.Rect2d{
|
||||
A: v.Point2d{X: p.MoveTo.X - 3, Y: p.MoveTo.Y - 3},
|
||||
B: v.Point2d{X: p.MoveTo.X + 3, Y: p.MoveTo.Y + 3}}
|
||||
|
||||
travel := newPos.Sub(p.Position)
|
||||
arrived, _, _ := v.RectIntersection(r_dest, p.Position, travel)
|
||||
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
|
||||
rprojectiles = append(rprojectiles, *p)
|
||||
}
|
||||
p.Tick(g)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
70
projectile.go
Normal file
70
projectile.go
Normal file
@ -0,0 +1,70 @@
|
||||
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:"-"`
|
||||
}
|
||||
|
||||
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?
|
||||
r_dest := v.Rect2d{
|
||||
A: v.Point2d{X: p.MoveTo.X - 3, Y: p.MoveTo.Y - 3},
|
||||
B: v.Point2d{X: p.MoveTo.X + 3, Y: p.MoveTo.Y + 3}}
|
||||
|
||||
travel := newPos.Sub(p.Position)
|
||||
arrived, _, _ := v.RectIntersection(r_dest, p.Position, travel)
|
||||
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
|
||||
}
|
||||
}
|
25
robot.go
25
robot.go
@ -103,31 +103,6 @@ func (s StatsRequest) Valid() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
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:"-"`
|
||||
}
|
||||
|
||||
type Splosion struct {
|
||||
Id string `json:"id"`
|
||||
Position v.Point2d `json:"position"`
|
||||
Radius int `json:"radius"`
|
||||
MaxDamage int `json:"-"`
|
||||
MinDamage int `json:"-"`
|
||||
Lifespan int `json:"-"`
|
||||
}
|
||||
|
||||
func (s *Splosion) Tick() {
|
||||
s.Lifespan--
|
||||
}
|
||||
|
||||
func (s *Splosion) Alive() bool {
|
||||
return s.Lifespan > 0
|
||||
}
|
||||
|
||||
type Instruction struct {
|
||||
MoveTo *v.Point2d `json:"move_to,omitempty"`
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.net/websocket"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Spectator struct {
|
||||
@ -12,9 +13,11 @@ type Spectator struct {
|
||||
func (s *Spectator) sender() {
|
||||
for things := range s.send {
|
||||
err := websocket.JSON.Send(s.ws, *things)
|
||||
log.Printf("%v", things)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
s.ws.Close()
|
||||
log.Printf("Spectator sender close")
|
||||
}
|
||||
|
22
splosion.go
Normal file
22
splosion.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
v "bitbucket.org/hackerbots/vector"
|
||||
)
|
||||
|
||||
type Splosion struct {
|
||||
Id string `json:"id"`
|
||||
Position v.Point2d `json:"position"`
|
||||
Radius int `json:"radius"`
|
||||
MaxDamage int `json:"-"`
|
||||
MinDamage int `json:"-"`
|
||||
Lifespan int `json:"-"`
|
||||
}
|
||||
|
||||
func (s *Splosion) Tick() {
|
||||
s.Lifespan--
|
||||
}
|
||||
|
||||
func (s *Splosion) Alive() bool {
|
||||
return s.Lifespan > 0
|
||||
}
|
Loading…
Reference in New Issue
Block a user