game reset cleanup

slack
Jordan Orelli 10 years ago
parent 6ee3051976
commit e7472a6d10

@ -6,6 +6,7 @@ import (
) )
type Bomb struct { type Bomb struct {
NopReset
player *Connection player *Connection
origin *System origin *System
target *System target *System

@ -6,6 +6,7 @@ import (
) )
type broadcast struct { type broadcast struct {
NopReset
start time.Time start time.Time
origin *System origin *System
dist float64 dist float64

@ -49,6 +49,7 @@ func (conn *Connection) Reset() {
Conn: conn.Conn, Conn: conn.Conn,
Reader: bufio.NewReader(conn.Conn), Reader: bufio.NewReader(conn.Conn),
bombs: 1, bombs: 1,
player: conn.player,
} }
currentGame.Join(conn) currentGame.Join(conn)
} }

@ -95,6 +95,9 @@ func (g *Game) Win(winner *Connection, method string) {
} }
func (g *Game) Reset() { func (g *Game) Reset() {
for elem, _ := range g.elems {
elem.Reset()
}
connections := g.connections connections := g.connections
fresh := NewGame() fresh := NewGame()
*g = *fresh *g = *fresh
@ -117,18 +120,25 @@ func (g *Game) Register(elem GameElement) {
func (g *Game) tick() { func (g *Game) tick() {
g.frame += 1 g.frame += 1
for elem := range g.elems {
elem.Tick(g.frame)
}
for elem := range g.elems { for elem := range g.elems {
if elem.Dead() { if elem.Dead() {
log_info("delete game object: %v", elem) log_info("delete game object: %v", elem)
delete(g.elems, elem) delete(g.elems, elem)
} }
} }
for elem := range g.elems {
elem.Tick(g.frame)
}
} }
type GameElement interface { type GameElement interface {
Tick(frame int64) Tick(frame int64)
Dead() bool Dead() bool
Reset()
}
type NopReset struct {
}
func (n NopReset) Reset() {
} }

@ -6,6 +6,7 @@ import (
) )
type scan struct { type scan struct {
NopReset
start time.Time start time.Time
origin *System origin *System
dist float64 dist float64

@ -34,6 +34,11 @@ func (s *System) Dead() bool {
return false return false
} }
func (s *System) Reset() {
s.players = make(map[*Connection]bool, 32)
s.colonizedBy = nil
}
func (s *System) Arrive(conn *Connection) { func (s *System) Arrive(conn *Connection) {
conn.SetSystem(s) conn.SetSystem(s)
log_info("player %s has arrived at system %s", conn.PlayerName(), s.name) log_info("player %s has arrived at system %s", conn.PlayerName(), s.name)

Loading…
Cancel
Save