All robots are spectators
This commit is contained in:
+35
-23
@@ -18,30 +18,13 @@ type size struct {
|
||||
width, height int
|
||||
}
|
||||
|
||||
type SimpleSpectator struct {
|
||||
width, height float64
|
||||
viewX, viewY int
|
||||
update chan *server.Boardstate
|
||||
Die chan struct{}
|
||||
}
|
||||
|
||||
// NewSimpleSpectator simply returns a populated, usable *SimplePlayer
|
||||
func NewSimpleSpectator(width, height float64) *SimpleSpectator {
|
||||
return &SimpleSpectator{
|
||||
width: width,
|
||||
height: height,
|
||||
update: make(chan *server.Boardstate),
|
||||
Die: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Recv is our implementation of receiving a server.Boardstate from the server
|
||||
func (s *SimpleSpectator) Update(bs *server.Boardstate) map[string]server.Instruction {
|
||||
s.update <- bs
|
||||
func (s *Client) Recv(bs *server.Boardstate) map[string]server.Instruction {
|
||||
s.StateStream <- bs
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SimpleSpectator) Run() {
|
||||
func (s *Client) Visualize() {
|
||||
err := termbox.Init()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -49,7 +32,7 @@ func (s *SimpleSpectator) Run() {
|
||||
|
||||
s.viewX, s.viewY = termbox.Size()
|
||||
|
||||
events := make(chan termbox.Event)
|
||||
events := make(chan termbox.Event, 1024)
|
||||
go func() {
|
||||
for {
|
||||
events <- termbox.PollEvent()
|
||||
@@ -83,7 +66,7 @@ func (s *SimpleSpectator) Run() {
|
||||
case termbox.EventError:
|
||||
log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
|
||||
}
|
||||
case u := <-s.update:
|
||||
case u := <-s.StateStream:
|
||||
termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
|
||||
|
||||
for _, obstacle := range u.Obstacles {
|
||||
@@ -103,7 +86,7 @@ func (s *SimpleSpectator) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
for _, bot := range u.OtherRobots {
|
||||
for _, bot := range u.MyRobots {
|
||||
x := int((bot.Position.X / s.width) * float64(s.viewX))
|
||||
y := int((bot.Position.Y / s.height) * float64(s.viewY))
|
||||
var b rune
|
||||
@@ -132,6 +115,35 @@ func (s *SimpleSpectator) Run() {
|
||||
)
|
||||
}
|
||||
|
||||
for _, bot := range u.OtherRobots {
|
||||
x := int((bot.Position.X / s.width) * float64(s.viewX))
|
||||
y := int((bot.Position.Y / s.height) * float64(s.viewY))
|
||||
var b rune
|
||||
if math.Abs(bot.Heading.X) > math.Abs(bot.Heading.Y) {
|
||||
if bot.Heading.X > 0 {
|
||||
b = botRight
|
||||
} else {
|
||||
b = botLeft
|
||||
}
|
||||
} else {
|
||||
if bot.Heading.Y > 0 {
|
||||
b = botUp
|
||||
} else {
|
||||
b = botDown
|
||||
}
|
||||
}
|
||||
c := termbox.ColorRed
|
||||
if bot.Health <= 0 {
|
||||
c = termbox.ColorBlack
|
||||
}
|
||||
termbox.SetCell(
|
||||
x,
|
||||
s.viewY-y,
|
||||
b,
|
||||
c, termbox.ColorBlack,
|
||||
)
|
||||
}
|
||||
|
||||
for _, p := range u.Projectiles {
|
||||
x := int((p.Position.X / s.width) * float64(s.viewX))
|
||||
y := int((p.Position.Y / s.height) * float64(s.viewY))
|
||||
|
||||
Reference in New Issue
Block a user