1 using System.Collections.Generic;
6 public enum GroupEventType : byte {
29 public delegate
void GroupChanged(
33 public delegate
void GroupUpdated(
39 public int count {
get {
return _entities.Count; } }
46 readonly HashSet<Entity> _entities =
new HashSet<Entity>(
52 string _toStringCache;
62 if(_matcher.Matches(entity)) {
63 addEntitySilently(entity);
65 removeEntitySilently(entity);
72 if(_matcher.Matches(entity)) {
73 addEntity(entity, index, component);
75 removeEntity(entity, index, component);
85 if(_entities.Contains(entity)) {
86 if(OnEntityRemoved != null) {
89 if(OnEntityAdded != null) {
92 if(OnEntityUpdated != null) {
94 this, entity, index, previousComponent, newComponent
104 OnEntityAdded = null;
105 OnEntityRemoved = null;
106 OnEntityUpdated = null;
109 internal GroupChanged handleEntity(
Entity entity) {
110 return _matcher.Matches(entity)
111 ? (addEntitySilently(entity) ? OnEntityAdded : null)
112 : (removeEntitySilently(entity) ? OnEntityRemoved : null);
115 bool addEntitySilently(
Entity entity) {
117 var added = _entities.Add(entity);
119 _entitiesCache = null;
120 _singleEntityCache = null;
131 if(addEntitySilently(entity) && OnEntityAdded != null) {
136 bool removeEntitySilently(
Entity entity) {
137 var removed = _entities.Remove(entity);
139 _entitiesCache = null;
140 _singleEntityCache = null;
148 var removed = _entities.Remove(entity);
150 _entitiesCache = null;
151 _singleEntityCache = null;
152 if(OnEntityRemoved != null) {
161 return _entities.Contains(entity);
166 if(_entitiesCache == null) {
167 _entitiesCache =
new Entity[_entities.Count];
168 _entities.CopyTo(_entitiesCache);
171 return _entitiesCache;
178 if(_singleEntityCache == null) {
179 var c = _entities.Count;
181 using (var enumerator = _entities.GetEnumerator()) {
182 enumerator.MoveNext();
183 _singleEntityCache = enumerator.Current;
192 return _singleEntityCache;
195 public override string ToString() {
196 if(_toStringCache == null) {
197 _toStringCache =
"Group(" + _matcher +
")";
199 return _toStringCache;
206 "Cannot get the single entity from " + group +
207 "!\nGroup contains " + group.
count +
" entities:",
209 group.
GetEntities().Select(e => e.ToString()).ToArray()
void RemoveAllEventHandlers()
IMatcher matcher
Returns the matcher which was used to create this group.
void HandleEntity(Entity entity, int index, IComponent component)
This is used by the pool to manage the group.
void UpdateEntity(Entity entity, int index, IComponent previousComponent, IComponent newComponent)
This is used by the pool to manage the group.
void Release(object owner)
GroupChanged OnEntityRemoved
Occurs when an entity gets removed.
Entity [] GetEntities()
Returns all entities which are currently in this group.
GroupUpdated OnEntityUpdated
Occurs when a component of an entity in the group gets replaced.
void HandleEntitySilently(Entity entity)
This is used by the pool to manage the group.
int count
Returns the number of entities in the group.
GroupChanged OnEntityAdded
Occurs when an entity gets added.
Base exception used by Entitas.
Entity Retain(object owner)
bool ContainsEntity(Entity entity)
Determines whether this group has the specified entity.