-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlloySmelter.cs
356 lines (340 loc) · 13.1 KB
/
AlloySmelter.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using UnityEngine;
public class AlloySmelter : Machine
{
public int speed = 1;
public string ID = "unassigned";
public string creationMethod = "built";
public int power;
public int heat;
public int cooling;
public float amount;
public float amount2;
public float outputAmount;
public string inputType1;
public string inputType2;
public string outputType;
public string inputID1;
public string inputID2;
public string outputID;
public PowerReceiver powerReceiver;
public GameObject fireObject;
public GameObject inputObject1;
public GameObject inputObject2;
public GameObject outputObject;
public GameObject powerObject;
public ConduitItem conduitItem;
public Material lineMat;
public bool hasHeatExchanger;
public bool powerON;
public bool connectionFailed;
public int connectionAttempts;
private LineRenderer connectionLine;
private int machineTimer;
private int warmup;
private GameObject builtObjects;
private StateManager stateManager;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
powerReceiver = gameObject.AddComponent<PowerReceiver>();
connectionLine = gameObject.AddComponent<LineRenderer>();
stateManager = FindObjectOfType<StateManager>();
connectionLine.startWidth = 0.2f;
connectionLine.endWidth = 0.2f;
connectionLine.material = lineMat;
connectionLine.loop = true;
connectionLine.enabled = false;
builtObjects = GameObject.Find("BuiltObjects");
conduitItem = GetComponentInChildren<ConduitItem>(true);
}
//! Called by MachineManager update coroutine.
public override void UpdateMachine()
{
if (ID == "unassigned" || stateManager.initMachines == false)
return;
GetComponent<PhysicsHandler>().UpdatePhysics();
UpdatePowerReceiver();
if (warmup < 10)
{
warmup++;
}
else if (speed > power)
{
speed = power > 0 ? power : 1;
}
if (speed > 1)
{
heat = speed - 1 - cooling;
}
else
{
heat = 0;
}
if (heat < 0)
{
heat = 0;
}
if (GetComponent<AudioSource>().isPlaying == false)
{
fireObject.SetActive(false);
}
if (inputObject1 == null || inputObject2 == null || outputObject == null)
{
connectionAttempts += 1;
if (creationMethod.Equals("spawned"))
{
if (connectionAttempts >= 128)
{
connectionAttempts = 0;
connectionFailed = true;
}
}
else
{
if (connectionAttempts >= 512)
{
connectionAttempts = 0;
connectionFailed = true;
}
}
if (connectionFailed == false)
{
GetOutputConduit();
}
}
if (inputObject1 != null && inputObject2 != null)
{
SmeltAlloy();
}
else
{
conduitItem.active = false;
}
OutputToConduit();
if (inputObject1 != null && inputObject2 != null && outputObject != null)
{
connectionAttempts = 0;
}
}
//! The object is a potential output connection.
private bool IsValidOutputObject(GameObject obj)
{
return outputObject == null && inputObject1 != null && inputObject2 != null && obj != inputObject1 && obj != inputObject2 && obj != gameObject;
}
//! Finds a universal conduit to use as an output.
private void GetOutputConduit()
{
GameObject[] allObjects = GameObject.FindGameObjectsWithTag("Machine");
foreach (GameObject obj in allObjects)
{
if (obj != null)
{
if (obj.GetComponent<UniversalConduit>() != null)
{
float distance = Vector3.Distance(transform.position, obj.transform.position);
if (IsValidOutputObject(obj) && distance < 20)
{
if (obj.GetComponent<UniversalConduit>().inputObject == null)
{
if (creationMethod.Equals("spawned") && obj.GetComponent<UniversalConduit>().ID.Equals(outputID))
{
outputObject = obj;
obj.GetComponent<UniversalConduit>().type = outputType;
obj.GetComponent<UniversalConduit>().inputObject = gameObject;
connectionLine.SetPosition(0, transform.position);
connectionLine.SetPosition(1, obj.transform.position);
connectionLine.enabled = true;
creationMethod = "built";
break;
}
else if (creationMethod.Equals("built"))
{
outputObject = obj;
obj.GetComponent<UniversalConduit>().type = outputType;
obj.GetComponent<UniversalConduit>().inputObject = gameObject;
connectionLine.SetPosition(0, transform.position);
connectionLine.SetPosition(1, obj.transform.position);
connectionLine.enabled = true;
break;
}
}
}
}
}
}
}
//! Consumes input items and produces an item for output.
private void SmeltAlloy()
{
if (inputObject1.GetComponent<UniversalConduit>() != null && inputObject2.GetComponent<UniversalConduit>() != null)
{
if (amount < 1)
{
if (inputObject1.GetComponent<UniversalConduit>().type != "" && inputObject1.GetComponent<UniversalConduit>().type != "nothing")
inputType1 = inputObject1.GetComponent<UniversalConduit>().type;
}
if (amount2 < 1)
{
if (inputObject2.GetComponent<UniversalConduit>().type != "" && inputObject2.GetComponent<UniversalConduit>().type != "nothing")
inputType2 = inputObject2.GetComponent<UniversalConduit>().type;
}
if (inputObject1.GetComponent<UniversalConduit>().type.Equals("Copper Ingot") && inputObject2.GetComponent<UniversalConduit>().type.Equals("Tin Ingot"))
{
outputType = "Bronze Ingot";
if (amount >= speed && amount2 >= speed)
{
if (powerON == true && connectionFailed == false && speed > 0)
{
outputAmount += speed - heat;
amount -= speed - heat;
amount2 -= speed - heat;
fireObject.SetActive(true);
GetComponent<AudioSource>().enabled = true;
}
else
{
fireObject.SetActive(false);
GetComponent<AudioSource>().enabled = false;
}
}
}
else if (inputObject1.GetComponent<UniversalConduit>().type.Equals("Tin Ingot") && inputObject2.GetComponent<UniversalConduit>().type.Equals("Copper Ingot"))
{
outputType = "Bronze Ingot";
if (amount >= speed && amount2 >= speed)
{
if (powerON == true && connectionFailed == false && speed > 0)
{
outputAmount += speed - heat;
amount -= speed - heat;
amount2 -= speed - heat;
fireObject.SetActive(true);
GetComponent<AudioSource>().enabled = true;
}
else
{
fireObject.SetActive(false);
GetComponent<AudioSource>().enabled = false;
}
}
}
else if (inputObject1.GetComponent<UniversalConduit>().type.Equals("Iron Ingot") && inputObject2.GetComponent<UniversalConduit>().type.Equals("Coal"))
{
outputType = "Steel Ingot";
if (amount >= speed && amount2 >= speed)
{
if (powerON == true && connectionFailed == false && speed > 0)
{
outputAmount += speed - heat;
amount -= speed - heat;
amount2 -= speed - heat;
fireObject.SetActive(true);
GetComponent<AudioSource>().enabled = true;
}
else
{
fireObject.SetActive(false);
GetComponent<AudioSource>().enabled = false;
}
}
}
else if (inputObject1.GetComponent<UniversalConduit>().type.Equals("Coal") && inputObject2.GetComponent<UniversalConduit>().type.Equals("Iron Ingot"))
{
outputType = "Steel Ingot";
if (amount >= speed && amount2 >= speed)
{
if (powerON == true && connectionFailed == false && speed > 0)
{
outputAmount += speed - heat;
amount -= speed - heat;
amount2 -= speed - heat;
fireObject.SetActive(true);
GetComponent<AudioSource>().enabled = true;
}
else
{
fireObject.SetActive(false);
GetComponent<AudioSource>().enabled = false;
}
}
}
if (inputObject1.GetComponent<UniversalConduit>().conduitItem.active == false)
{
conduitItem.active = false;
}
if (inputObject2.GetComponent<UniversalConduit>().conduitItem.active == false)
{
conduitItem.active = false;
}
}
}
//! Items are moved to output conduit, sounds and special effects are created.
private void OutputToConduit()
{
if (outputObject != null)
{
if (outputObject.GetComponent<UniversalConduit>() != null)
{
outputObject.GetComponent<UniversalConduit>().inputID = ID;
outputObject.GetComponent<UniversalConduit>().type = outputType;
outputObject.GetComponent<UniversalConduit>().speed = speed;
outputID = outputObject.GetComponent<UniversalConduit>().ID;
if (outputAmount >= speed)
{
if (outputType.Equals(outputObject.GetComponent<UniversalConduit>().type))
{
if (powerON == true && connectionFailed == false && inputObject1 != null && inputObject2 != null && speed > 0)
{
conduitItem.active = true;
if (GetComponent<AudioSource>().isPlaying == false)
{
GetComponent<AudioSource>().Play();
fireObject.SetActive(true);
}
machineTimer += 1;
if (machineTimer > 5)
{
outputObject.GetComponent<UniversalConduit>().amount += speed - heat;
outputAmount -= speed;
machineTimer = 0;
}
}
else
{
conduitItem.active = false;
machineTimer = 0;
}
}
else
{
conduitItem.active = false;
}
}
}
else
{
conduitItem.active = false;
}
}
else
{
conduitItem.active = false;
connectionLine.enabled = false;
if (connectionFailed == true)
{
if (creationMethod.Equals("spawned"))
{
creationMethod = "built";
}
}
}
}
//! Gets power values from power receiver.
private void UpdatePowerReceiver()
{
powerReceiver.ID = ID;
power = powerReceiver.power;
powerON = powerReceiver.powerON;
powerObject = powerReceiver.powerObject;
}
}