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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#pragma warning disable CS0282
#if MODULE_ENTITIES
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine.Profiling;
using Unity.Transforms;
using Unity.Burst;
using Unity.Jobs;
using Unity.Collections;
using UnityEngine.Jobs;
namespace Pathfinding.ECS {
using Pathfinding;
using Pathfinding.Util;
[UpdateBefore(typeof(TransformSystemGroup))]
[UpdateBefore(typeof(AIMovementSystemGroup))]
[UpdateInGroup(typeof(SimulationSystemGroup))]
public partial struct SyncTransformsToEntitiesSystem : ISystem {
public static readonly quaternion ZAxisForwardToYAxisForward = quaternion.Euler(math.PI / 2, 0, 0);
public static readonly quaternion YAxisForwardToZAxisForward = quaternion.Euler(-math.PI / 2, 0, 0);
public void OnCreate (ref SystemState state) {}
public void OnDestroy (ref SystemState state) {}
public void OnUpdate (ref SystemState systemState) {
int numComponents = BatchedEvents.GetComponents<FollowerEntity>(BatchedEvents.Event.None, out var transforms, out var components);
if (numComponents > 0) {
var entities = new NativeArray<Entity>(numComponents, Allocator.TempJob);
for (int i = 0; i < numComponents; i++) entities[i] = components[i].entity;
systemState.Dependency = new SyncTransformsToEntitiesJob {
entities = entities,
entityPositions = SystemAPI.GetComponentLookup<LocalTransform>(),
syncPositionWithTransform = SystemAPI.GetComponentLookup<SyncPositionWithTransform>(true),
syncRotationWithTransform = SystemAPI.GetComponentLookup<SyncRotationWithTransform>(true),
orientationYAxisForward = SystemAPI.GetComponentLookup<OrientationYAxisForward>(true),
movementState = SystemAPI.GetComponentLookup<MovementState>(true),
}.Schedule(transforms, systemState.Dependency);
}
}
[BurstCompile]
struct SyncTransformsToEntitiesJob : IJobParallelForTransform {
[ReadOnly]
[DeallocateOnJobCompletion]
public NativeArray<Entity> entities;
// Safety: All entities are unique
[NativeDisableParallelForRestriction]
public ComponentLookup<LocalTransform> entityPositions;
[ReadOnly]
public ComponentLookup<SyncPositionWithTransform> syncPositionWithTransform;
[ReadOnly]
public ComponentLookup<SyncRotationWithTransform> syncRotationWithTransform;
[ReadOnly]
public ComponentLookup<OrientationYAxisForward> orientationYAxisForward;
[ReadOnly]
public ComponentLookup<MovementState> movementState;
public void Execute (int index, TransformAccess transform) {
var entity = entities[index];
if (entityPositions.HasComponent(entity)) {
#if MODULE_ENTITIES_1_0_8_OR_NEWER
ref var tr = ref entityPositions.GetRefRW(entity).ValueRW;
#else
ref var tr = ref entityPositions.GetRefRW(entity, false).ValueRW;
#endif
float3 offset = float3.zero;
if (movementState.TryGetComponent(entity, out var ms)) {
offset = ms.positionOffset;
}
if (syncPositionWithTransform.HasComponent(entity)) tr.Position = (float3)transform.position - offset;
if (syncRotationWithTransform.HasComponent(entity)) {
if (orientationYAxisForward.HasComponent(entity)) {
tr.Rotation = math.mul(transform.rotation, YAxisForwardToZAxisForward);
} else {
// Z axis forward
tr.Rotation = transform.rotation;
}
}
tr.Scale = transform.localScale.y;
}
}
}
}
}
#endif
|