Starting entities, components, and levels

This commit is contained in:
bloeys
2022-08-14 22:00:04 +04:00
parent 52b77e017e
commit 35ff496a9a
6 changed files with 145 additions and 6 deletions

43
entity/entity.go Executable file
View File

@ -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
}

35
entity/registry.go Executable file
View File

@ -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),
}
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/bloeys/nmage module github.com/bloeys/nmage
go 1.17 go 1.18
require github.com/veandco/go-sdl2 v0.4.10 require github.com/veandco/go-sdl2 v0.4.10

4
go.sum
View File

@ -1,15 +1,11 @@
github.com/bloeys/assimp-go v0.4.2 h1:ArVK74BCFcTO/rCGj2NgZG9xtbjnJdEn5npIeJx1Z04= 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/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 h1:R9FMiI+VQVXAI+vDwCB7z9xqzy5VAR1657u8TQTDNKA=
github.com/bloeys/gglm v0.41.10/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk= 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 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:1Zi56D0LNfvkzM+BdoxKryvUEdyWO7LP8oRT+oSYJW0=
github.com/go-gl/gl v0.0.0-20211025173605-bda47ffaa784/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= 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 h1:iUon7q0Hr0c0/Gc2V6bTP7Anu9WH7H26dI7JkPBmMEA=
github.com/inkyblackness/imgui-go/v4 v4.5.0/go.mod h1:g8SAGtOYUP7rYaOB2AsVKCEHmPMDmJKgt4z6d+flhb0= 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= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

20
level/level.go Executable file
View File

@ -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),
}
}

47
main.go
View File

@ -20,7 +20,9 @@ import (
) )
// @Todo: // @Todo:
// Entities and components // Complete entity registry (e.g. HasEntity, GetEntity, Generational Indices etc...)
// Helper functions to update active entities
// Integrate physx // Integrate physx
// Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing) // Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing)
// Renderer batching // Renderer batching
@ -50,8 +52,51 @@ type OurGame struct {
ImGUIInfo nmageimgui.ImguiInfo 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() { func main() {
// Test()
// return
//Init engine //Init engine
err := engine.Init() err := engine.Init()
if err != nil { if err != nil {