2021-11-19 04:13:28 +00:00
|
|
|
package gohan
|
|
|
|
|
2021-11-22 02:48:54 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-11-22 19:18:28 +00:00
|
|
|
// Entity is an entity identifier.
|
|
|
|
type Entity int
|
2021-11-19 04:13:28 +00:00
|
|
|
|
2021-11-22 19:18:28 +00:00
|
|
|
// NewEntity returns a new (or previously removed and cleared) Entity. Because
|
|
|
|
// Gohan reuses removed Entity IDs, a previously removed ID may be returned.
|
2022-02-01 04:07:55 +00:00
|
|
|
func NewEntity() Entity {
|
2021-12-10 03:39:21 +00:00
|
|
|
w.entityMutex.Lock()
|
|
|
|
defer w.entityMutex.Unlock()
|
2021-11-22 02:48:54 +00:00
|
|
|
|
2021-12-08 03:20:36 +00:00
|
|
|
if len(w.availableEntities) > 0 {
|
|
|
|
id := w.availableEntities[0]
|
|
|
|
w.availableEntities = w.availableEntities[1:]
|
2021-12-06 01:17:15 +00:00
|
|
|
w.allEntities = append(w.allEntities, id)
|
2021-11-22 19:18:28 +00:00
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
w.maxEntityID++
|
|
|
|
w.allEntities = append(w.allEntities, w.maxEntityID)
|
|
|
|
w.components = append(w.components, make([]interface{}, w.maxComponentID+1))
|
2021-12-10 03:39:21 +00:00
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
return w.maxEntityID
|
2021-11-22 02:48:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 04:07:55 +00:00
|
|
|
// Remove removes the provided Entity's components, causing it to no
|
2021-12-06 01:17:15 +00:00
|
|
|
// longer be handled by any system. Because Gohan reuses removed EntityIDs,
|
|
|
|
// applications must also remove any internal references to the removed Entity.
|
2022-06-11 00:44:51 +00:00
|
|
|
func (e Entity) Remove() bool {
|
2021-12-10 03:39:21 +00:00
|
|
|
w.entityMutex.Lock()
|
|
|
|
defer w.entityMutex.Unlock()
|
2021-11-22 02:48:54 +00:00
|
|
|
|
2022-06-11 00:44:51 +00:00
|
|
|
for i, ent := range w.allEntities {
|
|
|
|
if ent == e {
|
2021-12-10 04:16:12 +00:00
|
|
|
w.allEntities = _removeAt(w.allEntities, i)
|
2021-12-08 03:20:36 +00:00
|
|
|
|
|
|
|
// Remove components.
|
2022-06-11 00:44:51 +00:00
|
|
|
for i := range w.components[e] {
|
|
|
|
w.components[e][i] = nil
|
2021-12-08 03:20:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 00:44:51 +00:00
|
|
|
w.removedEntities = append(w.removedEntities, e)
|
2021-12-08 03:20:36 +00:00
|
|
|
return true
|
2021-11-22 02:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 03:20:36 +00:00
|
|
|
return false
|
2021-11-22 02:48:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 04:07:55 +00:00
|
|
|
// AllEntities returns a slice of all active entities. To retrieve only the
|
|
|
|
// number of currently active entities, use CurrentEntities.
|
|
|
|
func AllEntities() []Entity {
|
2021-12-29 20:59:07 +00:00
|
|
|
w.entityMutex.Lock()
|
|
|
|
defer w.entityMutex.Unlock()
|
|
|
|
allEntities := make([]Entity, len(w.allEntities))
|
|
|
|
copy(allEntities, w.allEntities)
|
|
|
|
return allEntities
|
|
|
|
}
|
|
|
|
|
2021-11-22 02:48:54 +00:00
|
|
|
var numEntities int
|
|
|
|
var numEntitiesT time.Time
|
|
|
|
|
2021-12-09 03:47:34 +00:00
|
|
|
// CurrentEntities returns the number of currently active entities.
|
2022-02-01 04:07:55 +00:00
|
|
|
func CurrentEntities() int {
|
2021-12-06 01:17:15 +00:00
|
|
|
if time.Since(numEntitiesT) >= w.cacheTime {
|
|
|
|
numEntities = len(w.allEntities)
|
2021-11-22 02:48:54 +00:00
|
|
|
numEntitiesT = time.Now()
|
|
|
|
}
|
|
|
|
return numEntities
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
2021-12-10 04:16:12 +00:00
|
|
|
|
|
|
|
func _removeAt(v []Entity, i int) []Entity {
|
|
|
|
v[i] = v[len(v)-1]
|
|
|
|
return v[:len(v)-1]
|
|
|
|
}
|