-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageComputer.cs
166 lines (156 loc) · 5.7 KB
/
StorageComputer.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
using System.Collections.Generic;
using UnityEngine;
public class StorageComputer : Machine
{
public InventoryManager[] computerContainers;
private List<InventoryManager> computerContainerList;
private int connectionAttempts;
public string ID = "unassigned";
public bool powerON;
public int bootTimer;
public bool initialized;
private StateManager stateManager;
private List<GameObject> spawnedConnectionList;
public GameObject connectionObject;
public Material lineMat;
public GameObject powerObject;
public ConduitItem conduitItem;
public PowerReceiver powerReceiver;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
stateManager = FindObjectOfType<StateManager>();
powerReceiver = gameObject.AddComponent<PowerReceiver>();
computerContainerList = new List<InventoryManager>();
spawnedConnectionList = new List<GameObject>();
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 (powerON == true)
{
if (initialized == false)
{
bootTimer++;
if (bootTimer >= 5)
{
GetContainers();
bool foundContainer = false;
int containerCount = 0;
foreach (InventoryManager manager in computerContainers)
{
foundContainer |= computerContainers[containerCount] != null;
containerCount++;
}
if (foundContainer == false)
{
Reboot();
connectionAttempts++;
initialized |= connectionAttempts >= 128;
}
else
{
initialized = true;
}
bootTimer = 0;
}
}
}
else
{
initialized = false;
bootTimer = 0;
GameObject[] spawnedConnections = spawnedConnectionList.ToArray();
foreach (GameObject obj in spawnedConnections)
{
if (obj != null)
{
obj.SetActive(false);
}
}
}
}
//! Called when player interacts with the computer.
public void GetContainers()
{
computerContainerList.Clear();
GameObject[] spawnedConnections = spawnedConnectionList.ToArray();
foreach (GameObject obj in spawnedConnections)
{
if (obj != null)
{
obj.SetActive(false);
}
}
InventoryManager[] allContainers = FindObjectsOfType<InventoryManager>();
foreach (InventoryManager container in allContainers)
{
GameObject containerObject = container.gameObject;
Transform containerTransform = containerObject.transform;
float distance = Vector3.Distance(transform.position, containerObject.transform.position);
if (IsValidContainer(containerObject) && distance <= 40)
{
computerContainerList.Add(container);
GameObject spawnedConnection = Instantiate(connectionObject, containerTransform.position, containerTransform.rotation);
spawnedConnection.transform.parent = containerObject.transform;
spawnedConnection.SetActive(true);
LineRenderer inputLine = spawnedConnection.AddComponent<LineRenderer>();
inputLine.startWidth = 0.2f;
inputLine.endWidth = 0.2f;
inputLine.material = lineMat;
inputLine.SetPosition(0, transform.position);
inputLine.SetPosition(1, containerObject.transform.position);
spawnedConnectionList.Add(spawnedConnection);
}
}
computerContainers = computerContainerList.ToArray();
}
//! Returns true if the computer can access the container.
private bool IsValidContainer(GameObject obj)
{
return obj.GetComponent<InventoryManager>().initialized == true
&& obj.GetComponent<RailCart>() == null
&& obj.GetComponent<Retriever>() == null
&& obj.GetComponent<AutoCrafter>() == null
&& obj.GetComponent<InventoryManager>().ID != "player"
&& obj.GetComponent<InventoryManager>().ID != "Rocket";
}
//! Gets power values from power receiver.
private void UpdatePowerReceiver()
{
powerReceiver.ID = ID;
powerON = powerReceiver.powerON;
powerObject = powerReceiver.powerObject;
}
//! Removes all connections and allows the computer to search for storage containers.
public void Reboot()
{
GameObject[] spawnedConnections = spawnedConnectionList.ToArray();
foreach (GameObject obj in spawnedConnections)
{
if (obj != null)
{
obj.SetActive(false);
}
}
initialized = false;
bootTimer = 0;
}
//! Removes line renderers when the machine is destroyed.
public void OnDestroy()
{
GameObject[] spawnedConnections = spawnedConnectionList.ToArray();
foreach (GameObject obj in spawnedConnections)
{
if (obj != null)
{
obj.SetActive(false);
}
}
}
}