From ee12b90f1eaf54aebf5c627b982896d6d772d08d Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Fri, 27 Aug 2021 16:59:20 +1000 Subject: [PATCH] move registerComponent --- engine/game.go | 60 +++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/engine/game.go b/engine/game.go index f45bb73..6b38373 100644 --- a/engine/game.go +++ b/engine/game.go @@ -55,36 +55,6 @@ func (g *Game) Update() error { return g.Root.Update() } -func (g *Game) registerComponent(c interface{}, path []interface{}) error { - // register in g.dex - ct := reflect.TypeOf(c) - for _, b := range Behaviours { - if !ct.Implements(b) { - continue - } - // TODO: sub-quadratic? - for _, p := range append(path, c) { - k := dexKey{p, b} - g.dex[k] = append(g.dex[k], c) - } - } - - // register in g.db - i, ok := c.(Identifier) - if !ok { - return nil - } - id := i.Ident() - if id == "" { - return nil - } - if _, exists := g.db[id]; exists { - return fmt.Errorf("duplicate id %q", id) - } - g.db[id] = i - return nil -} - // Component returns the component with a given ID, or nil if there is none. // This only returns sensible values after LoadAndPrepare. func (g *Game) Component(id string) Identifier { @@ -160,3 +130,33 @@ func (g *Game) LoadAndPrepare(assets fs.FS) error { } return nil } + +func (g *Game) registerComponent(c interface{}, path []interface{}) error { + // register in g.dex + ct := reflect.TypeOf(c) + for _, b := range Behaviours { + if !ct.Implements(b) { + continue + } + // TODO: sub-quadratic? + for _, p := range append(path, c) { + k := dexKey{p, b} + g.dex[k] = append(g.dex[k], c) + } + } + + // register in g.db + i, ok := c.(Identifier) + if !ok { + return nil + } + id := i.Ident() + if id == "" { + return nil + } + if _, exists := g.db[id]; exists { + return fmt.Errorf("duplicate id %q", id) + } + g.db[id] = i + return nil +}