From 855cbfaba34140f41c2e310e7acdd7eb1f7daa3f Mon Sep 17 00:00:00 2001 From: bloeys Date: Tue, 6 Dec 2022 06:17:25 +0400 Subject: [PATCH] Improve assert+imporve error messages when adding comps --- assert/assert.go | 4 +--- entity/comp.go | 2 +- entity/registry.go | 1 + main.go | 11 +++-------- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/assert/assert.go b/assert/assert.go index 8c14a87..b23598e 100755 --- a/assert/assert.go +++ b/assert/assert.go @@ -1,8 +1,6 @@ package assert import ( - "fmt" - "github.com/bloeys/nmage/consts" "github.com/bloeys/nmage/logging" ) @@ -10,6 +8,6 @@ import ( func T(check bool, msg string, args ...any) { if consts.Debug && !check { - logging.ErrLog.Panicln("Assert failed:", fmt.Sprintf(msg, args...)) + logging.ErrLog.Panicf("Assert failed: "+msg, args...) } } diff --git a/entity/comp.go b/entity/comp.go index 633f0c4..4f2061b 100755 --- a/entity/comp.go +++ b/entity/comp.go @@ -15,7 +15,7 @@ type Comp interface { func AddComp[T Comp](e *Entity, c T) { - assert.T(!HasComp[T](e), "Entity with id %v already has component with name %s", e.ID, c.Name()) + assert.T(!HasComp[T](e), "Entity with id '%v' already has component of type '%T'", e.ID, c) e.Comps = append(e.Comps, c) c.Init(e) diff --git a/entity/registry.go b/entity/registry.go index efb358a..18be42f 100755 --- a/entity/registry.go +++ b/entity/registry.go @@ -76,6 +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 func (r *Registry) FreeEntity(id EntityHandle) { e := r.GetEntity(id) diff --git a/main.go b/main.go index aaa36f4..5204a5f 100755 --- a/main.go +++ b/main.go @@ -88,20 +88,15 @@ func Test() { e1 := lvl.Registry.NewEntity() trComp := entity.GetComp[*TransformComp](e1) - fmt.Println("Got comp 1:", trComp) + fmt.Println("Get comp before adding any:", trComp) - e1.Comps = append(e1.Comps, &TransformComp{ + entity.AddComp(e1, &TransformComp{ Pos: gglm.NewVec3(0, 0, 0), Rot: gglm.NewQuatEulerXYZ(0, 0, 0), Scale: gglm.NewVec3(0, 0, 0), - }, &TransformComp{ - Pos: gglm.NewVec3(0, 0, 0), - Rot: gglm.NewQuatEulerXYZ(0, 0, 0), - Scale: gglm.NewVec3(1, 1, 1), }) - trComp = entity.GetComp[*TransformComp](e1) - fmt.Println("Got comp 2:", trComp) + fmt.Println("Get transform comp:", trComp) fmt.Printf("Entity: %+v\n", e1) fmt.Printf("Entity: %+v\n", lvl.Registry.NewEntity())