comments cleanups

This commit is contained in:
Josh Deprez 2021-09-21 17:09:49 +10:00
parent 661a6bbbc0
commit 6e771cd3f2
3 changed files with 28 additions and 21 deletions

View file

@ -111,23 +111,40 @@ func (g *Game) Parent(c interface{}) interface{} {
return g.par[c] return g.par[c]
} }
func reverse(s []interface{}) { // PathRegister calls Register on every Registrar in the path between g and
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { // parent (top-to-bottom, i.e. g first)
s[i], s[j] = s[j], s[i] func (g *Game) PathRegister(component, parent interface{}) error {
for _, p := range g.Path(parent) {
if r, ok := p.(Registrar); ok {
if err := r.Register(component, parent); err != nil {
return err
}
}
}
return nil
}
// PathUnregister calls Unregister on every Registrar in the path between g and
// parent (bottom-to-top, i.e. parent first)
func (g *Game) PathUnregister(component interface{}) {
for _, p := range g.ReversePath(component) {
if r, ok := p.(Registrar); ok {
r.Unregister(component)
}
} }
} }
// Path returns a slice with the path of components to reach component from g.
func (g *Game) Path(component interface{}) []interface{} { func (g *Game) Path(component interface{}) []interface{} {
var stack []interface{} stack := g.ReversePath(component)
g.dbmu.RLock() for i, j := 0, len(stack)-1; i < j; i, j = i+1, j-1 {
for p := component; p != nil; p = g.Parent(p) { stack[i], stack[j] = stack[j], stack[i]
stack = append(stack, p)
} }
g.dbmu.RUnlock()
reverse(stack)
return stack return stack
} }
// ReversePath returns the same slice as Path, but reversed. (ReversePath is
// faster than Path).
func (g *Game) ReversePath(component interface{}) []interface{} { func (g *Game) ReversePath(component interface{}) []interface{} {
var stack []interface{} var stack []interface{}
g.dbmu.RLock() g.dbmu.RLock()

View file

@ -127,13 +127,7 @@ func (aw *Awakeman) realUpdate() error {
} }
// Add bubble to same parent as aw // Add bubble to same parent as aw
par := aw.game.Parent(aw) par := aw.game.Parent(aw)
for _, c := range aw.game.Path(par) { aw.game.PathRegister(bubble, par)
if r, ok := c.(engine.Registrar); ok {
if err := r.Register(bubble, par); err != nil {
return err
}
}
}
if err := engine.PostorderWalk(bubble, func(c, _ interface{}) error { if err := engine.PostorderWalk(bubble, func(c, _ interface{}) error {
if p, ok := c.(engine.Prepper); ok { if p, ok := c.(engine.Prepper); ok {
return p.Prepare(aw.game) return p.Prepare(aw.game)

View file

@ -71,11 +71,7 @@ func (b *Bubble) Prepare(g *engine.Game) error {
func (b *Bubble) Update() error { func (b *Bubble) Update() error {
b.Life-- b.Life--
if b.Life <= 0 { if b.Life <= 0 {
for _, c := range b.game.ReversePath(b) { b.game.PathUnregister(b)
if r, ok := c.(engine.Registrar); ok {
r.Unregister(b)
}
}
} }
if false { if false {
// not using MoveX/MoveY/... because collisions are unnecessary - // not using MoveX/MoveY/... because collisions are unnecessary -