BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / GAME / Scripts / Modules / SENSE_A.cs

SENSE_A.cs

f93b9f9e History
SENSE_A.cs 5.1 KiB Download Raw
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5
6public class SENSE_A : ModuleBase
7{
8 [SerializeField] private Connector leftBulb, midBulb, rightBulb, leftButton, midbutton, rightButton;
9
10 private LightComponent lB = null;
11 private LightComponent mB = null;
12 private LightComponent rB = null;
13
14 private bool canPush
15 {
16 get
17 {
18 return mB != null && rB != null && lB != null;
19 }
20 }
21
22 protected override bool TestCorrectness()
23 {
24 if (leftBulb.Connected == null || !(leftBulb.Connected is LightComponent) || !((LightComponent)leftBulb.Connected).LightOn)
25 return false;
26
27 if (midBulb.Connected == null || !(midBulb.Connected is LightComponent) || !((LightComponent)midBulb.Connected).LightOn)
28 return false;
29
30 if (rightBulb.Connected == null || !(rightBulb.Connected is LightComponent) || !((LightComponent)rightBulb.Connected).LightOn)
31 return false;
32
33 return true;
34 }
35
36 // Start is called before the first frame update
37 void Start()
38 {
39 mB = null;
40 rB = null;
41 lB = null;
42
43 if (leftBulb.Connected != null && leftBulb.Connected is LightComponent)
44 lB = (LightComponent)leftBulb.Connected;
45
46 if (midBulb.Connected != null && midBulb.Connected is LightComponent)
47 mB = (LightComponent)midBulb.Connected;
48
49 if (rightBulb.Connected != null && rightBulb.Connected is LightComponent)
50 rB = (LightComponent)rightBulb.Connected;
51
52 leftBulb.OnConnect = c =>
53 {
54 if (c is LightComponent)
55 {
56 if (UnityEngine.Random.value > 0.6f)
57 {
58 ((LightComponent)c).TurnOn(Color.yellow);
59 }
60 }
61 else
62 EjectComponents();
63 };
64
65 midBulb.OnConnect = c =>
66 {
67 if (c is LightComponent)
68 {
69 if (UnityEngine.Random.value > 0.6f)
70 {
71 ((LightComponent)c).TurnOn(Color.yellow);
72 }
73 }
74 else
75 EjectComponents();
76 };
77
78 rightBulb.OnConnect = c =>
79 {
80 if (c is LightComponent)
81 {
82 if (UnityEngine.Random.value > 0.6f)
83 {
84 ((LightComponent)c).TurnOn(Color.yellow);
85 }
86 }
87 else
88 EjectComponents();
89 };
90
91 leftBulb.OnDisconnect = c =>
92 {
93 EjectComponents();
94 };
95
96 midBulb.OnDisconnect = c =>
97 {
98 EjectComponents();
99 };
100
101 rightBulb.OnDisconnect = c =>
102 {
103 EjectComponents();
104 };
105
106 leftButton.OnConnect = c =>
107 {
108 if (c is Pushbutton)
109 {
110 Pushbutton btn = (Pushbutton)c;
111
112 btn.OnClick = () =>
113 {
114 if(canPush)
115 {
116 ToggleLight(lB);
117 ToggleLight(mB);
118 }
119 else
120 EjectComponents();
121 };
122 }
123 else
124 EjectComponents();
125 };
126
127 leftButton.OnDisconnect = c =>
128 {
129 if (c is Pushbutton)
130 {
131 Pushbutton btn = (Pushbutton)c;
132
133 btn.OnClick = null;
134 }
135 };
136 //
137 midbutton.OnConnect = c =>
138 {
139 if (c is Pushbutton)
140 {
141 Pushbutton btn = (Pushbutton)c;
142
143 btn.OnClick = () =>
144 {
145 if (canPush)
146 {
147 ToggleLight(rB);
148 ToggleLight(mB);
149 ToggleLight(lB);
150 }
151 else
152 EjectComponents();
153 };
154 }
155 else
156 EjectComponents();
157 };
158
159 midbutton.OnDisconnect = c =>
160 {
161 if (c is Pushbutton)
162 {
163 Pushbutton btn = (Pushbutton)c;
164
165 btn.OnClick = null;
166 }
167 };
168 //
169 rightButton.OnConnect = c =>
170 {
171 if (c is Pushbutton)
172 {
173 Pushbutton btn = (Pushbutton)c;
174
175 btn.OnClick = () =>
176 {
177 if (canPush)
178 {
179 ToggleLight(rB);
180 ToggleLight(mB);
181 }
182 else
183 EjectComponents();
184 };
185 }
186 else
187 EjectComponents();
188 };
189
190 rightButton.OnDisconnect = c =>
191 {
192 if (c is Pushbutton)
193 {
194 Pushbutton btn = (Pushbutton)c;
195
196 btn.OnClick = null;
197 }
198 };
199 }
200
201 private void ToggleLight(LightComponent light)
202 {
203 if (light.LightOn)
204 light.TurnOff();
205 else
206 light.TurnOn(Color.yellow);
207 }
208}