filter projectiles and objects by scanners
This commit is contained in:
parent
2707294bb6
commit
cfd3d91942
34
game.go
34
game.go
@ -165,7 +165,6 @@ func (g *game) send_update(payload *Boardstate) {
|
|||||||
for p := range g.players {
|
for p := range g.players {
|
||||||
// Copy the payload but only add the robots in scanner range
|
// Copy the payload but only add the robots in scanner range
|
||||||
player_payload := NewBoardstate()
|
player_payload := NewBoardstate()
|
||||||
player_payload.Projectiles = payload.Projectiles
|
|
||||||
player_payload.Splosions = payload.Splosions
|
player_payload.Splosions = payload.Splosions
|
||||||
player_payload.Obstacles = payload.Obstacles
|
player_payload.Obstacles = payload.Obstacles
|
||||||
player_payload.AllBots = payload.AllBots
|
player_payload.AllBots = payload.AllBots
|
||||||
@ -176,7 +175,11 @@ func (g *game) send_update(payload *Boardstate) {
|
|||||||
player_payload.OtherRobots,
|
player_payload.OtherRobots,
|
||||||
p.Robot.GetTruncatedDetails())
|
p.Robot.GetTruncatedDetails())
|
||||||
|
|
||||||
|
player_payload.Projectiles = []Projectile{}
|
||||||
|
player_payload.Obstacles = []Obstacle{}
|
||||||
|
|
||||||
if p.Robot.Health > 0 {
|
if p.Robot.Health > 0 {
|
||||||
|
// Filter robots by scanner
|
||||||
for player := range g.players {
|
for player := range g.players {
|
||||||
for _, scan_entry := range p.Robot.Scanners {
|
for _, scan_entry := range p.Robot.Scanners {
|
||||||
if player.Robot.Id == scan_entry.Id {
|
if player.Robot.Id == scan_entry.Id {
|
||||||
@ -186,8 +189,37 @@ func (g *game) send_update(payload *Boardstate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter projectiles
|
||||||
|
for proj := range g.projectiles {
|
||||||
|
|
||||||
|
if proj.Owner == p {
|
||||||
|
player_payload.Projectiles = append(
|
||||||
|
player_payload.Projectiles,
|
||||||
|
*proj)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, scan_entry := range p.Robot.Scanners {
|
||||||
|
if proj.Id == scan_entry.Id {
|
||||||
|
player_payload.Projectiles = append(
|
||||||
|
player_payload.Projectiles,
|
||||||
|
*proj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter objects
|
||||||
|
for _, ob := range g.obstacles {
|
||||||
|
if ob.distance_from_point(p.Robot.Position) < float32(p.Robot.Stats.ScannerRadius) {
|
||||||
|
player_payload.Obstacles = append(
|
||||||
|
player_payload.Obstacles,
|
||||||
|
ob)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
player_payload.OtherRobots = payload.OtherRobots
|
player_payload.OtherRobots = payload.OtherRobots
|
||||||
|
player_payload.Projectiles = payload.Projectiles
|
||||||
|
player_payload.Obstacles = payload.Obstacles
|
||||||
}
|
}
|
||||||
|
|
||||||
// x, _ := json.Marshal(player_payload)
|
// x, _ := json.Marshal(player_payload)
|
||||||
|
12
obstacle.go
12
obstacle.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
v "bitbucket.org/hackerbots/vector"
|
v "bitbucket.org/hackerbots/vector"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,6 +10,17 @@ type Obstacle struct {
|
|||||||
Bounds v.Rect2d `json:"bounds"`
|
Bounds v.Rect2d `json:"bounds"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o Obstacle) distance_from_point(p v.Point2d) float32 {
|
||||||
|
dist := math.MaxFloat32
|
||||||
|
|
||||||
|
dist = math.Min(dist, float64(p.Sub(o.Bounds.A).Mag()))
|
||||||
|
dist = math.Min(dist, float64(p.Sub(o.Bounds.B).Mag()))
|
||||||
|
dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.A.X, o.Bounds.B.Y}).Mag()))
|
||||||
|
dist = math.Min(dist, float64(p.Sub(v.Point2d{o.Bounds.B.X, o.Bounds.A.Y}).Mag()))
|
||||||
|
|
||||||
|
return float32(dist)
|
||||||
|
}
|
||||||
|
|
||||||
func GenerateObstacles(count int, width, height float32) []Obstacle {
|
func GenerateObstacles(count int, width, height float32) []Obstacle {
|
||||||
out := []Obstacle{}
|
out := []Obstacle{}
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user