forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveProjectilesSystem.cs
More file actions
54 lines (49 loc) · 1.85 KB
/
Copy pathMoveProjectilesSystem.cs
File metadata and controls
54 lines (49 loc) · 1.85 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
using System.Collections;
using System.Collections.Generic;
using System.Xml.Schema;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Samples.FixedTimestepSystem
{
// This system updates all entities in the scene with both a RotationSpeed and Rotation component.
public class MoveProjectilesSystem : JobComponentSystem
{
[BurstCompile]
struct MoveProjectileJob : IJobForEachWithEntity<ProjectileSpawnTime, Translation>
{
public EntityCommandBuffer.Concurrent Commands;
public float TimeSinceLoad;
public float ProjectileSpeed;
public void Execute(Entity entity, int index, [ReadOnly] ref ProjectileSpawnTime spawnTime, ref Translation translation)
{
float aliveTime = (TimeSinceLoad - spawnTime.SpawnTime);
if (aliveTime > 5.0f)
{
Commands.DestroyEntity(index, entity);
}
translation.Value.x = aliveTime * ProjectileSpeed;
}
}
BeginSimulationEntityCommandBufferSystem m_beginSimEcbSystem;
protected override void OnCreate()
{
m_beginSimEcbSystem = World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
}
protected override JobHandle OnUpdate(JobHandle inputDependencies)
{
var jobHandle = new MoveProjectileJob()
{
Commands = m_beginSimEcbSystem.CreateCommandBuffer().ToConcurrent(),
TimeSinceLoad = (float)Time.ElapsedTime,
ProjectileSpeed = 5.0f,
}.Schedule(this, inputDependencies);
m_beginSimEcbSystem.AddJobHandleForProducer(jobHandle);
return jobHandle;
}
}
}