forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeSystems.cs
More file actions
72 lines (59 loc) · 2.27 KB
/
Copy pathSnakeSystems.cs
File metadata and controls
72 lines (59 loc) · 2.27 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Graphical.Splines
{
[UpdateAfter(typeof(SnakeSpawnSystem))]
public partial struct SnakeUpdateSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<SnakeSettings>();
state.RequireForUpdate<ExecuteSplines>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var transformLookup = SystemAPI.GetComponentLookup<LocalTransform>();
var splineFromEntity = SystemAPI.GetComponentLookup<Spline>(true);
new SnakeJob
{
DeltaTime = SystemAPI.Time.DeltaTime,
TransformLookup = transformLookup,
SplineLookup = splineFromEntity
}.ScheduleParallel();
}
}
[BurstCompile]
public partial struct SnakeJob: IJobEntity
{
public float DeltaTime;
[NativeDisableParallelForRestriction] public ComponentLookup<LocalTransform> TransformLookup;
[ReadOnly] public ComponentLookup<Spline> SplineLookup;
void Execute(ref Snake snake, in DynamicBuffer<SnakePart> snakeParts)
{
var splineData = SplineLookup[snake.SplineEntity].Data;
ref var points = ref splineData.Value.Points;
ref var distance = ref splineData.Value.Distance;
var offset = snake.Offset + DeltaTime * snake.Speed;
snake.Offset = offset;
var maxDist = distance[distance.Length - 1];
for (int i = 0; i < snakeParts.Length; i += 1)
{
offset = (offset % maxDist + maxDist) % maxDist;
int idx = distance.LowerBound(offset);
var a = distance[idx];
var b = distance[idx + 1];
float t = (offset - a) / (b - a);
float3 pos = math.lerp(points[idx], points[idx + 1], t);
var transform = TransformLookup[snakeParts[i].Value];
transform.Position = pos + snake.Anchor;
TransformLookup[snakeParts[i].Value] = transform;
offset += snake.Spacing;
}
}
}
}