-
Notifications
You must be signed in to change notification settings - Fork 0
/
Powered_Component.cs
134 lines (119 loc) · 4.43 KB
/
Powered_Component.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Script handling power states of powered components
//Read from this script to toggle behavior of those components
//
//Created 11/22/2019 by A. Jayasinghe
// Edited Control Panel name
// Edited 5/27/2020 by M. Justice
public class Powered_Component : MonoBehaviour
{
//Lights indicating the state of each powered object
public GameObject[] attachedLights;
//The object responsible for powering this component
public GameObject[] inputObjects;
bool _powered = false;
// Update is called once per frame
void Update()
{
//Set the _powered boolean based on the input's status
if (!_powered)
{
_powered = CheckInputs();
}
}
//Method allowing a caller to set the power status of
//this component
public void SetPowerStatus(bool state)
{
_powered = state;
}
//Method allowing a caller to get the power status of
//this component
public bool GetPowerStatus()
{
return _powered;
}
//Method to change the color of the lights attached to this object
void ChangeLights(int index, bool state)
{
//foreach (GameObject light in attachedLights)
//{
// Light_States lightScript = light.GetComponent<Light_States>();
// lightScript.SetPowerStatus(state);
//}
//Bound check to make sure the light index exists
if(index >= 0 && index < attachedLights.Length)
{
//Swap the appropriate light's power state
Light_States lightScript = attachedLights[index].GetComponent<Light_States>();
lightScript.SetPowerStatus(state);
//Debug.Log("Swapping light material to " + state);
}
}
//Method to check the power from whatever input is attached to this component
//such as a switch or the control panel
bool CheckInputs()
{
int poweredCount = 0;
for(int i = 0; i < inputObjects.Length; ++i)
{
GameObject obj = inputObjects[i];
switch (obj.tag)
{
case "Control Panel":
FuseBox_Puzzle panelScript = obj.GetComponent<FuseBox_Puzzle>();
if (panelScript.GetPowerState())
{
ChangeLights(i, true);
poweredCount++;
}
break;
case "Keycard Reader":
Read_Keycard keycardScript = obj.GetComponent<Read_Keycard>();
//Flip the state of the "IsLocked" method for consistency
bool _keyCardReaderUnlocked = !keycardScript.IsLocked();
if (_keyCardReaderUnlocked)
{
ChangeLights(i, true);
poweredCount++;
}
break;
case "Codelock":
Read_Code codeLockScript = obj.GetComponent<Read_Code>();
if (codeLockScript.IsUnlocked())
{
ChangeLights(i, true);
poweredCount++;
}
break;
case "Power Switch":
Power_Switch switchScript = obj.GetComponent<Power_Switch>();
if (switchScript.getActiveState())
{
//Debug.Log("Switch flipped");
ChangeLights(i, true);
poweredCount++;
}
else
{
ChangeLights(i, false);
poweredCount--;
}
break;
case "Generator Switch":
Generator_Switch genSwitchScript = obj.GetComponent<Generator_Switch>();
if (genSwitchScript.IsFlipped())
{
ChangeLights(i, true);
poweredCount++;
}
break;
default:
throw new UnityException("Invalid input object attached to " + gameObject.name);
}
}
return poweredCount == inputObjects.Length;
}
}