-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathMoveProjectilesSystem.cs
More file actions
48 lines (43 loc) · 1.54 KB
/
MoveProjectilesSystem.cs
File metadata and controls
48 lines (43 loc) · 1.54 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
using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
namespace HelloCube.FixedTimestep
{
public partial struct MoveProjectilesSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EndSimulationEntityCommandBufferSystem.Singleton>();
state.RequireForUpdate<ExecuteFixedTimestep>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
new MoveJob
{
TimeSinceLoad = (float)SystemAPI.Time.ElapsedTime,
ProjectileSpeed = 5.0f,
ECBWriter = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter()
}.ScheduleParallel();
}
}
[BurstCompile]
public partial struct MoveJob : IJobEntity
{
public float TimeSinceLoad;
public float ProjectileSpeed;
public EntityCommandBuffer.ParallelWriter ECBWriter;
void Execute(Entity projectileEntity, [ChunkIndexInQuery] int chunkIndex, ref LocalTransform transform,
in Projectile projectile)
{
float aliveTime = TimeSinceLoad - projectile.SpawnTime;
if (aliveTime > 5.0f)
{
ECBWriter.DestroyEntity(chunkIndex, projectileEntity);
}
transform.Position.x = projectile.SpawnPos.x + aliveTime * ProjectileSpeed;
}
}
}