-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormationDynamicMovementSystem.cs
More file actions
595 lines (523 loc) · 28.7 KB
/
FormationDynamicMovementSystem.cs
File metadata and controls
595 lines (523 loc) · 28.7 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Physics;
using Unity.Physics.Systems;
using Reese.Nav;
using UnityEngine.AI;
//handles all movement done on unit basis
public class FormationDynamicMovementSystem : SystemBase
{
// ECBs for adding and removing of components, and destruction of Entities
private BeginSimulationEntityCommandBufferSystem beginECB;
private EndSimulationEntityCommandBufferSystem endECB;
BuildPhysicsWorld buildPhysicsWorld => World.GetExistingSystem<BuildPhysicsWorld>();
//flocking optimization using cells
public NativeMultiHashMap<int, FlockComponent> cellVsEntityPositions;
//calculation for getting cell key for the position of the entity
public static int GetUniqueKeyForPosition(float3 position, int cellSize)
{
return (int)((15 * math.floor(position.x / cellSize)) + (17 * math.floor(position.y / cellSize)) + (19 * math.floor(position.z / cellSize)));
}
protected override void OnCreate()
{
beginECB = World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
endECB = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
cellVsEntityPositions = new NativeMultiHashMap<int, FlockComponent>(0, Allocator.Persistent);
}
protected override void OnUpdate()
{
var ecb = endECB.CreateCommandBuffer().AsParallelWriter();
//setting the cellVSEntity Hashmap for later use
EntityQuery eq = GetEntityQuery(typeof(FlockComponent));
cellVsEntityPositions.Clear();
if (eq.CalculateEntityCount() > cellVsEntityPositions.Capacity)
{
cellVsEntityPositions.Capacity = eq.CalculateEntityCount();
}
NativeMultiHashMap<int, FlockComponent>.ParallelWriter cellVsEntityPositionsParallel = cellVsEntityPositions.AsParallelWriter();
Entities
.WithNone<UnitDeathComponent, DeathStateComponent>()
.ForEach((ref FlockComponent bc, ref Translation trans) =>
{
FlockComponent bcValues = new FlockComponent();
bc.currentPosition = trans.Value;
bcValues = bc;
bcValues.currentPosition = trans.Value;
cellVsEntityPositionsParallel.Add(GetUniqueKeyForPosition(trans.Value, bc.flockManager.cellSize), bcValues);
})
.WithName("Flocking_AddUnitToCellArray")
.ScheduleParallel();
float deltaTime = Time.DeltaTime;
NativeMultiHashMap<int, FlockComponent> cellVsEntityPositionsForJob = cellVsEntityPositions;
var translations = GetComponentDataFromEntity<Translation>(true);
var disableFlockingComponents = GetComponentDataFromEntity<DisableFlockingComponent>(true);
var localToWorldComponents = GetComponentDataFromEntity<LocalToWorld>(true);
var formationComponents = GetComponentDataFromEntity<FormationComponent>(true);
var unitEngageComponents = GetComponentDataFromEntity<UnitEngageComponent>(true);
var meleeFormationComponents = GetComponentDataFromEntity<MeleeFormationComponent>(true);
//Setting the new wanted formation position in the world
Entities
.WithReadOnly(translations)
.WithReadOnly(localToWorldComponents)
.WithReadOnly(formationComponents)
.WithNone<UnitDeathComponent>()
.ForEach((Entity entity, ref FlockComponent bc, ref UnitComponent unitComponent, in Translation trans, in UnitFormationPosComponent unitFormationPosComponent) =>
{
unitComponent.formationPos = unitFormationPosComponent.formationPos;
float3 formationLeaderPosition = translations[bc.formation].Value;
//This part will calculate the wanted formation position, including formation rotating
LocalToWorld formationLocalToWorld = localToWorldComponents[bc.formation];
//make a new 4x4 with that rotation and then add it to the localtoworld one
//or math has a function for it
var rotationMatrix = math.float4x4(formationComponents[bc.formation].formationRotation, float3.zero);
var matrix = math.mul(formationLocalToWorld.Value, rotationMatrix);
float4 targetPosition4 = math.mul(matrix, new float4(unitFormationPosComponent.formationPos.x, trans.Value.y, unitFormationPosComponent.formationPos.y, 1));
float3 formationPosition = new float3(targetPosition4.x, bc.yHeight, targetPosition4.z);
/////////////////////////////////
///
bc.targetFormationPosition = formationPosition;
})
.WithName("Flocking_FormationPositionUpdate")
.ScheduleParallel();
var unitDisableFlockComponents = GetComponentDataFromEntity<UnitDisableFlocking>(true);
//check if flocking should run
Entities
.WithReadOnly(disableFlockingComponents)
.WithReadOnly(unitEngageComponents)
.WithReadOnly(meleeFormationComponents)
.WithReadOnly(unitDisableFlockComponents)
.WithNone<UnitDeathComponent>()
.ForEach((Entity entity, int entityInQueryIndex, ref FlockComponent bc, in Translation trans) =>
{
//bool old = bc.isActive;
if (bc.stopAnimation)
{
bc.stopAnimationTimer -= deltaTime;
if (/*!bc.blockingUnit &&*/ bc.stopAnimationTimer <= 0.0f)
{
bc.stopAnimation = false;
bc.stopAnimationTimer = 0.3f;
}
}
bc.shouldFlock = !(bc.blockingUnit || bc.blockingBuilding);
bool disableFlocking = !disableFlockingComponents.HasComponent(bc.formation);
if (unitEngageComponents.HasComponent(entity) && meleeFormationComponents.HasComponent(bc.formation))
{
bc.isActive = disableFlocking && bc.shouldFlock;
}
else if (bc.inFormation)
{
bc.isActive = disableFlocking && bc.shouldFlock && math.distance(bc.targetFormationPosition, trans.Value) > bc.flockManager.acceptanceDistance;
}
else
{
bc.isActive = disableFlocking && bc.shouldFlock && math.distance(bc.targetPos, trans.Value) > bc.flockManager.acceptanceDistance * 0.8f;
}
if (!bc.isActive && !unitDisableFlockComponents.HasComponent(entity))
{
ecb.AddComponent<UnitDisableFlocking>(entityInQueryIndex, entity);
bc.velocity = float3.zero;
}
else if (bc.isActive && unitDisableFlockComponents.HasComponent(entity))
{
ecb.RemoveComponent<UnitDisableFlocking>(entityInQueryIndex, entity);
}
})
.WithName("Flocking_ActiveCheck")
.ScheduleParallel();
//setting unit height to terrai
var physicsWorld = buildPhysicsWorld.PhysicsWorld;
Dependency = JobHandle.CombineDependencies(Dependency, buildPhysicsWorld.GetOutputDependency());
//prepare the seed for random value
uint seed = 1 + (uint)Time.ElapsedTime;
Unity.Mathematics.Random random = new Unity.Mathematics.Random(seed);
Entities
.WithNone<UnitDeathComponent, UnitDisableFlocking>()
.WithReadOnly(physicsWorld)
.ForEach((Entity entity, int entityInQueryIndex, ref FlockComponent bc, in Translation translation, in LocalToWorld localToWorld) =>
{
bc.yTimer -= deltaTime;
if (bc.yTimer <= 0f)
{
float3 hitPosition = float3.zero;
if (NavUtil.GetPointOnSurfaceLayer(physicsWorld, localToWorld, translation.Value, out hitPosition))
{
bc.yHeight = hitPosition.y;
bc.yTimer = random.NextFloat(0.3f, 1f);
}
}
})
.WithName("Flocking_Grounding")
.ScheduleParallel();
Entities
.WithReadOnly(cellVsEntityPositionsForJob)
.WithNone<UnitDeathComponent, UnitDisableFlocking>()
.ForEach((Entity entity, ref FlockComponent bc, in Translation trans) =>
{
int key = GetUniqueKeyForPosition(trans.Value, bc.flockManager.cellSize);
NativeMultiHashMapIterator<int> nmhKeyIterator;
FlockComponent neighbour;
int totalAlignment = 0;
int totalSeperate = 0;
float3 separation = float3.zero;
float3 alignment = float3.zero;
float3 cohesion = float3.zero;
float angle;
// get the first neighbour for the current flock agent
if (cellVsEntityPositionsForJob.TryGetFirstValue(key, out neighbour, out nmhKeyIterator))
{
do
{
if (!bc.entity.Equals(neighbour.entity)
&& math.distance(trans.Value, neighbour.currentPosition) < bc.flockManager.PerceptionRadius && bc.formation == neighbour.formation)
{
angle = math.acos(math.dot(bc.velocity, (neighbour.currentPosition - trans.Value)) / (math.length(bc.velocity) * math.length(neighbour.currentPosition - trans.Value)));
if (math.abs(angle) <= bc.flockManager.fieldOfView)
{
//if more operations have been done than the maxPerceive value then break out of loop
if (totalAlignment >= bc.flockManager.maxPerceived)
{
break;
}
float3 distanceFromTo = trans.Value - neighbour.currentPosition;
separation += (distanceFromTo / math.distance(trans.Value, neighbour.currentPosition));
alignment += neighbour.velocity;
totalSeperate++;
totalAlignment++;
}
}
} while (cellVsEntityPositionsForJob.TryGetNextValue(out neighbour, ref nmhKeyIterator));
if (totalAlignment > 0)
{
alignment = alignment / totalAlignment;
alignment = alignment - bc.velocity;
alignment = math.normalize(alignment) * bc.flockManager.AlignmentBias;
}
if (totalSeperate > 0)
{
separation = separation / totalSeperate;
separation = separation - bc.velocity;
separation = math.normalize(separation) * bc.flockManager.SeparationBias;
}
//override, if not inFormation it will use a target position set outside of this script to move towards
if (bc.inFormation)
{
cohesion = (math.normalize(bc.targetFormationPosition - trans.Value) * math.clamp(math.distance(trans.Value, bc.targetFormationPosition), 0.00f, 10.0f) ) * bc.flockManager.CohesionBias;
}
else
{
cohesion = (math.normalize(bc.targetPos - trans.Value) * math.clamp(math.distance(trans.Value, bc.targetPos), 0.00f, 1.0f)) * bc.flockManager.CohesionBias;
}
//Combining all the forces
float3 forces = (cohesion + separation + alignment);
bc.velocity = bc.velocity + forces;
//bc.velocity = math.normalizesafe(bc.velocity) * bc.speed;
}
})
.WithName("Flocking_FlockingCalculation")
.ScheduleParallel();
var unitComponents = GetComponentDataFromEntity<UnitComponent>(true);
var formationInAttackRangeComponents = GetComponentDataFromEntity<FormationInAttackRangeComponent>(true);
//var unitAttackComponents = GetComponentDataFromEntity<UnitAttackComponent>(true);
var attackDistance = CloseCombatPositioningSystem._attackDistance;
//unit perception
Entities
.WithReadOnly(cellVsEntityPositionsForJob)
.WithReadOnly(unitComponents)
.WithReadOnly(formationInAttackRangeComponents)
//.WithReadOnly(unitAttackComponents)
.WithReadOnly(formationComponents)
.WithReadOnly(meleeFormationComponents)
.WithNone<UnitDeathComponent, MeleeUnitAttackComponent>()
.ForEach((Entity entity, int entityInQueryIndex, ref FlockComponent bc, in Translation trans, in UnitComponent unitComponent) =>
{
int key = GetUniqueKeyForPosition(trans.Value, bc.flockManager.cellSize);
NativeMultiHashMapIterator<int> nmhKeyIterator;
FlockComponent neighbour;
float angle;
FlockComponent closestNeighbour = new FlockComponent();
float closestDistance = 100000;
int allySeen = 0;
float avoidanceAngle = 0;
bool closestIsAlly = true;
bool found = false;
//if (true)
{
//targetting
if (formationInAttackRangeComponents.HasComponent(bc.formation) && meleeFormationComponents.HasComponent(bc.formation))
{
//Debug.Log("targetting");
if (cellVsEntityPositionsForJob.TryGetFirstValue(key, out neighbour, out nmhKeyIterator))
{
do
{
if (!bc.entity.Equals(neighbour.entity))
{
var distanceBetween = math.distance(trans.Value, neighbour.currentPosition);
if (distanceBetween < attackDistance * 1.0 && unitComponent.faction != unitComponents[neighbour.entity].faction)
{
closestDistance = distanceBetween;
closestNeighbour = neighbour;
//Debug.Log("attack");
closestIsAlly = false;
found = true;
break;
}
//check if distance is higher than saved distance, if so then skip.
if (distanceBetween > closestDistance)
{
continue;
}
//sees enemy unit
if (distanceBetween < attackDistance)
{
if (unitComponent.faction != unitComponents[neighbour.entity].faction /*&& !unitAttackComponents.HasComponent(neighbour.entity)*/)
{
//if (unitComponents[neighbour.entity].targetedAmount <= 2)
{
closestDistance = distanceBetween;
closestNeighbour = neighbour;
//Debug.Log("attack");
closestIsAlly = false;
found = true;
if (distanceBetween < attackDistance * 0.5f)
{
break;
}
continue;
}
}
}
//if is in collision range (check against friendlies)
if (distanceBetween < bc.flockManager.collisionRange && meleeFormationComponents.HasComponent(neighbour.formation))
{
//if velocity is 0 then use rotation, but might want to break instead
var tempcheck = new float3(0, 0, 0);
if (bc.velocity.Equals(tempcheck))
{
bc.velocity = tempcheck;
}
angle = math.acos(math.dot(bc.velocity, (neighbour.currentPosition - trans.Value)) / (math.length(bc.velocity) * math.length(neighbour.currentPosition - trans.Value)));
if (math.abs(angle) <= bc.flockManager.fieldOfView)
{
//seen unit is an ally
if (unitComponent.faction == unitComponents[neighbour.entity].faction)
{
closestDistance = distanceBetween;
closestNeighbour = neighbour;
allySeen++;
avoidanceAngle = angle;
closestIsAlly = true;
found = true;
continue;
}
}
}
}
} while (cellVsEntityPositionsForJob.TryGetNextValue(out neighbour, ref nmhKeyIterator));
}
if (found)
{
if (closestIsAlly && allySeen >= bc.flockManager.maxToCollide && math.distance(trans.Value, bc.targetPos) <= bc.flockManager.collisionRange * 2.5)
{
bc.blockingUnit = true;
bc.stopAnimation = true;
bc.stopAnimationTimer = 0.3f;
bc.velocity = float3.zero;
}
else if (closestIsAlly)
{
//Debug.Log("Avoiding ally");
bc.blockingUnit = false;
//Avoid ally unit
//direction away from centroid
var directionAway = trans.Value - formationComponents[bc.formation].formationCentroid;
directionAway = math.normalize(directionAway);
var tempDistance = math.distance(trans.Value, formationComponents[bc.formation].formationCentroid);
math.clamp(tempDistance, 0.20f, 10f);
directionAway = directionAway / tempDistance;
directionAway = directionAway * 0.5f;
var angleCalc = trans.Value - closestNeighbour.currentPosition;
angleCalc = math.normalize(angleCalc);
angleCalc = angleCalc / closestDistance;
angleCalc = angleCalc * 1f;
angleCalc = angleCalc + directionAway;
bc.velocity = bc.velocity + angleCalc * bc.flockManager.collisionForce;
//Debug.DrawLine(trans.Value, trans.Value + angleCalc * 10);
}
else
{
//Start attacking enemy unit
//important debug text
//if (unitComponents[entity].faction == FactionType.Enemy)
//{
// Debug.Log("enemy unit start attacking friendly unit");
//}
//else if (unitComponents[entity].faction == FactionType.Player)
//{
// Debug.Log("friendly unit start attacking enemy unit");
//}
ecb.AddComponent<MeleeUnitAttackComponent>(entityInQueryIndex, entity);
ecb.SetComponent(entityInQueryIndex, entity, new TargetComponent
{
target = closestNeighbour.entity
});
UnitComponent targetUnitComponent = unitComponents[closestNeighbour.entity];
targetUnitComponent.targetedAmount++;
ecb.SetComponent(entityInQueryIndex, closestNeighbour.entity, targetUnitComponent);
bc.blockingUnit = true;
bc.velocity = new float3(0, 0, 0);
}
}
else
{
bc.blockingUnit = false;
}
}
}
})
.WithName("Flocking_UnitPerception")
.ScheduleParallel();
//unit outside of combat collision
Entities
.WithReadOnly(cellVsEntityPositionsForJob)
.WithReadOnly(unitComponents)
.WithReadOnly(formationInAttackRangeComponents)
.WithReadOnly(formationComponents)
.WithNone<UnitDeathComponent, MeleeUnitAttackComponent, UnitDisableFlocking>()
.WithAll<MoveStateComponent>()
.ForEach((Entity entity, int entityInQueryIndex, ref FlockComponent bc, ref Translation trans, in UnitComponent unitComponent) =>
{
if (formationInAttackRangeComponents.HasComponent(bc.formation) || !bc.inFormation)
{
return;
}
//Debug.Log("running");
//timer for optimization
bc.collisionTimer -= deltaTime;
if (!(bc.collisionTimer <= 0.0f))
{
return;
}
bc.collisionTimer = random.NextFloat(0.1f, 0.3f); ;
int key = GetUniqueKeyForPosition(trans.Value, bc.flockManager.cellSize);
NativeMultiHashMapIterator<int> nmhKeyIterator;
FlockComponent neighbour;
float angle;
FlockComponent closestNeighbour = new FlockComponent();
float closestDistance = 100000;
int allySeen = 0;
float avoidanceAngle = 0;
bool closestIsAlly = true;
bool found = false;
if (true)
{
if (cellVsEntityPositionsForJob.TryGetFirstValue(key, out neighbour, out nmhKeyIterator))
{
do
{
if (!bc.entity.Equals(neighbour.entity) && neighbour.isActive)
{
var distanceBetween = math.distance(trans.Value, neighbour.currentPosition);
//check if distance is higher than saved distance, if so then skip.
if (distanceBetween > closestDistance)
{
continue;
}
//if is in collision range (check against friendlies)
if (distanceBetween < bc.flockManager.collisionRange)
{
//if velocity is 0 then use rotation, but might want to break instead
var tempcheck = new float3(0, 0, 0);
if (bc.velocity.Equals(tempcheck))
{
bc.velocity = tempcheck;
}
angle = math.acos(math.dot(bc.velocity, (neighbour.currentPosition - trans.Value)) / (math.length(bc.velocity) * math.length(neighbour.currentPosition - trans.Value)));
if (math.abs(angle) <= bc.flockManager.fieldOfView * 0.5)
{
//seen unit is an ally
if (unitComponent.faction == unitComponents[neighbour.entity].faction)
{
closestDistance = distanceBetween;
closestNeighbour = neighbour;
allySeen++;
avoidanceAngle = angle;
closestIsAlly = true;
found = true;
//if spotted 2 allies then stop
if (allySeen >= 3)
{
//Debug.Log("2 allies spotted, stopping..");
//bc.velocity = float3.zero;
break;
}
continue;
}
}
}
}
} while (cellVsEntityPositionsForJob.TryGetNextValue(out neighbour, ref nmhKeyIterator));
}
//Debug.DrawLine(trans.Value, trans.Value + bc.velocity * 5, Color.red);
//var formationFloat3 = new float3(unitComponent.formationPos.x, 0, unitComponent.formationPos.y);
//formationFloat3 = math.normalize(formationFloat3);
//Debug.DrawLine(trans.Value, trans.Value + formationFloat3 * 10, Color.blue);
if (found)
{
if (closestIsAlly && allySeen >= 3)
{
bc.velocity = float3.zero;
}
else if (closestIsAlly)
{
//Debug.Log("Avoiding ally");
//Avoid ally unit
//direction away from centroid
var directionAway = trans.Value - formationComponents[bc.formation].formationCentroid;
directionAway = directionAway * math.distance(trans.Value, formationComponents[bc.formation].formationCentroid);
directionAway = math.normalize(directionAway);
directionAway = directionAway * 0.5f;
//var angleCalc = new float3(math.cos(avoidanceAngle), 0, math.sin(avoidanceAngle));
var angleCalc = trans.Value - closestNeighbour.currentPosition;
angleCalc = angleCalc / closestDistance;
angleCalc = math.normalize(angleCalc);
angleCalc = angleCalc * 1f;
angleCalc = angleCalc + directionAway;
bc.velocity = bc.velocity + angleCalc * bc.flockManager.collisionForce;
//Debug.DrawLine(trans.Value, trans.Value + angleCalc * 10);
}
}
}
})
.WithName("Flocking_NonCombatCollision")
.ScheduleParallel();
//check if flocking should run
Entities
.WithNone<UnitDeathComponent, UnitDisableFlocking>()
.ForEach((ref FlockComponent bc, ref Translation trans) =>
{
// Set the translation
//normalize the velocity
bc.velocity = new float3(bc.velocity.x, 0, bc.velocity.z);
bc.velocity = math.normalizesafe(bc.velocity) * bc.speed;
//setting the new translation value
trans.Value = trans.Value + (bc.velocity) * math.clamp(deltaTime, 0.000f, 0.5f);
trans.Value = math.lerp(trans.Value, new float3(trans.Value.x, bc.yHeight, trans.Value.z), deltaTime * 5);
bc.currentPosition = trans.Value;
//
})
.WithName("Flocking_SetTranslation")
.ScheduleParallel();
endECB.AddJobHandleForProducer(Dependency);
}
protected override void OnDestroy()
{
cellVsEntityPositions.Dispose();
}
}