From c1d5033eb0472571f94c323555eab69649003234 Mon Sep 17 00:00:00 2001 From: bloeys Date: Fri, 6 Oct 2023 03:52:43 +0400 Subject: [PATCH] Separate components from entity --- entity/base_comp.go | 2 +- entity/comp.go | 22 +++++++++++++++------- entity/entity.go | 10 +--------- entity/registry.go | 7 +------ main.go | 7 ++++--- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/entity/base_comp.go b/entity/base_comp.go index 8ca1d4b..e9892aa 100755 --- a/entity/base_comp.go +++ b/entity/base_comp.go @@ -8,7 +8,7 @@ type BaseComp struct { Entity *Entity } -func (b *BaseComp) base() { +func (b *BaseComp) baseComp() { } func (b *BaseComp) Init(parent *Entity) { diff --git a/entity/comp.go b/entity/comp.go index 4f2061b..4a22dc8 100755 --- a/entity/comp.go +++ b/entity/comp.go @@ -5,7 +5,7 @@ import "github.com/bloeys/nmage/assert" type Comp interface { // This ensures that implementors of the Comp interface // always embed BaseComp - base() + baseComp() Name() string Init(parent *Entity) @@ -13,15 +13,23 @@ type Comp interface { Destroy() } -func AddComp[T Comp](e *Entity, c T) { +func NewCompContainer() CompContainer { + return CompContainer{Comps: []Comp{}} +} - assert.T(!HasComp[T](e), "Entity with id '%v' already has component of type '%T'", e.ID, c) +type CompContainer struct { + Comps []Comp +} - e.Comps = append(e.Comps, c) +func AddComp[T Comp](e *Entity, cc *CompContainer, c T) { + + assert.T(!HasComp[T](cc), "Entity with id '%v' already has component of type '%T'", e.ID, c) + + cc.Comps = append(cc.Comps, c) c.Init(e) } -func HasComp[T Comp](e *Entity) bool { +func HasComp[T Comp](e *CompContainer) bool { for i := 0; i < len(e.Comps); i++ { @@ -34,7 +42,7 @@ func HasComp[T Comp](e *Entity) bool { return false } -func GetComp[T Comp](e *Entity) (out T) { +func GetComp[T Comp](e *CompContainer) (out T) { for i := 0; i < len(e.Comps); i++ { @@ -48,7 +56,7 @@ func GetComp[T Comp](e *Entity) (out T) { } // DestroyComp calls Destroy on the component and then removes it from the entities component list -func DestroyComp[T Comp](e *Entity) { +func DestroyComp[T Comp](e *CompContainer) { for i := 0; i < len(e.Comps); i++ { diff --git a/entity/entity.go b/entity/entity.go index 8d308b3..f91d02c 100755 --- a/entity/entity.go +++ b/entity/entity.go @@ -16,22 +16,14 @@ const ( type EntityHandle uint64 type Entity struct { - // Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index - ID EntityHandle - Comps []Comp + ID EntityHandle } func (e *Entity) HasFlag(ef EntityFlag) bool { return GetFlags(e.ID)&ef > 0 } -func (e *Entity) UpdateAllComps() { - for i := 0; i < len(e.Comps); i++ { - e.Comps[i].Update() - } -} - func GetGeneration(id EntityHandle) byte { return byte(id >> GenerationShiftBits) } diff --git a/entity/registry.go b/entity/registry.go index 18be42f..bcefaa9 100755 --- a/entity/registry.go +++ b/entity/registry.go @@ -76,7 +76,7 @@ func (r *Registry) GetEntity(id EntityHandle) *Entity { return e } -// FreeEntity calls Destroy on all the entities components, resets the component list, resets the entity flags, then ads this entity to the free list +// FreeEntity resets the entity flags then adds this entity to the free list func (r *Registry) FreeEntity(id EntityHandle) { e := r.GetEntity(id) @@ -84,14 +84,9 @@ func (r *Registry) FreeEntity(id EntityHandle) { return } - for i := 0; i < len(e.Comps); i++ { - e.Comps[i].Destroy() - } - r.EntityCount-- eIndex := GetIndex(e.ID) - e.Comps = []Comp{} e.ID = NewEntityId(GetGeneration(e.ID), EntityFlag_None, eIndex) r.FreeList = &freeListitem{ diff --git a/main.go b/main.go index dc564e3..93b9189 100755 --- a/main.go +++ b/main.go @@ -89,16 +89,17 @@ func Test() { lvl := level.NewLevel("test level", 1000) e1 := lvl.Registry.NewEntity() + e1CompContainer := entity.NewCompContainer() - trComp := entity.GetComp[*TransformComp](e1) + trComp := entity.GetComp[*TransformComp](&e1CompContainer) fmt.Println("Get comp before adding any:", trComp) - entity.AddComp(e1, &TransformComp{ + entity.AddComp(e1, &e1CompContainer, &TransformComp{ Pos: gglm.NewVec3(0, 0, 0), Rot: gglm.NewQuatEulerXYZ(0, 0, 0), Scale: gglm.NewVec3(0, 0, 0), }) - trComp = entity.GetComp[*TransformComp](e1) + trComp = entity.GetComp[*TransformComp](&e1CompContainer) fmt.Println("Get transform comp:", trComp) fmt.Printf("Entity: %+v\n", e1)