From e7472a6d10bc9ca264c0aa3726977eeae5aca239 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 12 Nov 2014 23:19:32 -0500 Subject: [PATCH] game reset cleanup --- bomb.go | 1 + broadcast.go | 1 + connection.go | 1 + game.go | 16 +++++++++++++--- scan.go | 1 + system.go | 5 +++++ 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bomb.go b/bomb.go index f992cdc..cffacaa 100644 --- a/bomb.go +++ b/bomb.go @@ -6,6 +6,7 @@ import ( ) type Bomb struct { + NopReset player *Connection origin *System target *System diff --git a/broadcast.go b/broadcast.go index 2d97f09..d99dfc0 100644 --- a/broadcast.go +++ b/broadcast.go @@ -6,6 +6,7 @@ import ( ) type broadcast struct { + NopReset start time.Time origin *System dist float64 diff --git a/connection.go b/connection.go index a3cdc35..da2bd9c 100644 --- a/connection.go +++ b/connection.go @@ -49,6 +49,7 @@ func (conn *Connection) Reset() { Conn: conn.Conn, Reader: bufio.NewReader(conn.Conn), bombs: 1, + player: conn.player, } currentGame.Join(conn) } diff --git a/game.go b/game.go index e9f26de..0ad3f4f 100644 --- a/game.go +++ b/game.go @@ -95,6 +95,9 @@ func (g *Game) Win(winner *Connection, method string) { } func (g *Game) Reset() { + for elem, _ := range g.elems { + elem.Reset() + } connections := g.connections fresh := NewGame() *g = *fresh @@ -117,18 +120,25 @@ func (g *Game) Register(elem GameElement) { func (g *Game) tick() { g.frame += 1 + for elem := range g.elems { + elem.Tick(g.frame) + } for elem := range g.elems { if elem.Dead() { log_info("delete game object: %v", elem) delete(g.elems, elem) } } - for elem := range g.elems { - elem.Tick(g.frame) - } } type GameElement interface { Tick(frame int64) Dead() bool + Reset() +} + +type NopReset struct { +} + +func (n NopReset) Reset() { } diff --git a/scan.go b/scan.go index 91f3608..378c9c6 100644 --- a/scan.go +++ b/scan.go @@ -6,6 +6,7 @@ import ( ) type scan struct { + NopReset start time.Time origin *System dist float64 diff --git a/system.go b/system.go index a9ab640..9e26b0c 100644 --- a/system.go +++ b/system.go @@ -34,6 +34,11 @@ func (s *System) Dead() bool { return false } +func (s *System) Reset() { + s.players = make(map[*Connection]bool, 32) + s.colonizedBy = nil +} + func (s *System) Arrive(conn *Connection) { conn.SetSystem(s) log_info("player %s has arrived at system %s", conn.PlayerName(), s.name)