2 using System.Collections.Generic;
32 public delegate
void EntityChanged(
36 public delegate
void ComponentReplaced(
41 public delegate
void EntityReleased(
Entity entity);
52 public bool isEnabled {
get {
return _isEnabled; } }
62 get {
return _componentPools; }
70 internal int _creationIndex;
71 internal bool _isEnabled =
true;
73 readonly
int _totalComponents;
75 readonly Stack<IComponent>[] _componentPools;
79 int[] _componentIndicesCache;
80 string _toStringCache;
100 for(
int i = 0; i < componentNames.Length; i++) {
101 componentNames[i] = i.ToString();
104 "No Pool", componentNames, null
117 "Cannot add component '" +
118 _poolMetaData.componentNames[index] +
126 "Cannot add component '" +
127 _poolMetaData.componentNames[index] +
128 "' to " +
this +
"!",
129 "You should check if an entity already has the component " +
130 "before adding it or use entity.ReplaceComponent()." 134 _components[index] = component;
135 _componentsCache = null;
136 _componentIndicesCache = null;
137 _toStringCache = null;
138 if(OnComponentAdded != null) {
152 "Cannot remove component '" +
153 _poolMetaData.componentNames[index] +
154 "' from " +
this +
"!" 161 "Cannot remove component '" +
162 _poolMetaData.componentNames[index] +
163 "' from " +
this +
"!",
164 "You should check if an entity has the component" +
165 "before removing it." 169 replaceComponent(index, null);
181 "Cannot replace component '" +
182 _poolMetaData.componentNames[index] +
188 replaceComponent(index, component);
189 }
else if(component != null) {
196 void replaceComponent(
int index,
IComponent replacement) {
197 var previousComponent = _components[index];
198 if(replacement != previousComponent) {
199 _components[index] = replacement;
200 _componentsCache = null;
201 if(replacement != null) {
202 if(OnComponentReplaced != null) {
204 this, index, previousComponent, replacement
208 _componentIndicesCache = null;
209 _toStringCache = null;
210 if(OnComponentRemoved != null) {
218 if(OnComponentReplaced != null) {
220 this, index, previousComponent, replacement
234 "Cannot get component '" +
235 _poolMetaData.componentNames[index] +
"' from " +
237 "You should check if an entity has the component" +
242 return _components[index];
247 if(_componentsCache == null) {
250 for(
int i = 0; i < _components.Length; i++) {
251 var component = _components[i];
252 if(component != null) {
253 components.Add(component);
257 _componentsCache = components.ToArray();
262 return _componentsCache;
267 if(_componentIndicesCache == null) {
270 for(
int i = 0; i < _components.Length; i++) {
271 if(_components[i] != null) {
276 _componentIndicesCache = indices.ToArray();
281 return _componentIndicesCache;
287 return _components[index] != null;
293 for(
int i = 0; i < indices.Length; i++) {
294 if(_components[indices[i]] == null) {
305 for(
int i = 0; i < indices.Length; i++) {
306 if(_components[indices[i]] != null) {
316 _toStringCache = null;
317 for(
int i = 0; i < _components.Length; i++) {
318 if(_components[i] != null) {
319 replaceComponent(i, null);
331 var componentPool = _componentPools[index];
332 if(componentPool == null) {
333 componentPool =
new Stack<IComponent>();
334 _componentPools[index] = componentPool;
337 return componentPool;
344 return componentPool.Count > 0
345 ? componentPool.Pop()
353 return componentPool.Count > 0 ? (T)componentPool.Pop() :
new T();
356 #if ENTITAS_FAST_AND_UNSAFE 359 public int retainCount {
get {
return _retainCount; } }
368 public readonly HashSet<object>
owners =
new HashSet<object>();
379 #if ENTITAS_FAST_AND_UNSAFE 385 if(!owners.Add(owner)) {
391 _toStringCache = null;
404 #if ENTITAS_FAST_AND_UNSAFE 407 if(_retainCount == 0) {
411 if(!owners.Remove(owner)) {
415 if(owners.Count == 0) {
419 _toStringCache = null;
421 if(OnEntityReleased != null) {
429 internal void destroy() {
432 OnComponentAdded = null;
433 OnComponentReplaced = null;
434 OnComponentRemoved = null;
438 internal void removeAllOnEntityReleasedHandlers() {
439 OnEntityReleased = null;
446 if(_toStringCache == null) {
447 var sb =
new StringBuilder()
449 .Append(_creationIndex)
455 const string separator =
", ";
457 var lastSeparator = components.Length - 1;
458 for(
int i = 0; i < components.Length; i++) {
460 components[i].GetType().Name.RemoveComponentSuffix()
462 if(i < lastSeparator) {
463 sb.Append(separator);
468 _toStringCache = sb.ToString();
471 return _toStringCache;
478 int index,
string message,
string hint
481 "\nEntity already has a component at index " 491 int index,
string message,
string hint
494 "\nEntity does not have a component at index " 505 message +
"\nEntity is not enabled!",
506 "The entity has already been destroyed. " +
507 "You cannot modify destroyed entities." 521 public int GetHashCode(
Entity obj) {
522 return obj._creationIndex;
529 Entity entity,
object owner
531 "'" + owner +
"' cannot retain " + entity +
"!\n" +
532 "Entity is already retained by this object!",
533 "The entity must be released by this object first." 542 "'" + owner +
"' cannot release " + entity +
"!\n" +
543 "Entity is not retained by this object!",
544 "An entity can only be released from objects that retain it." int retainCount
Returns the number of objects that retain this entity.
ComponentReplaced OnComponentReplaced
bool HasComponents(int[] indices)
Entity ReplaceComponent(int index, IComponent component)
EntityChanged OnComponentRemoved
readonly HashSet< object > owners
Returns all the objects that retain this entity.
int totalComponents
The total amount of components an entity can possibly have.
EntityReleased OnEntityReleased
bool HasAnyComponent(int[] indices)
Entity(int totalComponents, Stack< IComponent >[] componentPools, PoolMetaData poolMetaData=null)
EntityChanged OnComponentAdded
IComponent CreateComponent(int index, Type type)
override string ToString()
IComponent GetComponent(int index)
IComponent [] GetComponents()
Returns all added components.
int [] GetComponentIndices()
Returns all indices of added components.
bool HasComponent(int index)
PoolMetaData poolMetaData
Stack< IComponent > GetComponentPool(int index)
void Release(object owner)
void RemoveAllComponents()
Removes all components.
Entity RemoveComponent(int index)
Entity AddComponent(int index, IComponent component)
Stack< IComponent > [] componentPools
Base exception used by Entitas.
T CreateComponent< T >(int index)
Entity Retain(object owner)