Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
PoolsGenerator.cs
1 using System.Linq;
2 
3 namespace Entitas.CodeGenerator {
4 
6 
7  const string CLASS_TEMPLATE = @"namespace Entitas {{
8 
9  public partial class Pools {{
10 {0}
11  public Pool[] allPools {{ get {{ return new [] {{ {1} }}; }} }}
12 
13 {2}
14 
15  public void SetAllPools() {{
16 {3}
17  }}
18  }}
19 }}
20 ";
21 
22  const string CREATE_POOL_TEMPLATE = @"
23  public static Pool Create{1}Pool() {{
24  return CreatePool(""{0}"", {2}.TotalComponents, {2}.componentNames, {2}.componentTypes);
25  }}
26 ";
27 
28  public CodeGenFile[] Generate(string[] poolNames) {
29  var createPoolMethods = poolNames.Aggregate(string.Empty, (acc, poolName) =>
30  acc + string.Format(CREATE_POOL_TEMPLATE, poolName, poolName.PoolPrefix(), poolName.PoolPrefix() + CodeGenerator.DEFAULT_COMPONENT_LOOKUP_TAG)
31  );
32 
33  var allPoolsList = string.Join(", ", poolNames.Select(poolName => poolName.LowercaseFirst()).ToArray());
34  var poolFields = string.Join("\n", poolNames.Select(poolName =>
35  " public Pool " + poolName.LowercaseFirst() + ";").ToArray());
36 
37  var setAllPools = string.Join("\n", poolNames.Select(poolName =>
38  " " + poolName.LowercaseFirst() + " = Create" + poolName.PoolPrefix() + "Pool();").ToArray());
39 
40  return new [] { new CodeGenFile(
41  "Pools",
42  string.Format(CLASS_TEMPLATE, createPoolMethods, allPoolsList, poolFields, setAllPools),
43  GetType().FullName
44  )};
45  }
46  }
47 }