62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package gohan
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// EntityID is an entity identifier.
|
|
type EntityID int
|
|
|
|
var nextEntityID EntityID
|
|
|
|
var entityMutex sync.Mutex
|
|
|
|
// NextEntityID returns the next available EntityID.
|
|
func NextEntityID() EntityID {
|
|
entityMutex.Lock()
|
|
defer entityMutex.Unlock()
|
|
|
|
nextEntityID++
|
|
allEntities = append(allEntities, nextEntityID)
|
|
return nextEntityID
|
|
}
|
|
|
|
// RemoveEntity removes the provided Entity, and all of its components.
|
|
func RemoveEntity(entity EntityID) {
|
|
entityMutex.Lock()
|
|
defer entityMutex.Unlock()
|
|
|
|
for i, e := range allEntities {
|
|
if e == entity {
|
|
allEntities = append(allEntities[:i], allEntities[i+1:]...)
|
|
removedEntities = append(removedEntities, e)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func wasRemoved(entity EntityID) bool {
|
|
entityMutex.Lock()
|
|
defer entityMutex.Unlock()
|
|
|
|
for _, e := range allEntities {
|
|
if e == entity {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
var numEntities int
|
|
var numEntitiesT time.Time
|
|
|
|
// ActiveEntities returns the number of currently active entities.
|
|
func ActiveEntities() int {
|
|
if time.Since(numEntitiesT) >= time.Second {
|
|
numEntities = len(allEntities)
|
|
numEntitiesT = time.Now()
|
|
}
|
|
return numEntities
|
|
}
|