Redo and simplify registry and move to own package

This commit is contained in:
bloeys
2023-10-06 07:28:16 +04:00
parent 1b83d7f9a7
commit 039d09f888
8 changed files with 198 additions and 178 deletions

View File

@ -1,6 +1,9 @@
package entity
import "github.com/bloeys/nmage/assert"
import (
"github.com/bloeys/nmage/assert"
"github.com/bloeys/nmage/registry"
)
type Comp interface {
// This ensures that implementors of the Comp interface
@ -8,7 +11,7 @@ type Comp interface {
baseComp()
Name() string
Init(parent *BaseEntity)
Init(parentHandle registry.Handle)
Update()
Destroy()
}
@ -21,12 +24,12 @@ type CompContainer struct {
Comps []Comp
}
func AddComp[T Comp](e *BaseEntity, cc *CompContainer, c T) {
func AddComp[T Comp](entityHandle registry.Handle, cc *CompContainer, c T) {
assert.T(!HasComp[T](cc), "Entity with id '%v' already has component of type '%T'", e.ID, c)
assert.T(!HasComp[T](cc), "Entity with id '%v' already has component of type '%T'", entityHandle, c)
cc.Comps = append(cc.Comps, c)
c.Init(e)
c.Init(entityHandle)
}
func HasComp[T Comp](e *CompContainer) bool {