walk change, parentals

This commit is contained in:
Josh Deprez 2021-08-30 16:25:50 +10:00
parent 4557f78175
commit f39c91a988
2 changed files with 58 additions and 29 deletions

View file

@ -2,6 +2,7 @@ package engine
import ( import (
"encoding/gob" "encoding/gob"
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"reflect" "reflect"
@ -18,6 +19,11 @@ var _ interface {
Scanner Scanner
} = &Game{} } = &Game{}
var (
errNilComponent = errors.New("nil component")
errNilParent = errors.New("nil parent")
)
func init() { func init() {
gob.Register(&Game{}) gob.Register(&Game{})
} }
@ -97,23 +103,23 @@ func (g *Game) Query(ancestorID string, behaviour reflect.Type) map[interface{}]
// Scan implements Scanner. // Scan implements Scanner.
func (g *Game) Scan() []interface{} { return []interface{}{g.Root} } func (g *Game) Scan() []interface{} { return []interface{}{g.Root} }
// Walk calls v with every path of components reachable from c via Scan, for as // Walk calls visit with every component and its parent, reachable from the
// long as visit returns nil. // given component via Scan, for as long as visit returns nil. The parent of
func Walk(c interface{}, v func(interface{}, []interface{}) error) error { // the first component (as passed to visit) will be nil.
return walk(c, make([]interface{}, 0, 16), v) func Walk(component interface{}, visit func(component, parent interface{}) error) error {
return walk(component, nil, visit)
} }
func walk(c interface{}, p []interface{}, v func(interface{}, []interface{}) error) error { func walk(component, parent interface{}, visit func(component, parent interface{}) error) error {
if err := v(c, p); err != nil { if err := visit(component, parent); err != nil {
return err return err
} }
sc, ok := c.(Scanner) sc, ok := component.(Scanner)
if !ok { if !ok {
return nil return nil
} }
p = append(p, c)
for _, c := range sc.Scan() { for _, c := range sc.Scan() {
if err := walk(c, p, v); err != nil { if err := walk(c, component, visit); err != nil {
return err return err
} }
} }
@ -125,7 +131,7 @@ func walk(c interface{}, p []interface{}, v func(interface{}, []interface{}) err
// LoadAndPrepare must be called before any calls to Component or Query. // LoadAndPrepare must be called before any calls to Component or Query.
func (g *Game) LoadAndPrepare(assets fs.FS) error { func (g *Game) LoadAndPrepare(assets fs.FS) error {
// Load all the Loaders. // Load all the Loaders.
if err := Walk(g, func(c interface{}, _ []interface{}) error { if err := Walk(g, func(c, _ interface{}) error {
l, ok := c.(Loader) l, ok := c.(Loader)
if !ok { if !ok {
return nil return nil
@ -154,20 +160,35 @@ func (g *Game) LoadAndPrepare(assets fs.FS) error {
return nil return nil
} }
func (g *Game) registerComponent(c interface{}, path []interface{}) error { // RegisterComponent registers a component into the component database (as the
// child of a given parent). Passing a nil component or parent is an error.
// Registering multiple components with the same ID is also an error.
func (g *Game) RegisterComopnent(component, parent interface{}) error {
if component == nil {
return errNilComponent
}
if parent == nil && component != g {
return errNilParent
}
g.dbmu.Lock()
defer g.dbmu.Unlock()
return g.registerComponent(component, parent)
}
func (g *Game) registerComponent(component, parent interface{}) error {
// register in g.par // register in g.par
if l := len(path); l > 0 { if parent != nil {
g.par[c] = path[l-1] g.par[component] = parent
} }
// register in g.byAB // register in g.byAB
ct := reflect.TypeOf(c) ct := reflect.TypeOf(component)
for _, b := range Behaviours { for _, b := range Behaviours {
if !ct.Implements(b) { if !ct.Implements(b) {
continue continue
} }
// TODO: this is quadratic - do better? // TODO: better than O(len(path)^2) time and memory?
for _, p := range append(path, c) { for p := component; p != nil; p = g.par[p] {
i, ok := p.(Identifier) i, ok := p.(Identifier)
if !ok { if !ok {
continue continue
@ -176,12 +197,12 @@ func (g *Game) registerComponent(c interface{}, path []interface{}) error {
if g.byAB[k] == nil { if g.byAB[k] == nil {
g.byAB[k] = make(map[interface{}]struct{}) g.byAB[k] = make(map[interface{}]struct{})
} }
g.byAB[k][c] = struct{}{} g.byAB[k][component] = struct{}{}
} }
} }
// register in g.byID if needed // register in g.byID if needed
i, ok := c.(Identifier) i, ok := component.(Identifier)
if !ok { if !ok {
return nil return nil
} }
@ -194,20 +215,24 @@ func (g *Game) registerComponent(c interface{}, path []interface{}) error {
} }
// UnregisterComponent removes the component from the component database. // UnregisterComponent removes the component from the component database.
func (g *Game) UnregisterComponent(c interface{}) { // Passing a nil component has no effect.
func (g *Game) UnregisterComponent(component interface{}) {
if component == nil {
return
}
g.dbmu.Lock() g.dbmu.Lock()
g.unregisterComponent(c) g.unregisterComponent(component)
g.dbmu.Unlock() g.dbmu.Unlock()
} }
func (g *Game) unregisterComponent(c interface{}) { func (g *Game) unregisterComponent(component interface{}) {
// unregister from g.byAB, using g.par to trace the path // unregister from g.byAB, using g.par to trace the path
ct := reflect.TypeOf(c) ct := reflect.TypeOf(component)
for _, b := range Behaviours { for _, b := range Behaviours {
if !ct.Implements(b) { if !ct.Implements(b) {
continue continue
} }
for p := c; p != nil; p = g.par[p] { for p := component; p != nil; p = g.par[p] {
i, ok := p.(Identifier) i, ok := p.(Identifier)
if !ok { if !ok {
continue continue
@ -216,15 +241,15 @@ func (g *Game) unregisterComponent(c interface{}) {
if g.byAB[k] == nil { if g.byAB[k] == nil {
continue continue
} }
delete(g.byAB[k], c) delete(g.byAB[k], component)
} }
} }
// unregister from g.par // unregister from g.par
delete(g.par, c) delete(g.par, component)
// unregister from g.byID if needed // unregister from g.byID if needed
i, ok := c.(Identifier) i, ok := component.(Identifier)
if !ok { if !ok {
return return
} }

View file

@ -88,10 +88,14 @@ func (g *Game) cmdTree(dst io.Writer, argv []string) {
return return
} }
} }
Walk(c, func(c interface{}, p []interface{}) error { Walk(c, func(c, p interface{}) error {
indent := "" indent := ""
if len(p) > 0 { l := 0
indent = strings.Repeat(" ", len(p)-1) + "↳ " for ; p != nil; p = g.par[p] {
l++
}
if l > 0 {
indent = strings.Repeat(" ", l-1) + "↳ "
} }
i, ok := c.(Identifier) i, ok := c.(Identifier)
if ok { if ok {