Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
EntityCollector.cs
1 using System.Collections.Generic;
2 using System.Text;
3 
4 namespace Entitas {
5 
6  /// An EntityCollector can observe one or more groups and collects
7  /// changed entities based on the specified eventType.
8  public class EntityCollector {
9 
10  /// Returns all collected entities.
11  /// Call collector.ClearCollectedEntities()
12  /// once you processed all entities.
13  public HashSet<Entity> collectedEntities {
14  get { return _collectedEntities; }
15  }
16 
17  readonly HashSet<Entity> _collectedEntities;
18  readonly Group[] _groups;
19  readonly GroupEventType[] _eventTypes;
20  Group.GroupChanged _addEntityCache;
21  string _toStringCache;
22 
23  /// Creates an EntityCollector and will collect changed entities
24  /// based on the specified eventType.
25  public EntityCollector(Group group, GroupEventType eventType)
26  : this(new [] { group }, new [] { eventType }) {
27  }
28 
29  /// Creates an EntityCollector and will collect changed entities
30  /// based on the specified eventTypes.
31  public EntityCollector(Group[] groups, GroupEventType[] eventTypes) {
32  _groups = groups;
33  _collectedEntities = new HashSet<Entity>(
34  EntityEqualityComparer.comparer
35  );
36  _eventTypes = eventTypes;
37 
38  if(groups.Length != eventTypes.Length) {
39  throw new EntityCollectorException(
40  "Unbalanced count with groups (" + groups.Length +
41  ") and event types (" + eventTypes.Length + ").",
42  "Group and event type count must be equal."
43  );
44  }
45 
46  _addEntityCache = addEntity;
47  Activate();
48  }
49 
50  /// Activates the EntityCollector and will start collecting
51  /// changed entities. EntityCollectors are activated by default.
52  public void Activate() {
53  for (int i = 0; i < _groups.Length; i++) {
54  var group = _groups[i];
55  var eventType = _eventTypes[i];
56  if(eventType == GroupEventType.OnEntityAdded) {
57  group.OnEntityAdded -= _addEntityCache;
58  group.OnEntityAdded += _addEntityCache;
59  } else if(eventType == GroupEventType.OnEntityRemoved) {
60  group.OnEntityRemoved -= _addEntityCache;
61  group.OnEntityRemoved += _addEntityCache;
62  } else if(eventType == GroupEventType.OnEntityAddedOrRemoved) {
63  group.OnEntityAdded -= _addEntityCache;
64  group.OnEntityAdded += _addEntityCache;
65  group.OnEntityRemoved -= _addEntityCache;
66  group.OnEntityRemoved += _addEntityCache;
67  }
68  }
69  }
70 
71  /// Deactivates the EntityCollector.
72  /// This will also clear all collected entities.
73  /// EntityCollectors are activated by default.
74  public void Deactivate() {
75  for (int i = 0; i < _groups.Length; i++) {
76  var group = _groups[i];
77  group.OnEntityAdded -= _addEntityCache;
78  group.OnEntityRemoved -= _addEntityCache;
79  }
81  }
82 
83  /// Clears all collected entities.
84  public void ClearCollectedEntities() {
85  foreach(var entity in _collectedEntities) {
86  entity.Release(this);
87  }
88  _collectedEntities.Clear();
89  }
90 
91  void addEntity(Group group,
92  Entity entity,
93  int index,
94  IComponent component) {
95  var added = _collectedEntities.Add(entity);
96  if(added) {
97  entity.Retain(this);
98  }
99  }
100 
101  public override string ToString() {
102  if(_toStringCache == null) {
103  var sb = new StringBuilder().Append("Collector(");
104 
105  const string separator = ", ";
106  var lastSeparator = _groups.Length - 1;
107  for (int i = 0; i < _groups.Length; i++) {
108  sb.Append(_groups[i]);
109  if(i < lastSeparator) {
110  sb.Append(separator);
111  }
112  }
113 
114  sb.Append(")");
115  _toStringCache = sb.ToString();
116  }
117 
118  return _toStringCache;
119  }
120 
121  ~EntityCollector () {
122  Deactivate();
123  }
124  }
125 
127  public EntityCollectorException(string message, string hint) :
128  base(message, hint) {
129  }
130  }
131 }
EntityCollector(Group group, GroupEventType eventType)
void ClearCollectedEntities()
Clears all collected entities.
EntityCollector(Group[] groups, GroupEventType[] eventTypes)
HashSet< Entity > collectedEntities
GroupChanged OnEntityRemoved
Occurs when an entity gets removed.
Definition: Group.cs:24
GroupChanged OnEntityAdded
Occurs when an entity gets added.
Definition: Group.cs:21
Base exception used by Entitas.
Entity Retain(object owner)
Definition: Entity.cs:377