forked from Blissgig/Easy-Road-3D-ECS-Traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ER3D_TrafficSystem.cs
203 lines (166 loc) · 8.68 KB
/
ER3D_TrafficSystem.cs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
public class ER3D_TrafficSystem : JobComponentSystem
{
protected override void OnStartRunning()
{
EntityQuery allRoadsQuery = GetEntityQuery(
typeof(ERRoadTag),
ComponentType.ReadOnly<RoadDetails>(),
ComponentType.ReadOnly<LanePoints>());
roadEntities = allRoadsQuery.ToEntityArray(Allocator.Persistent);
EntityQuery allConnectionsQuery = GetEntityQuery(
typeof(ERConnectionTag),
ComponentType.ReadOnly<ConnectionDetails>(),
ComponentType.ReadOnly<LanePoints>());
connectionEntities = allConnectionsQuery.ToEntityArray(Allocator.Persistent);
}
[BurstCompile]
[RequireComponentTag(typeof(ERAutoTag))]
protected struct AutoNavigationJob : IJobForEach_BCCCC<
AutoLanePoints,
AutoDetails,
AutoPosition,
Translation,
Rotation>
{
[ReadOnly]
public ComponentDataFromEntity<RoadDetails> RoadDetailsFromEntity;
[ReadOnly]
public ComponentDataFromEntity<ConnectionDetails> ConnectionDetailsFromEntity;
[ReadOnly]
public BufferFromEntity<LanePoints> LanePointsFromEntity;
[ReadOnly]
public NativeArray<Entity> roadEntities;
[ReadOnly]
public NativeArray<Entity> connectionEntities;
public float deltaTime;
public float reachedPositionDistance;
public uint randomSeed;
public void Execute(
DynamicBuffer<AutoLanePoints> autoLanePoints,
[ReadOnly] ref AutoDetails autoDetails,
ref AutoPosition autoPosition,
ref Translation autoTranslation,
ref Rotation autoRotation)
{
if (autoLanePoints.Length == 0)
{
/* This is a basic check,
* there seems to be a few instances
* where the auto does not have points,
*/
return;
}
var distance = math.distance(autoPosition.Destination, autoTranslation.Value);
if (distance <= reachedPositionDistance)
{
autoPosition.CurrentPositionIndex += 1;
//If the Auto's current position is at the end of it's list of
//points, get the next list of points from the road or connection.
if (autoPosition.CurrentPositionIndex >= autoLanePoints.Length)
{
autoPosition.CurrentPositionIndex = 0;
autoLanePoints.Clear();
int laneIndex = 0;
int roadIdentity = 0;
int connectionIdentityEnd = 0;
for (int i = 0; i < roadEntities.Length; i++)
{
RoadDetails roadDetails = RoadDetailsFromEntity[roadEntities[i]];
if ((roadDetails.RoadIdentity == autoPosition.RoadIdentity) &&
(roadDetails.LaneIndex == autoPosition.LaneIndex) &&
(roadDetails.ConnectionIdentityStart == autoPosition.ConnectionIdentity) &&
(roadDetails.ConnectionIndexStart == autoPosition.ConnectionIndex)
)
{
//Set variables for use to get the next connection
laneIndex = roadDetails.LaneIndex;
roadIdentity = roadDetails.RoadIdentity;
connectionIdentityEnd = roadDetails.ConnectionIdentityEnd;
var roadLanePointsBuffer = LanePointsFromEntity[roadEntities[i]];
var roadLanePoints = roadLanePointsBuffer.ToNativeArray(Allocator.Temp);
var lanePointAuto = new AutoLanePoints();
for (int x = 0; x < roadLanePoints.Length; x++)
{
lanePointAuto.value = roadLanePoints[x].value;
autoLanePoints.Add(lanePointAuto);
}
break;
}
}
//Get the Connection's Lane's points
//First find all options that have the same Idenity and Index.
//Then we can randomly select one of the routes through the connection
NativeList<Entity> availableConnections = new NativeList<Entity>(Allocator.Temp);
for (int i = 0; i < connectionEntities.Length; i++)
{
var connectionDetails = ConnectionDetailsFromEntity[connectionEntities[i]];
if ((connectionDetails.ConnectionIdentity == connectionIdentityEnd) &&
(connectionDetails.LaneIndexStart == laneIndex) &&
(connectionDetails.RoadIdentityStart == roadIdentity) &&
(connectionDetails.ConnectionIndexStart == autoPosition.ConnectionIndex)
)
{
availableConnections.Add(connectionEntities[i]);
}
}
Unity.Mathematics.Random mathRandom = new Unity.Mathematics.Random(randomSeed);
int randomValue = mathRandom.NextInt(0, availableConnections.Length);
var connectionDetailsNew = ConnectionDetailsFromEntity[availableConnections[randomValue]];
var connectionLanePointsBuffer = LanePointsFromEntity[availableConnections[randomValue]];
var connectionLanePoints = connectionLanePointsBuffer.ToNativeArray(Allocator.Temp);
var lanePoint = new AutoLanePoints();
for (int x = 0; x < connectionLanePoints.Length; x++)
{
lanePoint.value = connectionLanePoints[x].value;
autoLanePoints.Add(lanePoint);
}
//Reset the Auto's variables for the next Road/Connection selection
autoPosition.LaneIndex = connectionDetailsNew.LaneIndexEnd;
autoPosition.RoadIdentity = connectionDetailsNew.RoadIdentityEnd;
autoPosition.ConnectionIdentity = connectionDetailsNew.ConnectionIdentity;
autoPosition.ConnectionIndex = connectionDetailsNew.ConnectionIndexEnd;
availableConnections.Dispose();
}
autoPosition.Destination = autoLanePoints[autoPosition.CurrentPositionIndex].value;
autoPosition.Destination.y += autoDetails.yOffset;
}
float3 lookVector = autoPosition.Destination - autoTranslation.Value;
if (!lookVector.Equals(float3.zero))
{
Quaternion rotationLookAt = Quaternion.LookRotation(lookVector);
autoRotation.Value = rotationLookAt;
}
float3 smoothedPosition = math.lerp(autoTranslation.Value, autoPosition.Destination, autoDetails.speed * deltaTime);
autoTranslation.Value = smoothedPosition;
}
}
private const float REACHED_POSITION_DISTANCE = 1.0f;
private NativeArray<Entity> roadEntities;
private NativeArray<Entity> connectionEntities;
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
System.Random random = new System.Random();
uint randomSeed = (uint)random.Next(88, 1000000);
AutoNavigationJob autoNaviationJob = new AutoNavigationJob
{
RoadDetailsFromEntity = GetComponentDataFromEntity<RoadDetails>(true),
ConnectionDetailsFromEntity = GetComponentDataFromEntity<ConnectionDetails>(true),
LanePointsFromEntity = GetBufferFromEntity<LanePoints>(true),
roadEntities = roadEntities,
connectionEntities = connectionEntities,
deltaTime = Time.deltaTime,
randomSeed = randomSeed,
reachedPositionDistance = REACHED_POSITION_DISTANCE
};
JobHandle jobHandle = autoNaviationJob.Schedule(this, inputDeps);
//JobHandle jobHandle = autoNaviationJob.Run(this, inputDeps); //USED FOR DEBUGGING
return jobHandle;
}
}