This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathGSDTrafficLightController.cs
375 lines (347 loc) · 11.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
using UnityEngine;
using System.Collections;
[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;
bool bLeft = false;
bool bRight = false;
bool bMain = false;
bool bUseSharedMaterial = false;
bool bLeftTurnYieldOnGreen = true;
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 i=0;i<mCount;i++){
tLights = LightsObj[i].transform.GetComponentsInChildren<Light>();
foreach(Light tLight in tLights){
if(tLight.transform.name.ToLower().Contains("redlight")){
Lights_R[i] = tLight;
}
if(tLight.transform.name.ToLower().Contains("yellowlight")){
Lights_Y[i] = tLight;
}
if(tLight.transform.name.ToLower().Contains("greenl")){
Lights_G[i] = 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);
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);
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);
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);
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);
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 i=0;i<mCount;i++){
Lights_R[i].enabled = false;
Lights_Y[i].enabled = false;
Lights_G[i].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 i=0;i<mCount;i++){
LightChangeHelper(ref Lights_R[i],ref Lights_Y[i],ref Lights_G[i],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 i=1;i<mCount;i++){
if(bUseSharedMaterial){
MR_MainsStorage[i].sharedMaterial = MR_Main.sharedMaterial;
}else{
MR_MainsStorage[i].materials = new Material[1];
MR_MainsStorage[i].materials[0] = MR_Main.materials[0];
}
}
}
}
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");
}
}