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{}
type BaseComp struct {
Entity *Entity
Entity *BaseEntity
}
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.")
b.Entity = parent
}
func (b *BaseComp) Name() string {
func (b BaseComp) Name() string {
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()
Name() string
Init(parent *Entity)
Init(parent *BaseEntity)
Update()
Destroy()
}
@ -21,7 +21,7 @@ type CompContainer struct {
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)

View File

@ -13,14 +13,27 @@ const (
IndexBitMask = 0x00_00_FFFF_FFFF_FFFF
)
type Entity interface {
baseEntity()
GetHandle() EntityHandle
}
type EntityHandle uint64
type Entity struct {
var _ Entity = &BaseEntity{}
type BaseEntity struct {
// Byte 1: Generation; Byte 2: Flags; Bytes 3-8: Index
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
}

View File

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