package ecs
import (
"reflect"
"time"
"github.com/mlange-42/ark/ecs/stats"
)
type World struct {
stats *stats .World
resources Resources
storage storage
}
func NewWorld (initialCapacity ...int ) *World {
return &World {
storage : newStorage (16 , initialCapacity ...),
resources : newResources (),
stats : &stats .World {},
}
}
func (w *World ) NewEntity () Entity {
w .checkLocked ()
entity , _ := w .storage .createEntity (0 )
w .storage .observers .FireCreateEntityIfHas (entity , &w .storage .archetypes [0 ].mask )
return entity
}
func (w *World ) NewEntities (count int , fn func (entity Entity )) {
w .checkLocked ()
tableID , start , _ := w .newEntities (count , nil , nil , 0 )
hasObs := w .storage .observers .HasObservers (OnCreateEntity )
shouldLock := hasObs || fn != nil
var lock uint8
if shouldLock {
lock = w .lock ()
}
if fn != nil {
table := &w .storage .tables [tableID ]
for i := range count {
index := uintptr (start + i )
fn (
table .GetEntity (index ),
)
}
}
if hasObs {
table := &w .storage .tables [tableID ]
mask := &w .storage .archetypes [table .archetype ].mask
w .storage .observers .FireCreateEntityBatch (table , start , mask )
}
if shouldLock {
w .unlock (lock )
}
}
func (w *World ) CopyEntity (e Entity ) Entity {
w .checkLocked ()
s := &w .storage
entity := s .entityPool .Get ()
index := s .entities [e .id ]
table := &s .tables [index .table ]
idx := table .Add (entity )
if int (entity .id ) == len (s .entities ) {
s .entities = append (s .entities , entityIndex {table : index .table , row : idx })
s .isTarget = append (s .isTarget , false )
} else {
s .entities [entity .id ] = entityIndex {table : index .table , row : idx }
}
archetype := &s .archetypes [table .archetype ]
table .CopyAll (table , idx , index .row )
w .storage .observers .FireCreateEntityIfHas (entity , &archetype .mask )
if archetype .HasRelations () {
w .storage .observers .FireCreateEntityRelIfHas (entity , &archetype .mask )
}
return entity
}
func (w *World ) Alive (entity Entity ) bool {
return w .storage .entityPool .Alive (entity )
}
func (w *World ) RemoveEntity (entity Entity ) {
w .checkLocked ()
w .storage .RemoveEntity (entity )
}
func (w *World ) RemoveEntities (batch Batch , fn func (entity Entity )) {
w .checkLocked ()
hasEntityObs := w .storage .observers .HasObservers (OnRemoveEntity )
hasRelationObs := w .storage .observers .HasObservers (OnRemoveRelations )
shouldLock := hasEntityObs || hasRelationObs || fn != nil
var lock uint8
if shouldLock {
lock = w .lock ()
}
tables := w .storage .getBatchTables (&batch )
if fn != nil {
for _ , tableID := range tables {
table := &w .storage .tables [tableID ]
len := uintptr (table .Len ())
var i uintptr
for i = range len {
fn (table .GetEntity (i ))
}
}
}
if hasEntityObs || hasRelationObs {
if hasEntityObs {
for _ , tableID := range tables {
table := &w .storage .tables [tableID ]
mask := &w .storage .archetypes [table .archetype ].mask
w .storage .observers .FireRemoveEntityBatch (table , mask )
}
}
if hasRelationObs {
for _ , tableID := range tables {
table := &w .storage .tables [tableID ]
if !table .HasRelations () {
continue
}
mask := &w .storage .archetypes [table .archetype ].mask
w .storage .observers .FireRemoveEntityRelBatch (table , mask )
}
}
}
cleanup := w .storage .slices .entitiesCleanup
for _ , tableID := range tables {
table := &w .storage .tables [tableID ]
len := uintptr (table .Len ())
var i uintptr
for i = range len {
entity := table .GetEntity (i )
if w .storage .isTarget [entity .id ] {
cleanup = append (cleanup , entity )
}
w .storage .entities [entity .id ].table = maxTableID
w .storage .entityPool .Recycle (entity )
}
table .Reset ()
}
w .storage .slices .tables = tables [:0 ]
for _ , entity := range cleanup {
w .storage .cleanupArchetypes (entity )
w .storage .isTarget [entity .id ] = false
}
w .storage .slices .entitiesCleanup = cleanup [:0 ]
if shouldLock {
w .unlock (lock )
}
}
func (w *World ) IsLocked () bool {
return w .storage .locks .IsLocked ()
}
func (w *World ) Resources () *Resources {
return &w .resources
}
func (w *World ) Unsafe () Unsafe {
return Unsafe {
world : w ,
}
}
func (w *World ) Event (tp EventType ) Event {
if tp > customEvent {
panic ("only custom events can be emitted manually" )
}
return Event {
world : w ,
eventType : tp ,
}
}
func (w *World ) Reset () {
w .checkLocked ()
w .storage .Reset ()
w .resources .reset ()
}
func (w *World ) Stats () *stats .World {
w .stats .Entities = stats .Entities {
Used : w .storage .entityPool .Len (),
Total : w .storage .entityPool .Cap (),
Recycled : w .storage .entityPool .Available (),
Capacity : w .storage .entityPool .TotalCap (),
}
prevCount := len (w .stats .ComponentTypes )
compCount := len (w .storage .registry .Components )
if compCount != prevCount {
types := append ([]reflect .Type {}, w .storage .registry .Types [:compCount ]...)
typeNames := make ([]string , len (types ))
for i , t := range types {
typeNames [i ] = t .Name ()
}
w .stats .ComponentTypes = types
w .stats .ComponentTypeNames = typeNames
}
memory := cap (w .storage .entities )*int (entityIndexSize ) + w .storage .entityPool .TotalCap ()*int (entitySize )
memoryUsed := w .storage .entityPool .Len () * int (entityIndexSize +entitySize )
cntOld := int32 (len (w .stats .Archetypes ))
cntNew := int32 (len (w .storage .archetypes ))
var i int32
for i = range cntOld {
arch := &w .storage .archetypes [i ]
archStats := &w .stats .Archetypes [i ]
arch .UpdateStats (archStats , &w .storage )
memory += archStats .Memory
memoryUsed += archStats .MemoryUsed
}
for i = cntOld ; i < cntNew ; i ++ {
arch := &w .storage .archetypes [i ]
w .stats .Archetypes = append (w .stats .Archetypes , arch .Stats (&w .storage ))
archStats := &w .stats .Archetypes [i ]
memory += archStats .Memory
memoryUsed += archStats .MemoryUsed
}
w .stats .Locked = w .IsLocked ()
w .stats .Memory = memory
w .stats .MemoryUsed = memoryUsed
w .stats .CachedFilters = len (w .storage .cache .filters )
w .stats .Observers = int (w .storage .observers .totalCount )
return w .stats
}
func (w *World ) Shrink (stopAfter ...time .Duration ) bool {
if len (stopAfter ) > 1 {
panic ("no more than one time limit stopAfter can be given" )
}
limit := time .Hour
if len (stopAfter ) > 0 {
limit = stopAfter [0 ]
}
return w .storage .Shrink (limit )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .