forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveForwardSystem.cs
More file actions
54 lines (49 loc) · 1.89 KB
/
Copy pathMoveForwardSystem.cs
File metadata and controls
54 lines (49 loc) · 1.89 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 Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Samples.Common
{
public class MoveForwardSystem : JobComponentSystem
{
[BurstCompile]
struct MoveForwardRotation : IJobParallelFor
{
public ComponentDataArray<Position> positions;
[ReadOnly] public ComponentDataArray<Rotation> rotations;
[ReadOnly] public ComponentDataArray<MoveSpeed> moveSpeeds;
public float dt;
public void Execute(int i)
{
positions[i] = new Position
{
Value = positions[i].Value + (dt * moveSpeeds[i].speed * math.forward(rotations[i].Value))
};
}
}
ComponentGroup m_MoveForwardRotationGroup;
protected override void OnCreateManager()
{
m_MoveForwardRotationGroup = GetComponentGroup(
ComponentType.ReadOnly(typeof(MoveForward)),
ComponentType.ReadOnly(typeof(Rotation)),
ComponentType.ReadOnly(typeof(MoveSpeed)),
typeof(Position));
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var moveForwardRotationJob = new MoveForwardRotation
{
positions = m_MoveForwardRotationGroup.GetComponentDataArray<Position>(),
rotations = m_MoveForwardRotationGroup.GetComponentDataArray<Rotation>(),
moveSpeeds = m_MoveForwardRotationGroup.GetComponentDataArray<MoveSpeed>(),
dt = Time.deltaTime
};
var moveForwardRotationJobHandle = moveForwardRotationJob.Schedule(m_MoveForwardRotationGroup.CalculateLength(), 64, inputDeps);
return moveForwardRotationJobHandle;
}
}
}