Create a stripped down version of the robot struct to represent all robots and break up the update into my_robots and robots.

Has dependent change on client
This commit is contained in:
Fraser Graham 2013-10-24 20:13:21 -07:00
parent cc248dc2fe
commit de68ec2e7b
3 changed files with 36 additions and 10 deletions

25
game.go
View File

@ -18,7 +18,8 @@ type BotHealth struct {
}
type Boardstate struct {
Robots []Robot `json:"robots"`
MyRobots []Robot `json:"my_robots"`
OtherRobots []OtherRobot `json:"robots"`
Projectiles []Projectile `json:"projectiles"`
Splosions []Splosion `json:"splosions"`
Reset bool `json:"reset"`
@ -29,7 +30,8 @@ type Boardstate struct {
func NewBoardstate() *Boardstate {
return &Boardstate{
Robots: []Robot{},
MyRobots: []Robot{},
OtherRobots: []OtherRobot{},
Projectiles: []Projectile{},
Splosions: []Splosion{},
AllBots: []BotHealth{},
@ -40,7 +42,7 @@ func NewBoardstate() *Boardstate {
type Scanner struct {
Id string `json:"id"`
Position v.Point2d `json:"position"`
Stats Stats `json:"stats"`
Type string `json:"type"`
}
type MapLock struct {
@ -121,7 +123,9 @@ func (g *game) tick(payload *Boardstate) int {
robots_remaining++
p.Tick(g)
}
payload.Robots = append(payload.Robots, p.Robot)
payload.OtherRobots = append(
payload.OtherRobots,
p.Robot.GetTruncatedDetails())
payload.AllBots = append(
payload.AllBots,
@ -152,7 +156,7 @@ func (g *game) tick(payload *Boardstate) int {
func (g *game) send_update(payload *Boardstate) {
// Ensure that the robots are always sent in a consistent order
sort.Sort(RobotSorter{Robots: payload.Robots})
sort.Sort(RobotSorter{Robots: payload.OtherRobots})
for p := range g.players {
// Copy the payload but only add the robots in scanner range
@ -162,18 +166,23 @@ func (g *game) send_update(payload *Boardstate) {
player_payload.AllBots = payload.AllBots
player_payload.Turn = payload.Turn
player_payload.Reset = payload.Reset
player_payload.Robots = append(player_payload.Robots, p.Robot)
player_payload.MyRobots = append(player_payload.MyRobots, p.Robot)
player_payload.OtherRobots = append(
player_payload.OtherRobots,
p.Robot.GetTruncatedDetails())
if p.Robot.Health > 0 {
for player := range g.players {
for _, scan_entry := range p.Robot.Scanners {
if player.Robot.Id == scan_entry.Id {
player_payload.Robots = append(player_payload.Robots, player.Robot)
player_payload.OtherRobots = append(
player_payload.OtherRobots,
player.Robot.GetTruncatedDetails())
}
}
}
} else {
player_payload.Robots = payload.Robots
player_payload.OtherRobots = payload.OtherRobots
}
// x, _ := json.Marshal(player_payload)

View File

@ -71,7 +71,6 @@ func (p *player) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point
}
// Check Other Bots
// TODO - FIX THIS
for player := range g.players {
if player.Robot.Id == p.Robot.Id {
continue

View File

@ -19,8 +19,26 @@ type Robot struct {
LastFired int `json:"-"`
}
// This is the subset of data we send to players about robots
// that are not theirs.
type OtherRobot struct {
Id string `json:"id"`
Name string `json:"name"`
Position v.Point2d `json:"position"`
Health int `json:"health"`
}
func (r Robot) GetTruncatedDetails() OtherRobot {
return OtherRobot{
Id: r.Id,
Name: r.Name,
Position: r.Position,
Health: r.Health,
}
}
type RobotSorter struct {
Robots []Robot
Robots []OtherRobot
}
func (s RobotSorter) Len() int {