1 using System.Collections.Generic;
23 public int count {
get {
return _entities.Count; } }
30 readonly HashSet<TEntity> _entities =
new HashSet<TEntity>(
34 TEntity[] _entitiesCache;
35 TEntity _singleEntityCache;
36 string _toStringCache;
46 if(_matcher.Matches(entity)) {
47 addEntitySilently(entity);
49 removeEntitySilently(entity);
55 if(_matcher.Matches(entity)) {
56 addEntity(entity, index, component);
58 removeEntity(entity, index, component);
64 if(_entities.Contains(entity)) {
65 if(OnEntityRemoved != null) {
68 if(OnEntityAdded != null) {
71 if(OnEntityUpdated != null) {
73 this, entity, index, previousComponent, newComponent
84 OnEntityRemoved = null;
85 OnEntityUpdated = null;
88 public GroupChanged<TEntity>
HandleEntity(TEntity entity) {
89 return _matcher.Matches(entity)
94 bool addEntitySilently(TEntity entity) {
95 if(entity.isEnabled) {
96 var added = _entities.Add(entity);
98 _entitiesCache = null;
99 _singleEntityCache = null;
109 void addEntity(TEntity entity,
int index,
IComponent component) {
110 if(addEntitySilently(entity) && OnEntityAdded != null) {
115 bool removeEntitySilently(TEntity entity) {
116 var removed = _entities.Remove(entity);
118 _entitiesCache = null;
119 _singleEntityCache = null;
120 entity.Release(
this);
126 void removeEntity(TEntity entity,
int index,
IComponent component) {
127 var removed = _entities.Remove(entity);
129 _entitiesCache = null;
130 _singleEntityCache = null;
131 if(OnEntityRemoved != null) {
134 entity.Release(
this);
140 return _entities.Contains(entity);
145 if(_entitiesCache == null) {
146 _entitiesCache =
new TEntity[_entities.Count];
147 _entities.CopyTo(_entitiesCache);
150 return _entitiesCache;
157 if(_singleEntityCache == null) {
158 var c = _entities.Count;
160 using(var enumerator = _entities.GetEnumerator()) {
161 enumerator.MoveNext();
162 _singleEntityCache = enumerator.Current;
171 return _singleEntityCache;
174 public override string ToString() {
175 if(_toStringCache == null) {
176 _toStringCache =
"Group(" + _matcher +
")";
178 return _toStringCache;
GroupChanged< TEntity > OnEntityAdded
Occurs when an entity gets added.
bool ContainsEntity(TEntity entity)
Determines whether this group has the specified entity.
void HandleEntitySilently(TEntity entity)
This is used by the context to manage the group.
IMatcher< TEntity > matcher
Returns the matcher which was used to create this group.
int count
Returns the number of entities in the group.
TEntity [] GetEntities()
Returns all entities which are currently in this group.
void RemoveAllEventHandlers()
GroupUpdated< TEntity > OnEntityUpdated
Occurs when a component of an entity in the group gets replaced.
GroupChanged< TEntity > OnEntityRemoved
Occurs when an entity gets removed.
TEntity GetSingleEntity()
Group(IMatcher< TEntity > matcher)
void HandleEntity(TEntity entity, int index, IComponent component)
This is used by the context to manage the group.
void UpdateEntity(TEntity entity, int index, IComponent previousComponent, IComponent newComponent)
This is used by the context to manage the group.