Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
ReactiveSystem.cs
1 using System.Collections.Generic;
2 
3 namespace Entitas {
4 
5  /// A ReactiveSystem calls Execute(entities) if there were changes based on
6  /// the specified Collector and will only pass in changed entities.
7  /// A common use-case is to react to changes, e.g. a change of the position
8  /// of an entity to update the gameObject.transform.position
9  /// of the related gameObject.
10  public abstract class ReactiveSystem<TEntity> : IReactiveSystem where TEntity : class, IEntity, new() {
11 
12  readonly Collector<TEntity> _collector;
13  readonly List<TEntity> _buffer;
14  string _toStringCache;
15 
16  protected ReactiveSystem(IContext<TEntity> context) {
17  _collector = GetTrigger(context);
18  _buffer = new List<TEntity>();
19  }
20 
21  protected ReactiveSystem(Collector<TEntity> collector) {
22  _collector = collector;
23  _buffer = new List<TEntity>();
24  }
25 
26  /// Specify the collector that will trigger the ReactiveSystem.
27  protected abstract Collector<TEntity> GetTrigger(IContext<TEntity> context);
28 
29  /// This will exclude all entities which don't pass the filter.
30  protected abstract bool Filter(TEntity entity);
31 
32  protected abstract void Execute(List<TEntity> entities);
33 
34  /// Activates the ReactiveSystem and starts observing changes
35  /// based on the specified Collector.
36  /// ReactiveSystem are activated by default.
37  public void Activate() {
38  _collector.Activate();
39  }
40 
41  /// Deactivates the ReactiveSystem.
42  /// No changes will be tracked while deactivated.
43  /// This will also clear the ReactiveSystem.
44  /// ReactiveSystem are activated by default.
45  public void Deactivate() {
46  _collector.Deactivate();
47  }
48 
49  /// Clears all accumulated changes.
50  public void Clear() {
51  _collector.ClearCollectedEntities();
52  }
53 
54  /// Will call Execute(entities) with changed entities
55  /// if there are any. Otherwise it will not call Execute(entities).
56  public void Execute() {
57  if(_collector.collectedEntities.Count != 0) {
58  foreach(var e in _collector.collectedEntities) {
59  if(Filter(e)) {
60  e.Retain(this);
61  _buffer.Add(e);
62  }
63  }
64 
65  _collector.ClearCollectedEntities();
66 
67  if(_buffer.Count != 0) {
68  Execute(_buffer);
69  for(int i = 0; i < _buffer.Count; i++) {
70  _buffer[i].Release(this);
71  }
72  _buffer.Clear();
73  }
74  }
75  }
76 
77  public override string ToString() {
78  if(_toStringCache == null) {
79  _toStringCache = "ReactiveSystem(" + GetType().Name + ")";
80  }
81 
82  return _toStringCache;
83  }
84 
85  ~ReactiveSystem() {
86  Deactivate();
87  }
88  }
89 }
abstract Collector< TEntity > GetTrigger(IContext< TEntity > context)
Specify the collector that will trigger the ReactiveSystem.
void Clear()
Clears all accumulated changes.
abstract bool Filter(TEntity entity)
This will exclude all entities which don&#39;t pass the filter.