Change Entity->BaseEntity + Add Entity interface

This commit is contained in:
bloeys
2023-10-06 04:23:42 +04:00
parent 201d9546b2
commit 1b83d7f9a7
4 changed files with 27 additions and 14 deletions

View File

@ -5,23 +5,23 @@ import "github.com/bloeys/nmage/assert"
var _ Comp = &BaseComp{} var _ Comp = &BaseComp{}
type BaseComp struct { type BaseComp struct {
Entity *Entity Entity *BaseEntity
} }
func (b BaseComp) baseComp() { func (b BaseComp) baseComp() {
} }
func (b *BaseComp) Init(parent *Entity) { func (b *BaseComp) Init(parent *BaseEntity) {
assert.T(parent != nil, "Component was initialized with a nil parent. That is not allowed.") assert.T(parent != nil, "Component was initialized with a nil parent. That is not allowed.")
b.Entity = parent b.Entity = parent
} }
func (b *BaseComp) Name() string { func (b BaseComp) Name() string {
return "Base Component" return "Base Component"
} }
func (b *BaseComp) Update() { func (b BaseComp) Update() {
} }
func (b *BaseComp) Destroy() { func (b BaseComp) Destroy() {
} }

View File

@ -8,7 +8,7 @@ type Comp interface {
baseComp() baseComp()
Name() string Name() string
Init(parent *Entity) Init(parent *BaseEntity)
Update() Update()
Destroy() Destroy()
} }
@ -21,7 +21,7 @@ type CompContainer struct {
Comps []Comp Comps []Comp
} }
func AddComp[T Comp](e *Entity, cc *CompContainer, c T) { func AddComp[T Comp](e *BaseEntity, cc *CompContainer, c T) {
assert.T(!HasComp[T](cc), "Entity with id '%v' already has component of type '%T'", e.ID, c) assert.T(!HasComp[T](cc), "Entity with id '%v' already has component of type '%T'", e.ID, c)

View File

@ -13,14 +13,27 @@ const (
IndexBitMask = 0x00_00_FFFF_FFFF_FFFF IndexBitMask = 0x00_00_FFFF_FFFF_FFFF
) )
type Entity interface {
baseEntity()
GetHandle() EntityHandle
}
type EntityHandle uint64 type EntityHandle uint64
type Entity struct { var _ Entity = &BaseEntity{}
type BaseEntity struct {
// Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index // Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index
ID EntityHandle ID EntityHandle
} }
func (e *Entity) HasFlag(ef EntityFlag) bool { func (be BaseEntity) baseEntity() {}
func (be BaseEntity) GetHandle() EntityHandle {
return be.ID
}
func (e *BaseEntity) HasFlag(ef EntityFlag) bool {
return GetFlags(e.ID)&ef > 0 return GetFlags(e.ID)&ef > 0
} }

View File

@ -17,18 +17,18 @@ type freeListitem struct {
type Registry struct { type Registry struct {
EntityCount uint64 EntityCount uint64
Entities []Entity Entities []BaseEntity
FreeList *freeListitem FreeList *freeListitem
FreeListSize uint32 FreeListSize uint32
} }
func (r *Registry) NewEntity() *Entity { func (r *Registry) NewEntity() *BaseEntity {
assert.T(r.EntityCount < uint64(len(r.Entities)), "Can not add more entities to registry because it is full") assert.T(r.EntityCount < uint64(len(r.Entities)), "Can not add more entities to registry because it is full")
entityToUseIndex := uint64(0) entityToUseIndex := uint64(0)
var entityToUse *Entity = nil var entityToUse *BaseEntity = nil
if r.FreeList != nil && r.FreeListSize > FreeListUsageThreshold { if r.FreeList != nil && r.FreeListSize > FreeListUsageThreshold {
@ -61,7 +61,7 @@ func (r *Registry) NewEntity() *Entity {
return entityToUse return entityToUse
} }
func (r *Registry) GetEntity(id EntityHandle) *Entity { func (r *Registry) GetEntity(id EntityHandle) *BaseEntity {
index := GetIndex(id) index := GetIndex(id)
gen := GetGeneration(id) gen := GetGeneration(id)
@ -99,6 +99,6 @@ func (r *Registry) FreeEntity(id EntityHandle) {
func NewRegistry(size uint32) *Registry { func NewRegistry(size uint32) *Registry {
assert.T(size > 0, "Registry size must be more than zero") assert.T(size > 0, "Registry size must be more than zero")
return &Registry{ return &Registry{
Entities: make([]Entity, size), Entities: make([]BaseEntity, size),
} }
} }