-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilteredEntityCollisionsSystem.cs
50 lines (43 loc) · 1.7 KB
/
FilteredEntityCollisionsSystem.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
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
namespace Arc.FilteredCollisions
{
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(EndFramePhysicsSystem))]
public class FilteredEntityCollisionsSystem : SystemBase
{
private BuildPhysicsWorld buildPhysicsWorld => World.GetOrCreateSystem<BuildPhysicsWorld>();
private StepPhysicsWorld stepPhysicsWorld => World.GetOrCreateSystem<StepPhysicsWorld>();
internal NativeHashMap<EntityPair, CollisionEvent> Collisions;
internal JobHandle EventsCollectedHandle;
protected override void OnCreate()
{
Collisions = new NativeHashMap<EntityPair, CollisionEvent>(10, Allocator.Persistent);
}
protected override void OnDestroy()
{
Collisions.Dispose();
}
protected override void OnUpdate()
{
var collisions = Collisions;
Job.WithCode(() => { collisions.Clear(); })
.Schedule();
Dependency = new CollectCollisionsJob {Collisions = collisions,}
.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, Dependency);
EventsCollectedHandle = Dependency;
}
private struct CollectCollisionsJob : ICollisionEventsJob
{
public NativeHashMap<EntityPair, CollisionEvent> Collisions;
public void Execute(CollisionEvent collisionEvent)
{
Collisions[new EntityPair {EntityA = collisionEvent.EntityA, EntityB = collisionEvent.EntityB,}] =
collisionEvent;
}
}
}
}