This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
forked from MicroGSD/RoadArchitect
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGSDTrafficLightController.cs
560 lines (513 loc) · 17.5 KB
/
GSDTrafficLightController.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#region "Imports"
using UnityEngine;
//using System.Collections; // Unused
#endregion
[System.Serializable]
public class GSDTrafficLightController
{
public GameObject LightLeftObj;
public GameObject LightRightObj;
public GameObject[] LightsObj;
public MeshRenderer MR_Left;
public MeshRenderer MR_Right;
public MeshRenderer[] MR_MainsStorage;
public MeshRenderer MR_Main;
public Light LightLeft_R;
public Light LightLeft_Y;
public Light LightLeft_G;
public Light LightRight_R;
public Light LightRight_Y;
public Light LightRight_G;
public Light[] Lights_R;
public Light[] Lights_Y;
public Light[] Lights_G;
//Enums for controller:
public enum iLightControllerEnum { Regular, LeftTurn, MasterLeft1, MasterLeft2, Red }
//Enums for actual lights:
public enum iLightStatusEnum { Regular, LeftTurn, MasterLeft, Red, RightTurn }
public enum iLightSubStatusEnum { Green, Yellow, Red }
public enum iLightYieldSubStatusEnum { Green, Yellow, Red, YellowTurn, GreenTurn }
public iLightStatusEnum iLightStatus = iLightStatusEnum.Red;
public iLightSubStatusEnum iLightSubStatus = iLightSubStatusEnum.Green;
private bool bLeft = false;
private bool bRight = false;
private bool bMain = false;
private bool bUseSharedMaterial = false;
private bool bLeftTurnYieldOnGreen = true;
private bool bLightsEnabled = true;
#region "Constructor"
public GSDTrafficLightController(ref GameObject _LightLeft, ref GameObject _LightRight, ref GameObject[] _Lights, ref MeshRenderer _MR_Left, ref MeshRenderer _MR_Right, ref MeshRenderer[] MR_Mains)
{
LightLeftObj = _LightLeft;
LightRightObj = _LightRight;
LightsObj = _Lights;
MR_Left = _MR_Left;
MR_Right = _MR_Right;
MR_MainsStorage = MR_Mains;
MR_Main = MR_Mains[0];
Light[] tLights;
if (LightLeftObj != null)
{
tLights = LightLeftObj.transform.GetComponentsInChildren<Light>();
foreach (Light tLight in tLights)
{
if (tLight.transform.name.ToLower().Contains("redlight"))
{
LightLeft_R = tLight;
}
if (tLight.transform.name.ToLower().Contains("yellowlight"))
{
LightLeft_Y = tLight;
}
if (tLight.transform.name.ToLower().Contains("greenl"))
{
LightLeft_G = tLight;
}
}
}
if (LightRightObj != null)
{
tLights = LightRightObj.transform.GetComponentsInChildren<Light>();
foreach (Light tLight in tLights)
{
if (tLight.transform.name.ToLower().Contains("redlight"))
{
LightRight_R = tLight;
}
if (tLight.transform.name.ToLower().Contains("yellowlight"))
{
LightRight_Y = tLight;
}
if (tLight.transform.name.ToLower().Contains("greenl"))
{
LightRight_G = tLight;
}
}
}
int mCount = LightsObj.Length;
Lights_R = new Light[mCount];
Lights_Y = new Light[mCount];
Lights_G = new Light[mCount];
for (int index = 0; index < mCount; index++)
{
tLights = LightsObj[index].transform.GetComponentsInChildren<Light>();
foreach (Light tLight in tLights)
{
if (tLight.transform.name.ToLower().Contains("redlight"))
{
Lights_R[index] = tLight;
}
if (tLight.transform.name.ToLower().Contains("yellowlight"))
{
Lights_Y[index] = tLight;
}
if (tLight.transform.name.ToLower().Contains("greenl"))
{
Lights_G[index] = tLight;
}
}
}
}
#endregion
#region "Update"
public void UpdateLights(iLightStatusEnum tLightStatus, iLightSubStatusEnum tLightSubStatus, bool _bLightsEnabled)
{
bLightsEnabled = _bLightsEnabled;
iLightStatus = tLightStatus;
iLightSubStatus = tLightSubStatus;
bUseSharedMaterial = false;
switch (iLightStatus)
{
case iLightStatusEnum.Regular:
TriggerRegular();
break;
case iLightStatusEnum.LeftTurn:
TriggerLeftTurn();
break;
case iLightStatusEnum.MasterLeft:
TriggerMasterLeft();
break;
case iLightStatusEnum.Red:
TriggerRed();
break;
case iLightStatusEnum.RightTurn:
TriggerRightTurn();
break;
}
}
#endregion
#region "Triggers"
private void TriggerRegular()
{
if (bMain)
{
MRChange(ref MR_Main, iLightSubStatus);
for (int index = 1; index < MR_MainsStorage.Length; index++)
{
MRChange(ref MR_MainsStorage[index], iLightSubStatus);
}
LightChange(0, iLightSubStatus);
}
if (bLeft)
{
if (bLeftTurnYieldOnGreen)
{
if (iLightSubStatus == iLightSubStatusEnum.Green)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.Green);
}
else if (iLightSubStatus == iLightSubStatusEnum.Yellow)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.Yellow);
}
}
else
{
MRChange(ref MR_Left, iLightSubStatusEnum.Red);
LightChange(1, iLightSubStatusEnum.Red);
}
}
if (bRight)
{
MRChange(ref MR_Right, iLightSubStatusEnum.Red);
LightChange(2, iLightSubStatusEnum.Red);
}
}
private void TriggerLeftTurn()
{
if (bMain)
{
MRChange(ref MR_Main, iLightSubStatusEnum.Red);
for (int i = 1; i < MR_MainsStorage.Length; i++)
{
MRChange(ref MR_MainsStorage[i], iLightSubStatusEnum.Red);
}
LightChange(0, iLightSubStatusEnum.Red);
}
if (bLeft)
{
if (bLeftTurnYieldOnGreen)
{
if (iLightSubStatus == iLightSubStatusEnum.Green)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.GreenTurn);
}
else if (iLightSubStatus == iLightSubStatusEnum.Yellow)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.YellowTurn);
}
LightChange(1, iLightSubStatus);
}
else
{
MRChange(ref MR_Left, iLightSubStatus);
LightChange(1, iLightSubStatus);
}
}
if (bRight)
{
MRChange(ref MR_Right, iLightSubStatusEnum.Red);
LightChange(2, iLightSubStatusEnum.Red);
}
}
private void TriggerMasterLeft()
{
if (bMain)
{
MRChange(ref MR_Main, iLightSubStatus);
for (int index = 1; index < MR_MainsStorage.Length; index++)
{
MRChange(ref MR_MainsStorage[index], iLightSubStatus);
}
LightChange(0, iLightSubStatus);
}
if (bLeft)
{
if (iLightSubStatus == iLightSubStatusEnum.Green)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.GreenTurn);
}
else if (iLightSubStatus == iLightSubStatusEnum.Yellow)
{
MRChangeLeftYield(ref MR_Left, iLightYieldSubStatusEnum.YellowTurn);
}
LightChange(1, iLightSubStatus);
}
if (bRight)
{
MRChange(ref MR_Right, iLightSubStatus);
LightChange(2, iLightSubStatus);
}
}
private void TriggerRightTurn()
{
if (bMain)
{
MRChange(ref MR_Main, iLightSubStatusEnum.Red);
for (int index = 1; index < MR_MainsStorage.Length; index++)
{
MRChange(ref MR_MainsStorage[index], iLightSubStatusEnum.Red);
}
LightChange(0, iLightSubStatusEnum.Red);
}
if (bLeft)
{
MRChange(ref MR_Left, iLightSubStatusEnum.Red);
LightChange(1, iLightSubStatusEnum.Red);
}
if (bRight)
{
MRChange(ref MR_Right, iLightSubStatus);
LightChange(2, iLightSubStatus);
}
}
private void TriggerRed()
{
if (bMain)
{
MRChange(ref MR_Main, iLightSubStatusEnum.Red);
for (int index = 1; index < MR_MainsStorage.Length; index++)
{
MRChange(ref MR_MainsStorage[index], iLightSubStatusEnum.Red);
}
LightChange(0, iLightSubStatusEnum.Red);
}
if (bLeft)
{
MRChange(ref MR_Left, iLightSubStatusEnum.Red);
LightChange(1, iLightSubStatusEnum.Red);
}
if (bRight)
{
MRChange(ref MR_Right, iLightSubStatusEnum.Red);
LightChange(2, iLightSubStatusEnum.Red);
}
}
#endregion
private void MRChange(ref MeshRenderer MR, iLightSubStatusEnum iLSSE)
{
if (bUseSharedMaterial)
{
if (iLSSE == iLightSubStatusEnum.Green)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.667f, 0f);
}
else if (iLSSE == iLightSubStatusEnum.Yellow)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.334f, 0f);
}
else if (iLSSE == iLightSubStatusEnum.Red)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0f, 0f);
}
}
else
{
if (iLSSE == iLightSubStatusEnum.Green)
{
MR.material.mainTextureOffset = new Vector2(0.667f, 0f);
}
else if (iLSSE == iLightSubStatusEnum.Yellow)
{
MR.material.mainTextureOffset = new Vector2(0.334f, 0f);
}
else if (iLSSE == iLightSubStatusEnum.Red)
{
MR.material.mainTextureOffset = new Vector2(0f, 0f);
}
}
}
private void MRChangeLeftYield(ref MeshRenderer MR, iLightYieldSubStatusEnum iLYSSE)
{
if (bUseSharedMaterial)
{
if (iLYSSE == iLightYieldSubStatusEnum.Green)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.667f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.Yellow)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.334f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.Red)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.YellowTurn)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.6f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.GreenTurn)
{
MR.sharedMaterial.mainTextureOffset = new Vector2(0.8f, 0f);
}
}
else
{
if (iLYSSE == iLightYieldSubStatusEnum.Green)
{
MR.material.mainTextureOffset = new Vector2(0.4f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.Yellow)
{
MR.material.mainTextureOffset = new Vector2(0.2f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.Red)
{
MR.material.mainTextureOffset = new Vector2(0f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.YellowTurn)
{
MR.material.mainTextureOffset = new Vector2(0.6f, 0f);
}
else if (iLYSSE == iLightYieldSubStatusEnum.GreenTurn)
{
MR.material.mainTextureOffset = new Vector2(0.8f, 0f);
}
}
}
private void LightChange(int tIndex, iLightSubStatusEnum iLSSE)
{
if (!bLightsEnabled)
{
int mCount = MR_MainsStorage.Length;
for (int index = 0; index < mCount; index++)
{
Lights_R[index].enabled = false;
Lights_Y[index].enabled = false;
Lights_G[index].enabled = false;
}
if (LightLeft_R != null)
{
LightLeft_R.enabled = false;
}
if (LightLeft_Y != null)
{
LightLeft_Y.enabled = false;
}
if (LightLeft_G != null)
{
LightLeft_G.enabled = false;
}
if (LightRight_R != null)
{
LightRight_R.enabled = false;
}
if (LightRight_Y != null)
{
LightRight_Y.enabled = false;
}
if (LightRight_G != null)
{
LightRight_G.enabled = false;
}
return;
}
if (tIndex == 0)
{
//Main:
int mCount = MR_MainsStorage.Length;
for (int index = 0; index < mCount; index++)
{
LightChangeHelper(ref Lights_R[index], ref Lights_Y[index], ref Lights_G[index], iLSSE);
}
}
else if (tIndex == 1)
{
//Left:
LightChangeHelper(ref LightLeft_R, ref LightLeft_Y, ref LightLeft_G, iLSSE);
}
else if (tIndex == 2)
{
//Right:
LightChangeHelper(ref LightRight_R, ref LightRight_Y, ref LightRight_G, iLSSE);
}
}
private void LightChangeHelper(ref Light tRed, ref Light tYellow, ref Light tGreen, iLightSubStatusEnum iLSSE)
{
if (iLSSE == iLightSubStatusEnum.Green)
{
tRed.enabled = false;
tYellow.enabled = false;
tGreen.enabled = true;
}
else if (iLSSE == iLightSubStatusEnum.Yellow)
{
tRed.enabled = false;
tYellow.enabled = true;
tGreen.enabled = false;
}
else if (iLSSE == iLightSubStatusEnum.Red)
{
tRed.enabled = true;
tYellow.enabled = false;
tGreen.enabled = false;
}
}
#region "Setup"
public void Setup(bool bLeftYield)
{
SetupObject(MR_Left);
SetupObject(MR_Right);
SetupMainObjects();
bLeft = (MR_Left != null);
bRight = (MR_Right != null);
bMain = (MR_Main != null);
bLeftTurnYieldOnGreen = bLeftYield;
}
private void SetupMainObjects()
{
if (MR_Main == null)
{
return;
}
int mCount = MR_MainsStorage.Length;
if (mCount == 0)
{
return;
}
SetupObject(MR_Main);
if (mCount > 1)
{
for (int index = 1; index < mCount; index++)
{
if (bUseSharedMaterial)
{
MR_MainsStorage[index].sharedMaterial = MR_Main.sharedMaterial;
}
else
{
Material[] materials = new Material[1];
materials[0] = MR_Main.materials[0];
MR_MainsStorage[index].materials = materials;
}
}
}
}
private void SetupObject(MeshRenderer MR)
{
if (MR != null)
{
MR.material = MR.material;
}
}
#endregion
}
public class GSDTrafficLightSequence
{
public bool bLightMasterPath1 = true;
public GSDTrafficLightController.iLightControllerEnum iLightController = GSDTrafficLightController.iLightControllerEnum.Regular;
public GSDTrafficLightController.iLightSubStatusEnum iLightSubcontroller = GSDTrafficLightController.iLightSubStatusEnum.Green;
public float tTime = 10f;
public GSDTrafficLightSequence(bool bPath1, GSDTrafficLightController.iLightControllerEnum tLightController, GSDTrafficLightController.iLightSubStatusEnum tLightSubcontroller, float xTime)
{
bLightMasterPath1 = bPath1;
iLightController = tLightController;
iLightSubcontroller = tLightSubcontroller;
tTime = xTime;
}
public string ToStringGSD()
{
return "Path1: " + bLightMasterPath1 + " iLightController: " + iLightController.ToString() + " iLightSubcontroller: " + iLightSubcontroller.ToString() + " tTime: " + tTime.ToString("0F");
}
}