forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileOffsetSystem.cs
More file actions
51 lines (45 loc) · 1.94 KB
/
Copy pathTileOffsetSystem.cs
File metadata and controls
51 lines (45 loc) · 1.94 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.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Streaming.SceneManagement.CompleteSample
{
// System that will move/orient all the entities in the tile to the right position/rotation.
[WorldSystemFilter(WorldSystemFilterFlags.ProcessAfterLoad)]
public partial struct TileOffsetSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TileOffset>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var offsetQuery = SystemAPI.QueryBuilder().WithAll<TileOffset>().Build();
var offsets = offsetQuery.ToComponentDataArray<TileOffset>(Allocator.Temp);
state.EntityManager.DestroyEntity(offsetQuery);
foreach (var offset in offsets)
{
var rotation = quaternion.AxisAngle(new float3(0f, 1f, 0f), offset.Rotation);
var offsetTransform = LocalTransform.FromPositionRotation(offset.Offset, rotation);
// Apply the offset and rotation to all dynamic entities
foreach (var transform in
SystemAPI.Query<RefRW<LocalTransform>>()
.WithNone<Parent>())
{
transform.ValueRW = offsetTransform.TransformTransform(transform.ValueRW);
}
var offsetMatrix = float4x4.TRS(offset.Offset, rotation, new float3(1f, 1f, 1f));
// Apply the offset and rotation to all non-dynamic entities
foreach (var transform in
SystemAPI.Query<RefRW<LocalToWorld>>()
.WithNone<Parent, LocalTransform>())
{
transform.ValueRW.Value = math.mul(offsetMatrix, transform.ValueRW.Value);
}
}
}
}
}