forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityKillerBehaviour.cs
More file actions
60 lines (50 loc) · 1.63 KB
/
Copy pathEntityKillerBehaviour.cs
File metadata and controls
60 lines (50 loc) · 1.63 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
using Unity.Physics;
using Unity.Collections;
using Unity.Entities;
using System;
using UnityEngine;
using Unity.Jobs;
public struct EntityKiller : IComponentData
{
public int TimeToDie;
}
public class EntityKillerBehaviour : MonoBehaviour, IConvertGameObjectToEntity
{
public int TimeToDie;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData<EntityKiller>(entity, new EntityKiller() { TimeToDie = TimeToDie });
}
}
public class EntityKillerSystem : JobComponentSystem
{
EndSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
protected override void OnCreate()
{
m_EntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
struct EntityKillerJob : IJobForEachWithEntity<EntityKiller>
{
public EntityCommandBuffer CommandBuffer;
public void Execute(Entity entity, int index, ref EntityKiller killer)
{
if (killer.TimeToDie > 0)
{
CommandBuffer.SetComponent<EntityKiller>(entity, new EntityKiller() { TimeToDie = killer.TimeToDie - 1 });
}
else
{
CommandBuffer.DestroyEntity(entity);
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new EntityKillerJob
{
CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer()
}.ScheduleSingle(this, inputDeps);
m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
return job;
}
}