forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridMoveForwardSystem.cs
More file actions
55 lines (48 loc) · 2.05 KB
/
Copy pathCartesianGridMoveForwardSystem.cs
File metadata and controls
55 lines (48 loc) · 2.05 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
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
[RequireMatchingQueriesForUpdate]
[UpdateBefore(typeof(TransformSystemGroup))]
public partial class CartesianGridMoveForwardSystem : SystemBase
{
protected override void OnUpdate()
{
// Clamp delta time so you can't overshoot.
var deltaTime = math.min(SystemAPI.Time.DeltaTime, 0.05f);
// Move forward along direction in grid-space given speed.
// - This is the same for Plane or Cube and is the core of the movement code. Simply "move forward" along direction.
Entities
.WithName("CartesianGridMoveForward")
#if !ENABLE_TRANSFORM_V1
.ForEach((ref LocalToWorldTransform transform,
#else
.ForEach((ref Translation translation,
#endif
in CartesianGridDirection gridDirection,
in CartesianGridSpeed gridSpeed,
in CartesianGridCoordinates gridPosition) =>
{
var dir = gridDirection.Value;
if (dir == 0xff)
return;
#if !ENABLE_TRANSFORM_V1
var pos = transform.Value.Position;
#else
var pos = translation.Value;
#endif
// Speed adjusted to float m/s from fixed point 6:10 m/s
var speed = deltaTime * ((float)gridSpeed.Value) * (1.0f / 1024.0f);
// Write: add unit vector offset scaled by speed and deltaTime to current position
var dx = CartesianGridMovement.UnitMovement[(dir * 2) + 0] * speed;
var dz = CartesianGridMovement.UnitMovement[(dir * 2) + 1] * speed;
// Smooth y changes when transforming between cube faces.
var dy = math.min(speed, 1.0f - pos.y);
#if !ENABLE_TRANSFORM_V1
transform.Value.Position = new float3(pos.x + dx, pos.y + dy, pos.z + dz);
#else
translation.Value = new float3(pos.x + dx, pos.y + dy, pos.z + dz);
#endif
}).Schedule();
}
}