summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Core/ECS/Jobs/JobSyncEntitiesToTransforms.cs
blob: 041f03dc8ca835f2cea457b46a557aa5f8242092 (plain)
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
#if MODULE_ENTITIES
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine.Jobs;

namespace Pathfinding.ECS {
	[BurstCompile]
	struct JobSyncEntitiesToTransforms : IJobParallelForTransform {
		[ReadOnly]
		public NativeArray<Entity> entities;
		[ReadOnly]
		public ComponentLookup<LocalTransform> entityPositions;
		[ReadOnly]
		public ComponentLookup<MovementState> movementState;
		[ReadOnly]
		public ComponentLookup<SyncPositionWithTransform> syncPositionWithTransform;
		[ReadOnly]
		public ComponentLookup<SyncRotationWithTransform> syncRotationWithTransform;
		[ReadOnly]
		public ComponentLookup<OrientationYAxisForward> orientationYAxisForward;

		public void Execute (int index, TransformAccess transform) {
			var entity = entities[index];
			if (!entityPositions.HasComponent(entity)) return;
			float3 offset = float3.zero;
			if (movementState.TryGetComponent(entity, out var ms)) {
				offset = ms.positionOffset;
			}

			var tr = entityPositions.GetRefRO(entity);
			if (syncPositionWithTransform.HasComponent(entity)) transform.position = tr.ValueRO.Position + offset;
			if (syncRotationWithTransform.HasComponent(entity)) {
				if (orientationYAxisForward.HasComponent(entity)) {
					// Y axis forward
					transform.rotation = math.mul(tr.ValueRO.Rotation, SyncTransformsToEntitiesSystem.ZAxisForwardToYAxisForward);
				} else {
					// Z axis forward
					transform.rotation = tr.ValueRO.Rotation;
				}
			}
		}
	}
}
#endif