From 35ff496a9a152186129a4ec2b44cedfd368e70e7 Mon Sep 17 00:00:00 2001 From: bloeys Date: Sun, 14 Aug 2022 22:00:04 +0400 Subject: [PATCH] Starting entities, components, and levels --- entity/entity.go | 43 ++++++++++++++++++++++++++++++++++++++++++ entity/registry.go | 35 ++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ---- level/level.go | 20 ++++++++++++++++++++ main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 145 insertions(+), 6 deletions(-) create mode 100755 entity/entity.go create mode 100755 entity/registry.go create mode 100755 level/level.go diff --git a/entity/entity.go b/entity/entity.go new file mode 100755 index 0000000..00ec852 --- /dev/null +++ b/entity/entity.go @@ -0,0 +1,43 @@ +package entity + +type Entity struct { + + // Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index + ID uint64 + Comps []Comp +} + +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/registry.go b/entity/registry.go new file mode 100755 index 0000000..2185fb2 --- /dev/null +++ b/entity/registry.go @@ -0,0 +1,35 @@ +package entity + +import "github.com/bloeys/nmage/assert" + +type Registry struct { + EntityCount uint64 + Entities []Entity +} + +func (r *Registry) NewEntity() *Entity { + + assert.T(r.EntityCount < uint64(len(r.Entities)), "Can not add more entities to registry because it is full") + + for i := 0; i < len(r.Entities); i++ { + + // @TODO: Implement generational indices + e := &r.Entities[i] + if e.ID == 0 { + r.EntityCount++ + e.ID = uint64(i) + 1 + + assert.T(e.ID != 0, "Entity ID must not be zero") + return e + } + } + + panic("failed to create new entity because we did not find a free spot in the registry. Why did the assert not go off?") +} + +func NewRegistry(size uint32) *Registry { + assert.T(size > 0, "Registry size must be more than zero") + return &Registry{ + Entities: make([]Entity, size), + } +} diff --git a/go.mod b/go.mod index e99a1ea..8ded7c0 100755 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/bloeys/nmage -go 1.17 +go 1.18 require github.com/veandco/go-sdl2 v0.4.10 diff --git a/go.sum b/go.sum index 86c0707..8f67548 100755 --- a/go.sum +++ b/go.sum @@ -1,15 +1,11 @@ github.com/bloeys/assimp-go v0.4.2 h1:ArVK74BCFcTO/rCGj2NgZG9xtbjnJdEn5npIeJx1Z04= github.com/bloeys/assimp-go v0.4.2/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0= -github.com/bloeys/gglm v0.3.1 h1:Sy9upW7SBsBfDXrSmEhid3aQ+7J7itej+upwcxOnPMQ= -github.com/bloeys/gglm v0.3.1/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk= github.com/bloeys/gglm v0.41.10 h1:R9FMiI+VQVXAI+vDwCB7z9xqzy5VAR1657u8TQTDNKA= github.com/bloeys/gglm v0.41.10/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0= github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= -github.com/inkyblackness/imgui-go/v4 v4.3.0 h1:iyAzqWXq/dG5+6ckDPhGivtrIo6AywGQMvENKzun04s= -github.com/inkyblackness/imgui-go/v4 v4.3.0/go.mod h1:g8SAGtOYUP7rYaOB2AsVKCEHmPMDmJKgt4z6d+flhb0= github.com/inkyblackness/imgui-go/v4 v4.5.0 h1:iUon7q0Hr0c0/Gc2V6bTP7Anu9WH7H26dI7JkPBmMEA= github.com/inkyblackness/imgui-go/v4 v4.5.0/go.mod h1:g8SAGtOYUP7rYaOB2AsVKCEHmPMDmJKgt4z6d+flhb0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/level/level.go b/level/level.go new file mode 100755 index 0000000..3f492c4 --- /dev/null +++ b/level/level.go @@ -0,0 +1,20 @@ +package level + +import ( + "github.com/bloeys/nmage/assert" + "github.com/bloeys/nmage/entity" +) + +type Level struct { + *entity.Registry + Name string +} + +func NewLevel(name string, maxEntities uint32) *Level { + + assert.T(name != "", "Level name can not be empty") + return &Level{ + Name: name, + Registry: entity.NewRegistry(maxEntities), + } +} diff --git a/main.go b/main.go index ae0338f..4873fbb 100755 --- a/main.go +++ b/main.go @@ -20,7 +20,9 @@ import ( ) // @Todo: -// Entities and components +// Complete entity registry (e.g. HasEntity, GetEntity, Generational Indices etc...) +// Helper functions to update active entities + // Integrate physx // Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) // Renderer batching @@ -50,8 +52,51 @@ type OurGame struct { ImGUIInfo nmageimgui.ImguiInfo } +// type TransformComp struct { +// Pos *gglm.Vec3 +// Rot *gglm.Quat +// Scale *gglm.Vec3 +// } + +// func (t TransformComp) Name() string { +// return "Transform Component" +// } + +// func Test() { + +// lvl := level.NewLevel("test level", 1000) +// e := lvl.Registry.NewEntity() + +// trComp := entity.GetComp[*TransformComp](e) +// fmt.Println("Got comp 1:", trComp) + +// e.Comps = append(e.Comps, &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](e) +// fmt.Println("Got comp 2:", trComp) + +// trComps := entity.GetAllCompOfType[*TransformComp](e) +// fmt.Printf("Got comp 3: %+v, %+v\n", trComps[0], trComps[1]) + +// fmt.Printf("Entity: %+v\n", e) +// fmt.Printf("Entity: %+v\n", lvl.Registry.NewEntity()) +// fmt.Printf("Entity: %+v\n", lvl.Registry.NewEntity()) +// fmt.Printf("Entity: %+v\n", lvl.Registry.NewEntity()) +// } + func main() { + // Test() + // return + //Init engine err := engine.Init() if err != nil {