game types, deathmatch and melee

This commit is contained in:
Fraser Graham
2014-01-15 21:41:40 -08:00
parent 845db00032
commit 9583de2ffa
4 changed files with 95 additions and 28 deletions
+13 -27
View File
@@ -66,6 +66,13 @@ type game struct {
repair_rate float32
tick_duration int
winners WinnerMap
mode GameMode
}
type GameMode interface {
setup(g *game)
tick(gg *game, payload *Boardstate)
gameOver(gg *game) (bool, *GameOver)
}
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *game {
@@ -91,7 +98,11 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int) *
tick_duration: tick,
players_remaining: 2,
winners: WinnerMap{m: make(map[string]int)},
mode: &melee{respawn: make(map[*Robot]float64)},
}
g.mode.setup(g)
log.Printf("NewGame: %+v", g)
return g
}
@@ -285,11 +296,12 @@ func (g *game) run() {
}
// UPDATE GAME STATE
if end, data := g.gameOver(); end {
if end, data := g.mode.gameOver(g); end {
g.sendGameOver(data)
}
g.tick(payload)
g.mode.tick(g, payload)
t1 = time.Now()
if *verbose {
@@ -307,32 +319,6 @@ func (g *game) run() {
}
}
func (g *game) gameOver() (bool, *GameOver) {
over := false
var stats *GameOver
if g.players_remaining <= 1 && len(g.players) > 1 {
g.obstacles = GenerateObstacles(g.obstacle_count, g.width, g.height)
log.Printf("game %s: game over", g.id)
stats = NewGameOver()
for p := range g.players {
for _, r := range p.Robots {
if r.Health > 0 {
log.Printf("Robot %v Survived", r.Id)
g.winners.Lock()
g.winners.m[r.Id] += 1
g.winners.Unlock()
stats.Winners = append(stats.Winners, r.Id)
}
r.reset(g)
}
}
over = true
}
return over, stats
}
func (g *game) sendGameOver(eg *GameOver) {
log.Printf("sending out game over message: %+v", eg)
for p := range g.players {