move registerComponent
This commit is contained in:
parent
d564ee08d4
commit
ee12b90f1e
1 changed files with 30 additions and 30 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue