mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 05:18:21 +00:00
Change Entity->BaseEntity + Add Entity interface
This commit is contained in:
@ -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() {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user