moved a bunch of logic out of run and into functions of the game, for a cleaner separation of functionality.
Also, removed a bunch of overly verbose logging
This commit is contained in:
@@ -104,6 +104,75 @@ func NewGame(id string, width, height float32) *game {
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *game) tick(payload *Boardstate) int {
|
||||
robots_remaining := 0
|
||||
|
||||
// Update Players
|
||||
for p := range g.players {
|
||||
if p.Robot.Health > 0 {
|
||||
robots_remaining++
|
||||
p.Tick(g)
|
||||
}
|
||||
payload.Robots = append(payload.Robots, p.Robot)
|
||||
}
|
||||
|
||||
// Update Projectiles
|
||||
for pr := range g.projectiles {
|
||||
pr.Tick(g)
|
||||
}
|
||||
|
||||
// We do this here, because the tick calls can alter g.projectiles
|
||||
for pr := range g.projectiles {
|
||||
payload.Projectiles = append(payload.Projectiles, *pr)
|
||||
}
|
||||
|
||||
// Update Splosions
|
||||
for s := range g.splosions {
|
||||
s.Tick()
|
||||
if !s.Alive() {
|
||||
delete(g.splosions, s)
|
||||
}
|
||||
payload.Splosions = append(payload.Splosions, *s)
|
||||
}
|
||||
|
||||
return robots_remaining
|
||||
}
|
||||
|
||||
func (g *game) send_update(payload *Boardstate) {
|
||||
// Ensure that the robots are always sent in a consistent order
|
||||
sort.Sort(RobotSorter{Robots: payload.Robots})
|
||||
|
||||
for p := range g.players {
|
||||
// Copy the payload but only add the robots in scanner range
|
||||
player_payload := NewBoardstate()
|
||||
player_payload.Projectiles = payload.Projectiles
|
||||
player_payload.Splosions = payload.Splosions
|
||||
player_payload.Turn = payload.Turn
|
||||
player_payload.Reset = payload.Reset
|
||||
player_payload.Robots = append(player_payload.Robots, p.Robot)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
player_payload.Robots = payload.Robots
|
||||
}
|
||||
|
||||
// x, _ := json.Marshal(player_payload)
|
||||
// log.Printf("%v", string(x))
|
||||
p.send <- player_payload
|
||||
}
|
||||
for s := range g.spectators {
|
||||
s.send <- payload
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (g *game) run() {
|
||||
var t0, t1 time.Time
|
||||
ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond)
|
||||
@@ -129,13 +198,11 @@ func (g *game) run() {
|
||||
delete(g.spectators, s)
|
||||
close(s.send)
|
||||
case <-ticker.C:
|
||||
t0 = time.Now()
|
||||
payload := NewBoardstate()
|
||||
|
||||
g.turn++
|
||||
payload.Turn = g.turn
|
||||
|
||||
t0 = time.Now()
|
||||
|
||||
if *verbose {
|
||||
log.Printf("\033[2JTurn: %v", g.turn)
|
||||
log.Printf("Players: %v", len(g.players))
|
||||
@@ -143,39 +210,10 @@ func (g *game) run() {
|
||||
log.Printf("Explosions: %v", len(g.splosions))
|
||||
}
|
||||
|
||||
robots_remaining := 0
|
||||
|
||||
// UPDATE GAME STATE
|
||||
for p := range g.players {
|
||||
if p.Robot.Health > 0 {
|
||||
robots_remaining++
|
||||
// TODO: measure if this would be better to go and wait ...
|
||||
p.scan(g.players)
|
||||
p.nudge(g)
|
||||
if p.Robot.FireAt != nil {
|
||||
proj := p.fire(g.projectiles, g.turn)
|
||||
if proj != nil {
|
||||
g.projectiles[proj] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
payload.Robots = append(payload.Robots, p.Robot)
|
||||
}
|
||||
sort.Sort(RobotSorter{Robots: payload.Robots})
|
||||
|
||||
g.nudgeProjectiles()
|
||||
for p := range g.projectiles {
|
||||
payload.Projectiles = append(payload.Projectiles, *p)
|
||||
}
|
||||
|
||||
for s := range g.splosions {
|
||||
s.Tick()
|
||||
if !s.Alive() {
|
||||
delete(g.splosions, s)
|
||||
}
|
||||
payload.Splosions = append(payload.Splosions, *s)
|
||||
}
|
||||
robots_remaining := g.tick(payload)
|
||||
|
||||
// Determine end game?
|
||||
if robots_remaining <= 1 && len(g.players) > 1 {
|
||||
for p := range g.players {
|
||||
if p.Robot.Health > 0 {
|
||||
@@ -195,34 +233,7 @@ func (g *game) run() {
|
||||
}
|
||||
|
||||
// SEND THE UPDATE TO EACH PLAYER
|
||||
for p := range g.players {
|
||||
// Copy the payload but only add the robots in scanner range
|
||||
player_payload := NewBoardstate()
|
||||
player_payload.Projectiles = payload.Projectiles
|
||||
player_payload.Splosions = payload.Splosions
|
||||
player_payload.Turn = payload.Turn
|
||||
player_payload.Reset = payload.Reset
|
||||
player_payload.Robots = append(player_payload.Robots, p.Robot)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
player_payload.Robots = payload.Robots
|
||||
}
|
||||
|
||||
// x, _ := json.Marshal(player_payload)
|
||||
// log.Printf("%v", string(x))
|
||||
p.send <- player_payload
|
||||
}
|
||||
for s := range g.spectators {
|
||||
s.send <- payload
|
||||
}
|
||||
g.send_update(payload)
|
||||
|
||||
t1 = time.Now()
|
||||
if *verbose {
|
||||
@@ -231,11 +242,3 @@ func (g *game) run() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *game) nudgeProjectiles() (rprojectiles []Projectile) {
|
||||
rprojectiles = make([]Projectile, 0)
|
||||
for p := range g.projectiles {
|
||||
p.Tick(g)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user