2 using System.Collections.Generic;
41 public bool isEnabled {
get {
return _isEnabled; } }
50 public Stack<IComponent>[]
componentPools {
get {
return _componentPools; } }
62 Stack<IComponent>[] _componentPools;
66 int[] _componentIndicesCache;
67 string _toStringCache;
68 StringBuilder _toStringBuilder;
71 Reactivate(creationIndex);
77 _contextInfo =
contextInfo ?? createDefaultContextInfo();
82 for(
int i = 0; i < componentNames.Length; i++) {
83 componentNames[i] = i.ToString();
86 return new ContextInfo(
"No Context", componentNames, null);
89 public void Reactivate(
int creationIndex) {
102 "Cannot add component '" +
103 _contextInfo.componentNames[index] +
"' to " +
this +
"!" 109 index,
"Cannot add component '" +
110 _contextInfo.componentNames[index] +
"' to " +
this +
"!",
111 "You should check if an entity already has the component " +
112 "before adding it or use entity.ReplaceComponent()." 116 _components[index] = component;
117 _componentsCache = null;
118 _componentIndicesCache = null;
119 _toStringCache = null;
120 if(OnComponentAdded != null) {
132 "Cannot remove component '" +
133 _contextInfo.componentNames[index] +
"' from " +
this +
"!" 139 index,
"Cannot remove component '" +
140 _contextInfo.componentNames[index] +
"' from " +
this +
"!",
141 "You should check if an entity has the component " +
142 "before removing it." 146 replaceComponent(index, null);
156 "Cannot replace component '" +
157 _contextInfo.componentNames[index] +
"' on " +
this +
"!" 162 replaceComponent(index, component);
163 }
else if(component != null) {
168 void replaceComponent(
int index,
IComponent replacement) {
169 _toStringCache = null;
170 var previousComponent = _components[index];
171 if(replacement != previousComponent) {
172 _components[index] = replacement;
173 _componentsCache = null;
174 if(replacement != null) {
175 if(OnComponentReplaced != null) {
177 this, index, previousComponent, replacement
181 _componentIndicesCache = null;
182 if(OnComponentRemoved != null) {
190 if(OnComponentReplaced != null) {
192 this, index, previousComponent, replacement
205 index,
"Cannot get component '" +
206 _contextInfo.componentNames[index] +
"' from " +
this +
"!",
207 "You should check if an entity has the component " +
212 return _components[index];
217 if(_componentsCache == null) {
220 for(
int i = 0; i < _components.Length; i++) {
221 var component = _components[i];
222 if(component != null) {
223 components.Add(component);
227 _componentsCache = components.ToArray();
232 return _componentsCache;
237 if(_componentIndicesCache == null) {
240 for(
int i = 0; i < _components.Length; i++) {
241 if(_components[i] != null) {
246 _componentIndicesCache = indices.ToArray();
251 return _componentIndicesCache;
257 return _components[index] != null;
263 for(
int i = 0; i < indices.Length; i++) {
264 if(_components[indices[i]] == null) {
275 for(
int i = 0; i < indices.Length; i++) {
276 if(_components[indices[i]] != null) {
286 _toStringCache = null;
287 for(
int i = 0; i < _components.Length; i++) {
288 if(_components[i] != null) {
289 replaceComponent(i, null);
301 var componentPool = _componentPools[index];
302 if(componentPool == null) {
303 componentPool =
new Stack<IComponent>();
304 _componentPools[index] = componentPool;
307 return componentPool;
314 return componentPool.Count > 0
315 ? componentPool.Pop()
323 return componentPool.Count > 0 ? (T)componentPool.Pop() :
new T();
330 #if !ENTITAS_FAST_AND_UNSAFE 332 public HashSet<object>
owners {
get {
return _owners; } }
333 readonly HashSet<object> _owners =
new HashSet<object>();
344 #if !ENTITAS_FAST_AND_UNSAFE 350 _toStringCache = null;
361 _toStringCache = null;
363 #if !ENTITAS_FAST_AND_UNSAFE 364 if(!
owners.Remove(owner)) {
369 if(_retainCount == 0) {
370 if(OnEntityReleased != null) {
378 public void Destroy() {
381 OnComponentAdded = null;
382 OnComponentReplaced = null;
383 OnComponentRemoved = null;
387 public void RemoveAllOnEntityReleasedHandlers() {
388 OnEntityReleased = null;
395 if(_toStringCache == null) {
396 if(_toStringBuilder == null) {
397 _toStringBuilder =
new StringBuilder();
399 _toStringBuilder.Length = 0;
402 .Append(_creationIndex)
408 const string separator =
", ";
410 var lastSeparator = components.Length - 1;
411 for(
int i = 0; i < components.Length; i++) {
412 var component = components[i];
413 var type = component.GetType();
414 var implementsToString = type.GetMethod(
"ToString")
415 .DeclaringType.ImplementsInterface<
IComponent>();
416 _toStringBuilder.Append(
418 ? component.ToString()
419 : type.ToCompilableString().RemoveComponentSuffix()
422 if(i < lastSeparator) {
423 _toStringBuilder.Append(separator);
427 _toStringBuilder.Append(
")");
428 _toStringCache = _toStringBuilder.ToString();
431 return _toStringCache;
int retainCount
Returns the number of objects that retain this entity.
EntityComponentReplaced OnComponentReplaced
void AddComponent(int index, IComponent component)
bool HasComponents(int[] indices)
EntityComponentChanged OnComponentAdded
EntityComponentChanged OnComponentRemoved
int totalComponents
The total amount of components an entity can possibly have.
EntityReleased OnEntityReleased
bool HasAnyComponent(int[] indices)
IComponent CreateComponent(int index, Type type)
override string ToString()
IComponent GetComponent(int index)
IComponent [] GetComponents()
Returns all added components.
void Retain(object owner)
int [] GetComponentIndices()
Returns all indices of added components.
void ReplaceComponent(int index, IComponent component)
bool HasComponent(int index)
HashSet< object > owners
Returns all the objects that retain this entity.
Stack< IComponent > GetComponentPool(int index)
void Release(object owner)
void RemoveAllComponents()
Removes all components.
void RemoveComponent(int index)
Stack< IComponent > [] componentPools
T CreateComponent< T >(int index)