2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-03 23:26:40 -07:00
|
|
|
"bitbucket.org/hackerbots/bot"
|
2013-08-19 20:43:26 -07:00
|
|
|
v "bitbucket.org/hackerbots/vector"
|
2013-08-19 21:42:43 -07:00
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-08-19 20:43:26 -07:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
)
|
|
|
|
|
|
|
|
type player struct {
|
|
|
|
ws *websocket.Conn
|
2013-09-03 23:26:40 -07:00
|
|
|
game *game
|
|
|
|
Robot bot.Robot
|
|
|
|
send chan *bot.Boardstate
|
2013-08-19 20:43:26 -07:00
|
|
|
Instruction instruction
|
|
|
|
}
|
|
|
|
|
|
|
|
type instruction struct {
|
|
|
|
MoveTo *v.Point2d `json:"move_to,omitempty"`
|
|
|
|
FireAt *v.Point2d `json:"fire_at,omitempty"`
|
2013-09-03 23:26:40 -07:00
|
|
|
Stats bot.Stats `json:"stats"`
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) sender() {
|
|
|
|
for things := range p.send {
|
|
|
|
err := websocket.JSON.Send(p.ws, *things)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) recv() {
|
|
|
|
for {
|
|
|
|
var msg instruction
|
|
|
|
err := websocket.JSON.Receive(p.ws, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if msg.MoveTo != nil {
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.MoveTo = msg.MoveTo
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
if msg.FireAt != nil {
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.FireAt = msg.FireAt
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
if msg.Stats.Speed > 0 {
|
|
|
|
p.Robot.Stats = msg.Stats
|
|
|
|
p.Robot.Health = p.Robot.Stats.Hp
|
|
|
|
log.Printf("%+v", p.Robot.Stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) nudge() {
|
2013-09-03 23:35:42 -07:00
|
|
|
newPos := v.Move(p.Robot.Position, p.Robot.MoveTo, p.Robot.Stats.Speed, delta)
|
2013-08-19 20:43:26 -07:00
|
|
|
p.Robot.Position.X = newPos.X
|
|
|
|
p.Robot.Position.Y = newPos.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) scan() {
|
2013-09-03 23:26:40 -07:00
|
|
|
p.Robot.Scanners = make([]bot.Scanner, 0)
|
|
|
|
for player := range p.game.players {
|
2013-08-19 20:43:26 -07:00
|
|
|
if player.Robot.Id == p.Robot.Id || player.Robot.Health <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
2013-09-03 23:35:42 -07:00
|
|
|
dist := v.Distance(player.Robot.Position, p.Robot.Position)
|
2013-08-19 20:43:26 -07:00
|
|
|
if dist < float64(p.Robot.Stats.ScannerRadius) {
|
2013-09-03 23:26:40 -07:00
|
|
|
s := bot.Scanner{
|
2013-08-19 20:43:26 -07:00
|
|
|
Position: v.Point2d{
|
|
|
|
X: player.Robot.Position.X,
|
|
|
|
Y: player.Robot.Position.Y,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.Robot.Scanners = append(p.Robot.Scanners, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
func (p *player) fire() *bot.Projectile {
|
|
|
|
for proj := range p.game.projectiles {
|
2013-08-19 20:43:26 -07:00
|
|
|
if proj.Id == p.Robot.Id {
|
2013-09-03 23:26:40 -07:00
|
|
|
return nil
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:26:40 -07:00
|
|
|
return &bot.Projectile{
|
2013-08-19 20:43:26 -07:00
|
|
|
Id: p.Robot.Id,
|
|
|
|
Position: p.Robot.Position,
|
2013-09-04 00:05:38 -07:00
|
|
|
MoveTo: *p.Robot.FireAt,
|
2013-08-19 20:43:26 -07:00
|
|
|
Damage: 10,
|
|
|
|
Radius: p.Robot.Stats.WeaponRadius,
|
|
|
|
Speed: float64(p.Robot.Stats.Speed * 2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) reset() {
|
|
|
|
start_pos := v.Point2d{
|
2013-08-19 23:25:08 -07:00
|
|
|
X: rand.Float64() * *width,
|
|
|
|
Y: rand.Float64() * *height,
|
2013-08-19 20:43:26 -07:00
|
|
|
}
|
2013-09-04 00:05:38 -07:00
|
|
|
p.Robot.MoveTo = &start_pos
|
2013-08-19 20:43:26 -07:00
|
|
|
p.Robot.Position = start_pos
|
|
|
|
p.Robot.Health = p.Robot.Stats.Hp
|
|
|
|
}
|