chaneg the player interface to be more consistent with the JS version

This commit is contained in:
Fraser Graham 2014-04-26 13:59:32 -06:00
parent 2d8b878f50
commit a2436fe8d8
2 changed files with 22 additions and 19 deletions

View File

@ -163,10 +163,14 @@ func (c *Client) Play() error {
if err != nil { if err != nil {
return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err)) return errors.New(fmt.Sprintf("%s: Connection likely lost: %s", c.Name, err))
} }
c.Player.Recv(&c.boardstate)
instruction := c.Player.Instruction() instructions := make(map[string]server.Instruction)
err = c.enc.Encode(instruction)
for _,bot := range(c.boardstate.MyRobots){
instructions[bot.Id] = c.Player.Update(&bot, &c.boardstate)
}
err = c.enc.Encode(instructions)
if err != nil { if err != nil {
return err return err
} }

View File

@ -15,8 +15,7 @@ import (
// The general case will be to implement a Player type that contains the magic // The general case will be to implement a Player type that contains the magic
// required to slay other robots quickly while staying alive for a long time. // required to slay other robots quickly while staying alive for a long time.
type Player interface { type Player interface {
Recv(bs *server.Boardstate) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction
Instruction() map[string]server.Instruction
} }
// SimplePlayer is our default player and stands as a starting point for your // SimplePlayer is our default player and stands as a starting point for your
@ -45,16 +44,28 @@ func NewSimplePlayer(width, height float32) *SimplePlayer {
} }
// Recv is our implementation of receiving a server.Boardstate from the server // Recv is our implementation of receiving a server.Boardstate from the server
func (p *SimplePlayer) Recv(bs *server.Boardstate) { func (p *SimplePlayer) Update(bot *server.Robot, bs *server.Boardstate) server.Instruction{
instruction := server.Instruction{
MoveTo: nil,
TargetSpeed: nil,
FireAt: nil,
}
p.speed = p.maxSpeed p.speed = p.maxSpeed
if len(bs.MyRobots) > 0 { if len(bs.MyRobots) > 0 {
p.me = bs.MyRobots[0] p.me = bs.MyRobots[0]
} else { } else {
return return instruction
} }
p.recon(bs) p.recon(bs)
p.navigate() p.navigate()
return server.Instruction{
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
}
} }
func (p *SimplePlayer) navigate() { func (p *SimplePlayer) navigate() {
@ -107,18 +118,6 @@ func (p *SimplePlayer) recon(bs *server.Boardstate) {
} }
} }
// Instruction is our default implementation of preparing a map of information
// to be sent to server.
func (p *SimplePlayer) Instruction() map[string]server.Instruction {
return map[string]server.Instruction{
p.me.Id: {
MoveTo: p.moveto,
TargetSpeed: &p.speed,
FireAt: p.fireat,
},
}
}
func (p *SimplePlayer) randomDirection() *vector.Point2d { func (p *SimplePlayer) randomDirection() *vector.Point2d {
pt := vector.Vector2d{ pt := vector.Vector2d{
X: rand.Float32() * p.width, X: rand.Float32() * p.width,