-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rocket.cs
215 lines (196 loc) · 6.23 KB
/
Rocket.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
using UnityEngine;
public class Rocket : MonoBehaviour
{
public GameObject exhaust;
public int payloadRequired = 25;
public int day = 1;
private bool ascending;
private bool visible;
public bool landed;
public float gameTime;
private float liftOffTimer;
private PlayerController player;
private StateManager stateManager;
private bool initialized;
public bool rocketRequested;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
player = GameObject.Find("Player").GetComponent<PlayerController>();
stateManager = GameObject.Find("GameManager").GetComponent<StateManager>();
}
//! Called once per frame by unity engine.
public void Update()
{
if (!player.stateManager.Busy())
{
if (initialized == false)
{
Init();
}
gameTime += 1 * Time.deltaTime;
FileBasedPrefs.SetFloat(stateManager.worldName + "gameTime", gameTime);
if (gameTime >= 2400)
{
EndOfDay();
}
if (landed == false && (player.timeToDeliver == true || rocketRequested == true))
{
Descend();
}
if (landed == true)
{
liftOffTimer += 1 * Time.deltaTime;
if (player.timeToDeliver == true)
{
CheckCargo();
}
else if (liftOffTimer >= 600 && ascending == false)
{
ascending = true;
}
if (ascending == true)
{
if (transform.position.y < 2000)
{
Ascend();
}
else
{
Reset();
}
}
}
}
}
//! Initializes variables when the world is loaded.
private void Init()
{;
if (FileBasedPrefs.GetInt(stateManager.worldName + "payloadRequired") != 0)
{
payloadRequired = FileBasedPrefs.GetInt(stateManager.worldName + "payloadRequired");
}
if (FileBasedPrefs.GetInt(stateManager.worldName + "day") != 0)
{
day = FileBasedPrefs.GetInt(stateManager.worldName + "day");
}
gameTime = FileBasedPrefs.GetFloat(stateManager.worldName + "gameTime");
initialized = true;
}
//! Checks the cargo of the rocket for the required amount of dark matter.
private void CheckCargo()
{
int amountInRocket = 0;
foreach (InventorySlot slot in GetComponent<InventoryManager>().inventory)
{
if (slot.amountInSlot > 0 && slot.typeInSlot == "Dark Matter")
{
amountInRocket += slot.amountInSlot;
}
}
if (amountInRocket >= payloadRequired && ascending == false)
{
player.money += 1000;
FileBasedPrefs.SetInt(stateManager.worldName + "money", player.money);
ascending = true;
}
else if (liftOffTimer >= 600 && ascending == false)
{
player.money -= 500;
FileBasedPrefs.SetInt(stateManager.worldName + "money", player.money);
ascending = true;
}
}
//! Called at the end of a day to increase the payload required for the next day.
private void EndOfDay()
{
day += 1;
gameTime = 0;
if (payloadRequired <= 800000)
{
payloadRequired = payloadRequired * 2;
}
FileBasedPrefs.SetInt(stateManager.worldName + "day", day);
FileBasedPrefs.SetInt(stateManager.worldName + "payloadRequired", payloadRequired);
player.timeToDeliver = true;
}
//! Resets the rocket when it leaves the area, disabling all sounds and rendering and clearing the inventory.
private void Reset()
{
ClearInventory();
DisableRenderers();
liftOffTimer = 0;
landed = false;
player.timeToDeliver = false;
rocketRequested = false;
ascending = false;
GetComponent<AudioSource>().enabled = false;
exhaust.SetActive(false);
}
//! Moves the rocket in the positive Y direction.
private void Ascend()
{
GetComponent<AudioSource>().enabled = true;
exhaust.SetActive(true);
transform.position += transform.up * 25 * Time.deltaTime;
}
//! Moves the rocket in the negative Y direction.
private void Descend()
{
if (visible == false)
{
EnableRenderers();
}
if (Physics.Raycast(transform.position, -transform.up, out RaycastHit hit, 0.5f) || transform.position.y <= -66)
{
landed = true;
GetComponent<AudioSource>().enabled = false;
exhaust.SetActive(false);
}
else
{
GetComponent<AudioSource>().enabled = true;
exhaust.SetActive(true);
transform.position -= transform.up * 25 * Time.deltaTime;
}
}
//! Empty the rocket's inventory.
private void ClearInventory()
{
foreach (InventorySlot s in GetComponent<InventoryManager>().inventory)
{
s.typeInSlot = "nothing";
s.amountInSlot = 0;
}
GetComponent<InventoryManager>().SaveData();
}
//! Disable all renderers attached to the rocket, so it is no longer visible.
private void DisableRenderers()
{
if (visible == true)
{
Renderer[] renderers = GetComponentsInChildren<Renderer>(true);
foreach (Renderer r in renderers)
{
if (r.enabled == true)
{
r.enabled = false;
}
}
visible = false;
}
}
//! Enabled all renderers attached to the rocket, making it visible.
private void EnableRenderers()
{
Renderer[] renderers = GetComponentsInChildren<Renderer>(true);
foreach (Renderer r in renderers)
{
if (r.enabled == false)
{
r.enabled = true;
}
}
visible = true;
}
}