forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplosionCountdownSystem.cs
More file actions
73 lines (60 loc) · 2.46 KB
/
Copy pathExplosionCountdownSystem.cs
File metadata and controls
73 lines (60 loc) · 2.46 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
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Extensions;
using Unity.Physics.Systems;
using Unity.Transforms;
public struct ExplosionCountdown : IComponentData
{
public Entity Source;
public int Countdown;
public float3 Center;
public float Force;
}
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(BuildPhysicsWorld))]
public class ExplosionCountdownSystem : SystemBase
{
private BuildPhysicsWorld m_BuildPhysicsWorld;
private EndFixedStepSimulationEntityCommandBufferSystem m_CommandBufferSystem;
protected override void OnCreate()
{
m_BuildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
m_CommandBufferSystem = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
RequireForUpdate(GetEntityQuery(new ComponentType[] { typeof(ExplosionCountdown) }));
}
protected override void OnUpdate()
{
var commandBufferParallel = m_CommandBufferSystem.CreateCommandBuffer().AsParallelWriter();
var timeStep = Time.DeltaTime;
var up = math.up();
var positions = GetComponentDataFromEntity<Translation>(true);
Entities
.WithName("ExplosionCountdown_Tick")
.WithReadOnly(positions)
.WithBurst()
.ForEach((Entity entity, ref ExplosionCountdown explosion) =>
{
explosion.Countdown--;
bool bang = explosion.Countdown <= 0;
if (bang && !explosion.Source.Equals(Entity.Null))
{
explosion.Center = positions[explosion.Source].Value;
}
}).ScheduleParallel();
Entities
.WithName("ExplosionCountdown_Bang")
.WithBurst()
.ForEach((int entityInQueryIndex, Entity entity,
ref ExplosionCountdown explosion, ref PhysicsVelocity pv,
in PhysicsMass pm, in PhysicsCollider collider,
in Translation pos, in Rotation rot) =>
{
if (0 < explosion.Countdown) return;
pv.ApplyExplosionForce(pm, collider, pos, rot,
explosion.Force, explosion.Center, 0, timeStep, up);
commandBufferParallel.RemoveComponent<ExplosionCountdown>(entityInQueryIndex, entity);
}).Schedule();
m_BuildPhysicsWorld.AddInputDependencyToComplete(Dependency);
}
}