BlueGit

blue-star ggj-2020

Files

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

SENSE_A.cs

Jam end
timfa Tim Falken · pushed by timfa · 6 years ago
f9387e84 History
SENSE_A.cs 2.3 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 bulbConnector, buttonConnector;
9
10 private bool pressedCorrectly = false;
11 private float offset;
12
13 protected override bool TestCorrectness()
14 {
15 return pressedCorrectly;
16 }
17
18 // Start is called before the first frame update
19 void Start()
20 {
21 offset = UnityEngine.Random.value * 10;
22
23 bulbConnector.OnConnect = c =>
24 {
25 if (c is LightComponent)
26 {
27 ((LightComponent)c).TurnOn(Color.blue);
28 }
29 else
30 EjectComponents();
31 };
32
33 buttonConnector.OnConnect = c =>
34 {
35 if (c is Pushbutton)
36 {
37 Pushbutton btn = (Pushbutton)c;
38
39 btn.OnClick = () =>
40 {
41 LightComponent bulb = null;
42
43 if (bulbConnector.Connected != null && bulbConnector.Connected is LightComponent)
44 bulb = (LightComponent)bulbConnector.Connected;
45
46 if (bulb != null && bulb.LightOn)
47 {
48 pressedCorrectly = true;
49 bulb.TurnOn(Color.green);
50 }
51 else
52 EjectComponents();
53 };
54 }
55 else
56 EjectComponents();
57 };
58
59 buttonConnector.OnDisconnect = c =>
60 {
61 if (c is Pushbutton)
62 {
63 Pushbutton btn = (Pushbutton)c;
64
65 btn.OnClick = null;
66 }
67 };
68 }
69
70 void Update()
71 {
72 if (!pressedCorrectly)
73 {
74 LightComponent bulb = null;
75
76 if (bulbConnector.Connected != null && bulbConnector.Connected is LightComponent)
77 bulb = (LightComponent)bulbConnector.Connected;
78
79 if (bulb.LightOn)
80 {
81 if (Mathf.Sin((Time.realtimeSinceStartup + offset) * 3) <= 0)
82 bulb.TurnOff();
83 }
84 else
85 {
86 if (Mathf.Sin((Time.realtimeSinceStartup + offset) * 3) > 0)
87 bulb.TurnOn(Color.red);
88 }
89 }
90 }
91}