| 1 | using System; |
| 2 | using System.Collections; |
| 3 | using System.Collections.Generic; |
| 4 | using UnityEngine; |
| 5 | |
| 6 | public class DNGN_02 : ModuleBase |
| 7 | { |
| 8 | [SerializeField] private Connector topWire, midWire, bottomWire, controlWire, topLight, midLight, bottomLight, controlButton; |
| 9 | |
| 10 | private bool topOn, midOn, bottomOn; |
| 11 | |
| 12 | private int colorindex = -1; |
| 13 | |
| 14 | private Color[] colors = new Color[] |
| 15 | { |
| 16 | Color.red, |
| 17 | Color.green, |
| 18 | Color.blue, |
| 19 | Color.yellow |
| 20 | }; |
| 21 | |
| 22 | protected override bool TestCorrectness() |
| 23 | { |
| 24 | LightComponent tLight = null; |
| 25 | LightComponent mLight = null; |
| 26 | LightComponent bLight = null; |
| 27 | |
| 28 | if (topLight.Connected != null && topLight.Connected is LightComponent) |
| 29 | { |
| 30 | tLight = (LightComponent)topLight.Connected; |
| 31 | } |
| 32 | else return false; |
| 33 | |
| 34 | if (midLight.Connected != null && midLight.Connected is LightComponent) |
| 35 | { |
| 36 | mLight = (LightComponent)midLight.Connected; |
| 37 | } |
| 38 | else return false; |
| 39 | |
| 40 | if (bottomLight.Connected != null && bottomLight.Connected is LightComponent) |
| 41 | { |
| 42 | bLight = (LightComponent)bottomLight.Connected; |
| 43 | } |
| 44 | else return false; |
| 45 | |
| 46 | switch (colorindex) |
| 47 | { |
| 48 | case 0: //red |
| 49 | if (!tLight.LightOn && mLight.LightOn && !bLight.LightOn) |
| 50 | return true; |
| 51 | break; |
| 52 | |
| 53 | case 1: //green |
| 54 | if (tLight.LightOn && !mLight.LightOn && bLight.LightOn) |
| 55 | return true; |
| 56 | break; |
| 57 | |
| 58 | case 2: //blue |
| 59 | if (tLight.LightOn && !mLight.LightOn && !bLight.LightOn) |
| 60 | return true; |
| 61 | break; |
| 62 | |
| 63 | case 3: //yellow |
| 64 | if (tLight.LightOn && mLight.LightOn && bLight.LightOn) |
| 65 | return true; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Start is called before the first frame update |
| 73 | void Start() |
| 74 | { |
| 75 | colorindex = UnityEngine.Random.Range(0, colors.Length); |
| 76 | |
| 77 | topLight.OnConnect = c => |
| 78 | { |
| 79 | if (c is LightComponent) |
| 80 | { |
| 81 | LightComponent light = (LightComponent)c; |
| 82 | |
| 83 | if (topOn) |
| 84 | light.TurnOn(colors[colorindex]); |
| 85 | else |
| 86 | light.TurnOff(); |
| 87 | } |
| 88 | else |
| 89 | EjectComponents(); |
| 90 | }; |
| 91 | |
| 92 | midLight.OnConnect = c => |
| 93 | { |
| 94 | if (c is LightComponent) |
| 95 | { |
| 96 | LightComponent light = (LightComponent)c; |
| 97 | |
| 98 | if (midOn) |
| 99 | light.TurnOn(colors[colorindex]); |
| 100 | else |
| 101 | light.TurnOff(); |
| 102 | } |
| 103 | else |
| 104 | EjectComponents(); |
| 105 | }; |
| 106 | |
| 107 | bottomLight.OnConnect = c => |
| 108 | { |
| 109 | if (c is LightComponent) |
| 110 | { |
| 111 | LightComponent light = (LightComponent)c; |
| 112 | |
| 113 | if (bottomOn) |
| 114 | light.TurnOn(colors[colorindex]); |
| 115 | else |
| 116 | light.TurnOff(); |
| 117 | } |
| 118 | else |
| 119 | EjectComponents(); |
| 120 | }; |
| 121 | |
| 122 | controlButton.OnConnect = c => |
| 123 | { |
| 124 | if (c is Pushbutton) |
| 125 | { |
| 126 | Pushbutton btn = (Pushbutton)c; |
| 127 | |
| 128 | btn.OnClick = () => |
| 129 | { |
| 130 | if (controlWire.Connected != null && controlWire.Connected is WireHead) |
| 131 | { |
| 132 | WireHead wire = (WireHead)controlWire.Connected; |
| 133 | |
| 134 | if(wire.MatchingWireHead.ConnectedConnector != null) |
| 135 | { |
| 136 | Connector connected = wire.MatchingWireHead.ConnectedConnector; |
| 137 | |
| 138 | bool correct = false; |
| 139 | |
| 140 | if(connected == topWire) |
| 141 | { |
| 142 | correct = true; |
| 143 | ToggleTop(); |
| 144 | } |
| 145 | |
| 146 | if(connected == midWire) |
| 147 | { |
| 148 | correct = true; |
| 149 | ToggleMid(); |
| 150 | } |
| 151 | |
| 152 | if(connected == bottomWire) |
| 153 | { |
| 154 | correct = true; |
| 155 | ToggleBottom(); |
| 156 | } |
| 157 | |
| 158 | if (!correct) |
| 159 | EjectComponents(); |
| 160 | } |
| 161 | else |
| 162 | EjectComponents(); |
| 163 | } |
| 164 | else |
| 165 | EjectComponents(); |
| 166 | }; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | controlButton.OnDisconnect = c => |
| 171 | { |
| 172 | if (c is Pushbutton) |
| 173 | { |
| 174 | Pushbutton btn = (Pushbutton)c; |
| 175 | |
| 176 | btn.OnClick = null; |
| 177 | } |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | private void ToggleTop() |
| 182 | { |
| 183 | topOn = !topOn; |
| 184 | |
| 185 | if (topLight.Connected && topLight.Connected is LightComponent) |
| 186 | { |
| 187 | if (topOn) |
| 188 | ((LightComponent)topLight.Connected).TurnOn(colors[colorindex]); |
| 189 | else |
| 190 | ((LightComponent)topLight.Connected).TurnOff(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private void ToggleMid() |
| 195 | { |
| 196 | midOn = !midOn; |
| 197 | |
| 198 | if (midLight.Connected && midLight.Connected is LightComponent) |
| 199 | { |
| 200 | if (midOn) |
| 201 | ((LightComponent)midLight.Connected).TurnOn(colors[colorindex]); |
| 202 | else |
| 203 | ((LightComponent)midLight.Connected).TurnOff(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private void ToggleBottom() |
| 208 | { |
| 209 | bottomOn = !bottomOn; |
| 210 | |
| 211 | if (bottomLight.Connected && bottomLight.Connected is LightComponent) |
| 212 | { |
| 213 | if (bottomOn) |
| 214 | ((LightComponent)bottomLight.Connected).TurnOn(colors[colorindex]); |
| 215 | else |
| 216 | ((LightComponent)bottomLight.Connected).TurnOff(); |
| 217 | } |
| 218 | } |
| 219 | } |