forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridOnPlaneTargetSystem.cs
More file actions
51 lines (44 loc) · 2.21 KB
/
Copy pathCartesianGridOnPlaneTargetSystem.cs
File metadata and controls
51 lines (44 loc) · 2.21 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
using Unity.Entities;
using Unity.Jobs;
[UpdateBefore(typeof(CartesianGridOnPlaneFollowTargetSystem))]
public unsafe class CartesianGridOnPlaneTargetSystem : JobComponentSystem
{
EntityQuery m_GridQuery;
protected override void OnCreate()
{
m_GridQuery = GetEntityQuery(ComponentType.ReadOnly<CartesianGridOnPlane>());
RequireForUpdate(m_GridQuery);
}
protected override JobHandle OnUpdate(JobHandle lastJobHandle)
{
// Get component data from the GridPlane
var cartesianGridPlane = GetSingleton<CartesianGridOnPlane>();
var rowCount = cartesianGridPlane.Blob.Value.RowCount;
var colCount = cartesianGridPlane.Blob.Value.ColCount;
var targetDirectionsBufferCapacity = ((colCount + 1) / 2) * rowCount;
var gridWalls = (byte*)cartesianGridPlane.Blob.Value.Walls.GetUnsafePtr();
// Initialize the buffer for the target paths with size appropriate to grid.
Entities
.WithName("InitializeTargets")
.WithAll<CartesianGridTarget>()
.WithNone<CartesianGridTargetDirection>()
.WithStructuralChanges()
.ForEach((Entity entity) =>
{
var buffer = EntityManager.AddBuffer<CartesianGridTargetDirection>(entity);
buffer.ResizeUninitialized(targetDirectionsBufferCapacity);
}).Run();
// Rebuild all the paths to the target *only* when the target's grid position changes.
Entities
.WithName("UpdateTargetPaths")
.WithAll<CartesianGridTarget>()
.ForEach((Entity entity, ref CartesianGridTargetCoordinates prevTargetPosition, in CartesianGridCoordinates targetPosition, in DynamicBuffer<CartesianGridTargetDirection> targetDirections) =>
{
if (prevTargetPosition.Equals(targetPosition))
return;
prevTargetPosition = new CartesianGridTargetCoordinates(targetPosition);
CartesianGridOnPlaneShortestPath.CalculateShortestPathsToTarget(targetDirections.Reinterpret<byte>().AsNativeArray(), rowCount, colCount, targetPosition, gridWalls);
}).Run();
return lastJobHandle;
}
}