diff --git a/entity/comp.go b/entity/comp.go new file mode 100755 index 0000000..bca5a8c --- /dev/null +++ b/entity/comp.go @@ -0,0 +1,36 @@ +package entity + +type Comp interface { + Name() string +} + +func AddComp(e *Entity, c Comp) { + e.Comps = append(e.Comps, c) +} + +func GetComp[T Comp](e *Entity) (out T) { + + for i := 0; i < len(e.Comps); i++ { + + comp, ok := e.Comps[i].(T) + if ok { + return comp + } + } + + return out +} + +func GetAllCompOfType[T Comp](e *Entity) (out []T) { + + out = []T{} + for i := 0; i < len(e.Comps); i++ { + + comp, ok := e.Comps[i].(T) + if ok { + out = append(out, comp) + } + } + + return out +} diff --git a/entity/entity.go b/entity/entity.go index 28fb0f5..f4cc35e 100755 --- a/entity/entity.go +++ b/entity/entity.go @@ -39,38 +39,3 @@ func (e *Entity) HasFlag(ef EntityFlag) bool { func NewEntityId(generation byte, flags EntityFlag, index uint64) uint64 { return index | (uint64(generation) << GenerationShiftBits) | (uint64(flags) << FlagsShiftBits) } - -type Comp interface { - Name() string -} - -func AddComp(e *Entity, c Comp) { - e.Comps = append(e.Comps, c) -} - -func GetComp[T Comp](e *Entity) (out T) { - - for i := 0; i < len(e.Comps); i++ { - - comp, ok := e.Comps[i].(T) - if ok { - return comp - } - } - - return out -} - -func GetAllCompOfType[T Comp](e *Entity) (out []T) { - - out = []T{} - for i := 0; i < len(e.Comps); i++ { - - comp, ok := e.Comps[i].(T) - if ok { - out = append(out, comp) - } - } - - return out -}