get multibots working
This commit is contained in:
parent
e39a97089e
commit
570d8eea8e
6
game.go
6
game.go
@ -117,7 +117,7 @@ func (g *game) tick(payload *Boardstate) int {
|
||||
for _, r := range p.Robots {
|
||||
if r.Health > 0 {
|
||||
living_robots++
|
||||
p.Tick(g)
|
||||
r.Tick(g)
|
||||
}
|
||||
|
||||
if len(r.Message) > 0 {
|
||||
@ -180,7 +180,7 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
player_payload.Turn = payload.Turn
|
||||
player_payload.Reset = payload.Reset
|
||||
for _, r := range p.Robots {
|
||||
player_payload.MyRobots = append(player_payload.MyRobots, r)
|
||||
player_payload.MyRobots = append(player_payload.MyRobots, *r)
|
||||
player_payload.OtherRobots = append(
|
||||
player_payload.OtherRobots,
|
||||
r.GetTruncatedDetails())
|
||||
@ -209,7 +209,7 @@ func (g *game) send_update(payload *Boardstate) {
|
||||
// Filter projectiles
|
||||
for proj := range g.projectiles {
|
||||
|
||||
if proj.Owner == &r {
|
||||
if proj.Owner == r {
|
||||
player_payload.Projectiles = append(
|
||||
player_payload.Projectiles,
|
||||
*proj)
|
||||
|
19
player.go
19
player.go
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type player struct {
|
||||
ws *websocket.Conn
|
||||
Robots []Robot
|
||||
Robots []*Robot
|
||||
send chan *Boardstate
|
||||
Instruction Instruction
|
||||
}
|
||||
@ -31,8 +31,9 @@ func (p *player) recv() {
|
||||
for {
|
||||
// XXX: need to mark myself as having received something, also binding
|
||||
// such action to a particular game turn ID
|
||||
var msgs PlayerInstructions
|
||||
var msgs map[string]Instruction
|
||||
err := websocket.JSON.Receive(p.ws, &msgs)
|
||||
|
||||
if err != nil {
|
||||
// TODO: perhaps we could be a bit more precise in the handling of
|
||||
// this 'error' by selecting on some kill signal channel and this
|
||||
@ -43,11 +44,14 @@ func (p *player) recv() {
|
||||
|
||||
for _, r := range p.Robots {
|
||||
|
||||
msg, ok := msgs.RobotInstructions[r.Id]
|
||||
msg, ok := msgs[r.Id]
|
||||
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// log.Printf("%v", msg.FireAt)
|
||||
|
||||
if msg.Repair != nil && *msg.Repair == true {
|
||||
r.TargetSpeed = 0
|
||||
r.FireAt = nil
|
||||
@ -116,7 +120,7 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
|
||||
player_rect := v.RectFromPoint(bot.Position, 3)
|
||||
collision, _, pos := v.RectIntersection(player_rect, r.Position, move_vector)
|
||||
if collision {
|
||||
return collision, pos, &bot
|
||||
return collision, pos, bot
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -132,9 +136,7 @@ func (r *Robot) checkCollisions(g *game, move_vector v.Vector2d) (bool, v.Point2
|
||||
return collision, intersection_point, nil
|
||||
}
|
||||
|
||||
func (p *player) Tick(g *game) {
|
||||
for _, r := range p.Robots {
|
||||
|
||||
func (r *Robot) Tick(g *game) {
|
||||
r.Collision = false
|
||||
r.Hit = false
|
||||
r.scan(g)
|
||||
@ -244,7 +246,6 @@ func (p *player) Tick(g *game) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Robot) scan(g *game) {
|
||||
r.Scanners = r.Scanners[:0]
|
||||
@ -311,6 +312,8 @@ func (r *Robot) reset(g *game) {
|
||||
r.Position = start_pos
|
||||
r.Health = r.Stats.Hp
|
||||
|
||||
log.Printf("Reset %v", r)
|
||||
|
||||
// Check Obstacles
|
||||
retry := false
|
||||
for _, obj := range g.obstacles {
|
||||
|
@ -24,7 +24,7 @@ func (p *Projectile) Tick(g *game) {
|
||||
hit_player := false
|
||||
for player := range g.players {
|
||||
for _, r := range player.Robots {
|
||||
if &r == p.Owner {
|
||||
if r == p.Owner {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -192,7 +192,7 @@ func addPlayer(ws *websocket.Conn) {
|
||||
}
|
||||
|
||||
p := &player{
|
||||
Robots: []Robot{},
|
||||
Robots: []*Robot{},
|
||||
send: make(chan *Boardstate),
|
||||
ws: ws,
|
||||
}
|
||||
@ -200,14 +200,14 @@ func addPlayer(ws *websocket.Conn) {
|
||||
for _, stats := range conf.Stats {
|
||||
r := Robot{
|
||||
Stats: DeriveStats(stats),
|
||||
Id: player_id,
|
||||
Id: idg.Hash(),
|
||||
Name: clientid.Name,
|
||||
Health: 10,
|
||||
Scanners: make([]Scanner, 0)}
|
||||
r.Health = r.Stats.Hp
|
||||
log.Printf("Adding Robot: %v", r)
|
||||
p.Robots = append(p.Robots, r)
|
||||
r.reset(game)
|
||||
p.Robots = append(p.Robots, &r)
|
||||
}
|
||||
|
||||
game.register <- p
|
||||
|
5
robot.go
5
robot.go
@ -157,9 +157,4 @@ type Instruction struct {
|
||||
TargetSpeed *float32 `json:"target_speed,omitempty"`
|
||||
Repair *bool `json:"repair,omitempty"`
|
||||
Scan *bool `json:"scan,omitempty"`
|
||||
Stats Stats `json:"stats"`
|
||||
}
|
||||
|
||||
type PlayerInstructions struct {
|
||||
RobotInstructions map[string]Instruction `json:"instructions"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user