-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkMatterCollector.cs
190 lines (178 loc) · 5.77 KB
/
DarkMatterCollector.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
using UnityEngine;
public class DarkMatterCollector : Machine
{
public float darkMatterAmount;
public int speed = 1;
public int power;
public int heat;
public bool hasHeatExchanger;
public int cooling;
public GameObject outputObject;
public GameObject powerObject;
public ConduitItem conduitItem;
public Material lineMat;
public string ID = "unassigned";
public string creationMethod = "built";
public PowerReceiver powerReceiver;
private LineRenderer connectionLine;
private LineRenderer inputLine;
public bool powerON;
private int machineTimer;
public int connectionAttempts;
public bool connectionFailed;
public bool foundDarkMatter;
private GameObject builtObjects;
private StateManager stateManager;
//! Called by unity engine on start up to initialize variables.
private void Start()
{
connectionLine = gameObject.AddComponent<LineRenderer>();
powerReceiver = gameObject.AddComponent<PowerReceiver>();
conduitItem = GetComponentInChildren<ConduitItem>(true);
connectionLine.startWidth = 0.2f;
connectionLine.endWidth = 0.2f;
connectionLine.material = lineMat;
connectionLine.loop = true;
connectionLine.enabled = false;
builtObjects = GameObject.Find("BuiltObjects");
stateManager = FindObjectOfType<StateManager>();
}
//! Called by MachineManager update coroutine.
public override void UpdateMachine()
{
if (ID == "unassigned" || stateManager.initMachines == false)
return;
GetComponent<PhysicsHandler>().UpdatePhysics();
UpdatePowerReceiver();
if (speed > power && power != 0)
{
speed = power;
}
if (speed > 1)
{
heat = speed - 1 - cooling;
}
else
{
heat = 0;
}
if (heat < 0)
{
heat = 0;
}
if (outputObject != null)
{
connectionLine.SetPosition(0, transform.position);
connectionLine.SetPosition(1, outputObject.transform.position);
connectionLine.enabled = true;
}
else
{
connectionLine.enabled = false;
}
if (foundDarkMatter == true)
{
if (powerON == true && connectionFailed == false && speed > 0)
{
conduitItem.active = connectionLine.enabled;
GetComponent<Light>().enabled = true;
GetComponent<AudioSource>().enabled = true;
machineTimer += 1;
if (machineTimer > 5)
{
darkMatterAmount += speed - heat;
machineTimer = 0;
}
}
else
{
machineTimer = 0;
conduitItem.active = false;
GetComponent<Light>().enabled = false;
GetComponent<AudioSource>().enabled = false;
}
}
if (foundDarkMatter == false)
{
connectionAttempts += 1;
if (creationMethod.Equals("spawned"))
{
if (connectionAttempts >= 128)
{
connectionAttempts = 0;
connectionFailed = true;
}
}
else
{
if (connectionAttempts >= 512)
{
connectionAttempts = 0;
connectionFailed = true;
}
}
if (connectionFailed == false)
{
FindDarkMatter();
}
}
}
//! Used to remove the connection line renderer when the block is destroyed.
public void OnDestroy()
{
if (inputLine != null)
{
Destroy(inputLine);
}
}
//! The object exists, is active and is not a standard building block.
bool IsValidObject(GameObject obj)
{
if (obj != null)
{
return obj.transform.parent != builtObjects.transform && obj.activeInHierarchy;
}
return false;
}
//! Finds a dark matter source node to harvest.
private void FindDarkMatter()
{
DarkMatter[] allDarkMatter = FindObjectsOfType<DarkMatter>();
foreach (DarkMatter d in allDarkMatter)
{
GameObject obj = d.gameObject;
if (IsValidObject(obj))
{
float distance = Vector3.Distance(transform.position, obj.transform.position);
if (distance < 20)
{
if (obj.GetComponent<DarkMatter>().collector == null)
{
obj.GetComponent<DarkMatter>().collector = gameObject;
}
if (obj.GetComponent<DarkMatter>().collector == gameObject)
{
if (inputLine == null && obj.GetComponent<LineRenderer>() == null)
{
inputLine = obj.AddComponent<LineRenderer>();
inputLine.startWidth = 0.2f;
inputLine.endWidth = 0.2f;
inputLine.material = lineMat;
inputLine.SetPosition(0, transform.position);
inputLine.SetPosition(1, obj.transform.position);
}
foundDarkMatter = true;
}
}
}
}
}
//! Gets power values from power receiver.
private void UpdatePowerReceiver()
{
powerReceiver.ID = ID;
power = powerReceiver.power;
powerON = powerReceiver.powerON;
powerObject = powerReceiver.powerObject;
}
}