Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
PoolExtension.cs
1 using System;
2 
3 namespace Entitas {
4 
5  /// Implement this interface if you want to create a system which needs a
6  /// reference to a pool. Recommended way to create systems in general:
7  /// pool.CreateSystem(new MySystem());
8  /// Calling pool.CreateSystem(new MySystem()) will automatically inject
9  /// the pool if ISetPool is implemented.
10  /// It's recommended to pass in the pool as a dependency using ISetPool
11  /// rather than using Pools.sharedInstance.pool directly within the system
12  /// to avoid tight coupling.
13  public interface ISetPool {
14  void SetPool(Pool pool);
15  }
16 
17  /// Implement this interface if you want to create a system which needs a
18  /// reference to pools. Recommended way to create systems in general:
19  /// pool.CreateSystem(new MySystem());
20  /// Calling pool.CreateSystem(new MySystem()) will automatically inject
21  /// the pools if ISetPools is implemented.
22  /// It's recommended to pass in the pools as a dependency using ISetPools
23  /// rather than using Pools.sharedInstance directly within the system
24  /// to avoid tight coupling.
25  public interface ISetPools {
26  void SetPools(Pools pools);
27  }
28 
29  public static class PoolExtension {
30 
31  /// Returns all entities matching the specified matcher.
32  public static Entity[] GetEntities(this Pool pool, IMatcher matcher) {
33  return pool.GetGroup(matcher).GetEntities();
34  }
35 
36  /// This is the recommended way to create systems.
37  /// It will inject the pool if ISetPool is implemented.
38  /// It will inject the Pools.sharedInstance if ISetPools is implemented.
39  /// It will automatically create a ReactiveSystem if it is a
40  /// IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
41  public static ISystem CreateSystem(this Pool pool, ISystem system) {
42  return CreateSystem(pool, system, Pools.sharedInstance);
43  }
44 
45  /// This is the recommended way to create systems.
46  /// It will inject the pool if ISetPool is implemented.
47  /// It will inject the pools if ISetPools is implemented.
48  public static ISystem CreateSystem(this Pool pool,
49  ISystem system,
50  Pools pools) {
51  SetPool(system, pool);
52  SetPools(system, pools);
53  return system;
54  }
55 
56  /// This is the recommended way to create systems.
57  /// It will inject the pool if ISetPool is implemented.
58  /// It will inject the pools if ISetPools is implemented.
59  /// It will automatically create a ReactiveSystem if it is a
60  /// IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
61  public static ISystem CreateSystem(this Pool pool,
62  IReactiveExecuteSystem system) {
63  return CreateSystem(pool, system, Pools.sharedInstance);
64  }
65 
66  /// This is the recommended way to create systems.
67  /// It will inject the pool if ISetPool is implemented.
68  /// It will inject the pools if ISetPools is implemented.
69  /// It will automatically create a ReactiveSystem if it is a
70  /// IReactiveSystem, IMultiReactiveSystem or IEntityCollectorSystem.
71  public static ISystem CreateSystem(this Pool pool,
73  Pools pools) {
74  SetPool(system, pool);
75  SetPools(system, pools);
76 
77  var reactiveSystem = system as IReactiveSystem;
78  if(reactiveSystem != null) {
79  return new ReactiveSystem(pool, reactiveSystem);
80  }
81  var multiReactiveSystem = system as IMultiReactiveSystem;
82  if(multiReactiveSystem != null) {
83  return new ReactiveSystem(pool, multiReactiveSystem);
84  }
85  var entityCollectorSystem = system as IEntityCollectorSystem;
86  if(entityCollectorSystem != null) {
87  return new ReactiveSystem(entityCollectorSystem);
88  }
89 
90  throw new EntitasException(
91  "Could not create ReactiveSystem for " + system + "!",
92  "The system has to implement IReactiveSystem, " +
93  "IMultiReactiveSystem or IEntityCollectorSystem."
94  );
95  }
96 
97  /// This is the recommended way to create systems.
98  /// It will inject the pools if ISetPools is implemented.
99  public static ISystem CreateSystem(this Pools pools, ISystem system) {
100  SetPools(system, pools);
101  return system;
102  }
103 
104  /// This is the recommended way to create systems.
105  /// It will inject the pools if ISetPools is implemented.
106  /// It will automatically create a ReactiveSystem
107  /// if it is a IEntityCollectorSystem.
108  public static ISystem CreateSystem(this Pools pools,
109  IReactiveExecuteSystem system) {
110  SetPools(system, pools);
111 
112  var entityCollectorSystem = system as IEntityCollectorSystem;
113  if(entityCollectorSystem != null) {
114  return new ReactiveSystem(entityCollectorSystem);
115  }
116 
117  throw new EntitasException(
118  "Could not create ReactiveSystem for " + system + "!",
119  "Only IEntityCollectorSystem is supported for " +
120  "pools.CreateSystem(system)."
121  );
122  }
123 
124  [Obsolete("pools.CreateSystem(system) can not infer which pool to set for ISetPool!", true)]
125  public static ISystem CreateSystem(this Pools pools, ISetPool system) {
126  throw new EntitasException(
127  "pools.CreateSystem(" + system + ") can not infer which pool " +
128  "to set for ISetPool!",
129  "pools.CreateSystem(system) only supports IInitializeSystem, " +
130  "IExecuteSystem, ICleanupSystem, ITearDownSystem and " +
131  "IEntityCollectorSystem."
132  );
133  }
134 
135  [Obsolete("pools.CreateSystem(system) can not infer which pool to use to create a ReactiveSystem!", true)]
136  public static ISystem CreateSystem(this Pools pools, IReactiveSystem system) {
137  throw new EntitasException(
138  "pools.CreateSystem(" + system + ") can not infer which pool " +
139  "to use to create a ReactiveSystem!",
140  "pools.CreateSystem(system) only supports IInitializeSystem, " +
141  "IExecuteSystem, ICleanupSystem, ITearDownSystem and " +
142  "IEntityCollectorSystem."
143  );
144  }
145 
146  [Obsolete("pools.CreateSystem(system) can not infer which pool to use to create a ReactiveSystem!", true)]
147  public static ISystem CreateSystem(this Pools pools, IMultiReactiveSystem system) {
148  throw new EntitasException(
149  "pools.CreateSystem(" + system + ") can not infer which pool " +
150  "to use to create a ReactiveSystem!",
151  "pools.CreateSystem(system) only supports IInitializeSystem, " +
152  "IExecuteSystem, ICleanupSystem, ITearDownSystem and " +
153  "IEntityCollectorSystem."
154  );
155  }
156 
157  /// This will set the pool if ISetPool is implemented.
158  public static void SetPool(ISystem system, Pool pool) {
159  var poolSystem = system as ISetPool;
160  if(poolSystem != null) {
161  poolSystem.SetPool(pool);
162  }
163  }
164 
165  /// This will set the pools if ISetPools is implemented.
166  public static void SetPools(ISystem system, Pools pools) {
167  var poolsSystem = system as ISetPools;
168  if(poolsSystem != null) {
169  poolsSystem.SetPools(pools);
170  }
171  }
172 
173  /// Creates an EntityCollector which observes all specified pools.
174  /// This is useful when you want to create an EntityCollector
175  /// for multiple pools which can be used with IEntityCollectorSystem.
177  this Pool[] pools,
178  IMatcher matcher,
179  GroupEventType eventType = GroupEventType.OnEntityAdded) {
180  var groups = new Group[pools.Length];
181  var eventTypes = new GroupEventType[pools.Length];
182 
183  for (int i = 0; i < pools.Length; i++) {
184  groups[i] = pools[i].GetGroup(matcher);
185  eventTypes[i] = eventType;
186  }
187 
188  return new EntityCollector(groups, eventTypes);
189  }
190 
191  /// Creates a new entity and adds copies of all
192  /// specified components to it.
193  /// If replaceExisting is true it will replace exisintg components.
194  public static Entity CloneEntity(this Pool pool,
195  Entity entity,
196  bool replaceExisting = false,
197  params int[] indices) {
198  var target = pool.CreateEntity();
199  entity.CopyTo(target, replaceExisting, indices);
200  return target;
201  }
202  }
203 }
static ISystem CreateSystem(this Pool pool, ISystem system)
static ISystem CreateSystem(this Pools pools, IReactiveExecuteSystem system)
static ISystem CreateSystem(this Pool pool, ISystem system, Pools pools)
virtual Group GetGroup(IMatcher matcher)
Definition: Pool.cs:230
static ISystem CreateSystem(this Pools pools, ISystem system)
virtual Entity CreateEntity()
Definition: Pool.cs:141
static void SetPool(ISystem system, Pool pool)
This will set the pool if ISetPool is implemented.
static Entity [] GetEntities(this Pool pool, IMatcher matcher)
Returns all entities matching the specified matcher.
static Entity CloneEntity(this Pool pool, Entity entity, bool replaceExisting=false, params int[] indices)
Entity [] GetEntities()
Returns all entities which are currently in this group.
Definition: Group.cs:165
static ISystem CreateSystem(this Pool pool, IReactiveExecuteSystem system)
static EntityCollector CreateEntityCollector(this Pool[] pools, IMatcher matcher, GroupEventType eventType=GroupEventType.OnEntityAdded)
Base exception used by Entitas.
static void SetPools(ISystem system, Pools pools)
This will set the pools if ISetPools is implemented.
static ISystem CreateSystem(this Pool pool, IReactiveExecuteSystem system, Pools pools)