forked from victorsclui/DOTSStructuralChangeTests
/
WeaponEquipSystem_HandlerUsingECB.cs
237 lines (188 loc) · 7.64 KB
/
WeaponEquipSystem_HandlerUsingECB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine.Profiling;
#if true
[BurstCompile]
[UpdateBefore(typeof(TransformSystemGroup))]
public class WeaponEquipSystem_HandlerUsingECB : EntityCommandBufferSystem
{
bool initialized = false;
public JobHandle commandQueueDependency;
public struct Command
{
public Entity weaponOwner;
public Weapon currentWeaponRef;
public int newWeaponIdx;
public float4x4 localToParent;
}
public NativeQueue<Command>.ParallelWriter commandQueueFrontEnd;
NativeQueue<Command> commandQueue;
NativeList<Entity> weaponPrefabs;
NativeList<Entity> weaponToDestroyCache;
NativeList<Entity> newWeaponInstantiatedCache;
protected override void OnCreate()
{
base.OnCreate();
commandQueue = new NativeQueue<Command>(Allocator.Persistent);
commandQueueFrontEnd = commandQueue.AsParallelWriter();
weaponPrefabs = new NativeList<Entity>(Allocator.Persistent);
weaponToDestroyCache = new NativeList<Entity>(Allocator.Persistent);
newWeaponInstantiatedCache = new NativeList<Entity>(Allocator.Persistent);
}
protected override void OnDestroy()
{
newWeaponInstantiatedCache.Dispose();
weaponToDestroyCache.Dispose();
weaponPrefabs.Dispose();
commandQueue.Dispose();
base.OnDestroy();
}
protected override void OnUpdate()
{
// Some initialization must happen on first update for singleton entity query to work
if (!initialized)
{
initialized = true;
var weaponDataEntity = GetSingletonEntity<WeaponDataEntry>();
var weaponDatabase = EntityManager.GetBuffer<WeaponDataEntry>(weaponDataEntity);
weaponPrefabs.Clear();
for (int i = 0; i < weaponDatabase.Length; ++i)
{
weaponPrefabs.Add(weaponDatabase[i].prefab);
}
}
var commandBuffer = CreateCommandBuffer();
commandQueueDependency.Complete();
// Quick way to burst compile processing of command
var job = new ProcessCommandJob
{
commandBuffer = commandBuffer,
commandQueue = commandQueue,
weaponToDestroyCache = weaponToDestroyCache,
newWeaponInstantiatedCache = newWeaponInstantiatedCache,
weaponPrefabs = weaponPrefabs
};
job.Run();
base.OnUpdate();
}
[BurstCompile]
struct ProcessCommandJob : IJob
{
public EntityCommandBuffer commandBuffer;
public NativeQueue<Command> commandQueue;
public NativeList<Entity> weaponToDestroyCache;
public NativeList<Entity> newWeaponInstantiatedCache;
public NativeList<Entity> weaponPrefabs;
public void Execute()
{
var commandQueueArray = commandQueue.ToArray(Allocator.Temp);
{
// Mass destroy
weaponToDestroyCache.Clear();
for (int i = 0; i < commandQueueArray.Length; ++i)
{
var command = commandQueueArray[i];
if (command.currentWeaponRef.Value != Entity.Null)
{
weaponToDestroyCache.Add(command.currentWeaponRef.Value);
}
}
if (weaponToDestroyCache.Length > 0)
{
// To be replaced with batched ECB destroy when API ships
{
for (int i = 0; i < weaponToDestroyCache.Length; ++i)
commandBuffer.DestroyEntity(weaponToDestroyCache[i]);
}
}
// Instantiate has to 1-1 since mass instantiate does not work with LinkedEntityGroup
newWeaponInstantiatedCache.Clear();
for (int i = 0; i < commandQueueArray.Length; ++i)
{
var command = commandQueueArray[i];
newWeaponInstantiatedCache.Add(commandBuffer.Instantiate(weaponPrefabs[command.newWeaponIdx]));
}
// Mass add component
{
// To be replaced with batched ECB add component when API ships
{
for (int i = 0; i < newWeaponInstantiatedCache.Length; ++i)
commandBuffer.AddComponent<Parent>(newWeaponInstantiatedCache[i]);
for (int i = 0; i < newWeaponInstantiatedCache.Length; ++i)
commandBuffer.AddComponent<LocalToParent>(newWeaponInstantiatedCache[i]);
}
}
// There is no mass set component but setting component is way less of a cost than add component
for (int i = 0; i < newWeaponInstantiatedCache.Length; ++i)
{
var command = commandQueueArray[i];
var newWeapon = newWeaponInstantiatedCache[i];
commandBuffer.SetComponent(newWeapon, new Parent { Value = command.weaponOwner });
commandBuffer.SetComponent(newWeapon, new LocalToParent { Value = command.localToParent });
commandBuffer.SetComponent(command.weaponOwner, new Weapon { Value = newWeapon });
}
}
commandQueueArray.Dispose();
commandQueue.Clear();
}
}
}
[UpdateBefore(typeof(WeaponEquipSystem_HandlerUsingECB))]
public class WeaponEquipSystemTest_HandlerUsingECB : SystemBase
{
int currentWeaponIdx;
EntityQuery m_WeaponOwner;
protected override void OnCreate()
{
m_WeaponOwner = GetEntityQuery(ComponentType.ReadOnly<Weapon>());
}
[BurstCompile]
struct WeaponEquipJob : IJobChunk
{
public int weaponToEquipIdx;
[ReadOnly]
public EntityTypeHandle entityTypeHandle;
[ReadOnly]
public ComponentTypeHandle<Weapon> weaponRefHandle;
public NativeQueue<WeaponEquipSystem_HandlerUsingECB.Command>.ParallelWriter commandQueue;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var entityChunk = chunk.GetNativeArray(entityTypeHandle);
var weaponRefChunk = chunk.GetNativeArray(weaponRefHandle);
for (var i = 0; i < entityChunk.Length; i++)
{
var ownerEntity = entityChunk[i];
var weaponRef = weaponRefChunk[i];
commandQueue.Enqueue(new WeaponEquipSystem_HandlerUsingECB.Command
{
weaponOwner = ownerEntity,
currentWeaponRef = weaponRef,
newWeaponIdx = weaponToEquipIdx,
localToParent = float4x4.identity
});
}
}
}
protected override void OnUpdate()
{
++currentWeaponIdx;
currentWeaponIdx = currentWeaponIdx % 4;
var entityTypeHandle = GetEntityTypeHandle();
var weaponRefHandle = GetComponentTypeHandle<Weapon>(true);
var weaponEquipSystem = World.GetExistingSystem<WeaponEquipSystem_HandlerUsingECB>();
var job = new WeaponEquipJob()
{
weaponToEquipIdx = currentWeaponIdx,
entityTypeHandle = entityTypeHandle,
weaponRefHandle = weaponRefHandle,
commandQueue = weaponEquipSystem.commandQueueFrontEnd
};
Dependency = job.ScheduleSingle(m_WeaponOwner, Dependency);
weaponEquipSystem.commandQueueDependency = Dependency;
}
}
#endif