-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
336 lines (307 loc) · 12.5 KB
/
GameManager.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
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using System.Diagnostics;
using System;
using System.Net;
public class GameManager : MonoBehaviour
{
public bool fileBasedPrefsInitialized;
private HazardManager hazardManager;
public CombinedMeshManager meshManager;
public GameObject pirateObject;
public GameObject meteorObject;
public GameObject[] ironHolders;
public GameObject[] glassHolders;
public GameObject[] steelHolders;
public GameObject[] brickHolders;
public List<string> modBlockNames;
public List<GameObject[]> modBlockHolders;
public GameObject ironHolder;
public GameObject glassHolder;
public GameObject steelHolder;
public GameObject brickHolder;
public GameObject modBlockHolder;
public GameObject lander;
public GameObject rocketObject;
public GameObject builtObjects;
public Material glassMaterial;
public int chunkSize;
public float simulationSpeed;
public bool blockPhysics;
public bool hazardsEnabled = true;
public float meteorTimer;
public float pirateTimer;
public bool dataSaveRequested;
public bool blocksCombined;
public bool working;
public bool replacingMeshFilters;
public bool runningUndo;
private float mfDelay;
private float userSimSpeed;
public bool clearBrickDummies;
public bool clearGlassDummies;
public bool clearIronDummies;
public bool clearSteelDummies;
public float pirateAttackTimer;
public float meteorShowerTimer;
public float pirateFrequency;
public PlayerController player;
public Vector3 meteorShowerLocation;
public bool loadedMeteorTimer;
public bool loadedPirateTimer;
private bool loadedBlockPhysics;
private bool loadedHazardsEnabled;
private bool memoryCoroutineBusy;
private bool resetSimSpeed;
public Rocket rocketScript;
public Coroutine separateCoroutine;
public Coroutine meshCombineCoroutine;
public Coroutine blockCombineCoroutine;
public Coroutine hazardRemovalCoroutine;
private Coroutine memoryCoroutine;
private Coroutine undoCoroutine;
public List<Vector3> meteorShowerLocationList;
//! Stores information about blocks placed for UndoBuiltObjects function.
public class Block
{
public string blockType;
public GameObject blockObject;
public Block(string blockType, GameObject blockObject)
{
this.blockType = blockType;
this.blockObject = blockObject;
}
}
public List<Block> undoBlocks;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
// Get a reference to the player.
player = GameObject.Find("Player").GetComponent<PlayerController>();
// Get a reference to the rocket.
rocketScript = rocketObject.GetComponent<Rocket>();
// Initiate meteor shower location list.
meteorShowerLocationList = new List<Vector3>();
// Create the hazard manager.
hazardManager = new HazardManager(this);
// Create the combined mesh manager.
meshManager = new CombinedMeshManager(this);
// Create an object list to hold the player's most recently built objects for the 'undo' function.
undoBlocks = new List<Block>();
// Create mod block name list.
modBlockNames = new List<string>();
modBlockHolders = new List<GameObject[]>();
// Load chunk size setting.
int cs = PlayerPrefs.GetInt("chunkSize");
chunkSize = cs > 0 ? cs : 40;
// Load chunk size setting.
float simSpeed = PlayerPrefs.GetFloat("simulationSpeed");
simulationSpeed = simSpeed > 0 ? simSpeed : 0.02f;
// Create initial iron block holder for mesh manager.
GameObject ironInit = Instantiate(ironHolder, transform.position, transform.rotation);
ironInit.transform.parent = builtObjects.transform;
ironInit.GetComponent<MeshPainter>().ID = 0;
ironInit.SetActive(false);
ironHolders = new GameObject[] { ironInit };
// Create initial iron block holder for mesh manager.
GameObject glassInit = Instantiate(glassHolder, transform.position, transform.rotation);
glassInit.transform.parent = builtObjects.transform;
glassInit.GetComponent<MeshPainter>().ID = 0;
glassInit.SetActive(false);
glassHolders = new GameObject[] { glassInit };
// Create initial iron block holder for mesh manager.
GameObject steelInit = Instantiate(steelHolder, transform.position, transform.rotation);
steelInit.transform.parent = builtObjects.transform;
steelInit.GetComponent<MeshPainter>().ID = 0;
steelInit.SetActive(false);
steelHolders = new GameObject[] { steelInit };
// Create initial iron block holder for mesh manager.
GameObject brickInit = Instantiate(brickHolder, transform.position, transform.rotation);
brickInit.transform.parent = builtObjects.transform;
brickInit.GetComponent<MeshPainter>().ID = 0;
brickInit.SetActive(false);
brickHolders = new GameObject[] { brickInit };
}
//! Creates initial block holders for mod blocks.
public void InitModBlocks()
{
int index = 0;
int count = modBlockNames.Count;
foreach (string blockName in modBlockNames)
{
GameObject modBlockInit = Instantiate(modBlockHolder, transform.position, transform.rotation);
meshManager.SetMaterial(modBlockInit, blockName);
modBlockInit.transform.parent = builtObjects.transform;
modBlockInit.SetActive(false);
modBlockHolders.Add(new GameObject[] { modBlockInit });
index++;
}
}
//! Called once per frame by unity engine.
public void Update()
{
if (FileBasedPrefs.initialized == true)
{
if (FileBasedPrefs.GetBool(GetComponent<StateManager>().worldName + "Initialized") == false)
{
if (lander.GetComponent<InventoryManager>().initialized == true)
{
lander.GetComponent<InventoryManager>().AddItem("Solar Panel", 9);
lander.GetComponent<InventoryManager>().AddItem("Universal Conduit", 8);
lander.GetComponent<InventoryManager>().AddItem("Storage Container", 4);
lander.GetComponent<InventoryManager>().AddItem("Smelter", 3);
lander.GetComponent<InventoryManager>().AddItem("Universal Extractor", 2);
lander.GetComponent<InventoryManager>().AddItem("Dark Matter Conduit", 1);
lander.GetComponent<InventoryManager>().AddItem("Dark Matter Collector", 1);
FileBasedPrefs.SetBool(GetComponent<StateManager>().worldName + "Initialized", true);
}
}
else
{
if (loadedBlockPhysics == false)
{
if (PlayerPrefsX.GetPersistentBool("multiplayer") == false)
{
blockPhysics = PlayerPrefsX.GetPersistentBool("blockPhysics");
}
else
{
blockPhysics = false;
}
loadedBlockPhysics = true;
}
if (loadedHazardsEnabled == false)
{
hazardsEnabled = PlayerPrefsX.GetPersistentBool("hazardsEnabled");
loadedHazardsEnabled = true;
}
if (loadedMeteorTimer == false)
{
meteorShowerTimer = FileBasedPrefs.GetFloat(GetComponent<StateManager>().worldName + "meteorShowerTimer");
loadedMeteorTimer = true;
}
if (loadedPirateTimer == false)
{
pirateAttackTimer = FileBasedPrefs.GetFloat(GetComponent<StateManager>().worldName + "pirateAttackTimer");
loadedPirateTimer = true;
}
}
// A save game request is pending.
if (dataSaveRequested == true)
{
if (GetComponent<StateManager>().saving == false && GetComponent<StateManager>().AddressManagerBusy() == false)
{
UnityEngine.Debug.Log("Saving world...");
GetComponent<StateManager>().SaveData();
dataSaveRequested = false;
}
else if (GetComponent<StateManager>().AddressManagerBusy() == true)
{
if (GetComponent<GameManager>().simulationSpeed < 0.1f)
{
userSimSpeed = GetComponent<GameManager>().simulationSpeed;
resetSimSpeed = true;
}
GetComponent<GameManager>().simulationSpeed = 0.1f;
}
}
else if (resetSimSpeed == true)
{
GetComponent<GameManager>().simulationSpeed = userSimSpeed;
resetSimSpeed = false;
}
// Used to ensure components are removed before combining meshes.
if (replacingMeshFilters == true)
{
mfDelay += 1 * Time.deltaTime;
if (mfDelay > 1)
{
meshManager.CombineMeshes();
mfDelay = 0;
replacingMeshFilters = false;
}
}
if (GetComponent<StateManager>().worldLoaded == true)
{
hazardManager.UpdateHazards();
}
if (memoryCoroutineBusy == false)
{
memoryCoroutine = StartCoroutine(ManageMemory());
}
}
}
//! Unloads unused assets when the game is using too much memory.
public IEnumerator ManageMemory()
{
memoryCoroutineBusy = true;
float availableMemory = SystemInfo.systemMemorySize;
Process proc = Process.GetCurrentProcess();
proc.Refresh();
float usedMemory = Math.Abs((int)(proc.WorkingSet64 / (1024*1024)));
proc.Dispose();
float percentUsedMemory = usedMemory / availableMemory;
if (percentUsedMemory >= 0.5f)
{
Resources.UnloadUnusedAssets();
}
yield return new WaitForSeconds(60);
memoryCoroutineBusy = false;
}
//! Starts the undo coroutine.
public void UndoBuiltObjects()
{
if (runningUndo == false)
{
undoCoroutine = StartCoroutine(RemoveRecent());
}
}
//! Removes recently placed blocks.
private IEnumerator RemoveRecent()
{
runningUndo = true;
foreach (Block block in undoBlocks)
{
if (block.blockObject != null)
{
if (block.blockObject.activeInHierarchy)
{
Destroy(block.blockObject);
player.playerInventory.AddItem(block.blockType, 1);
if (PlayerPrefsX.GetPersistentBool("multiplayer") == true)
{
Vector3 pos = block.blockObject.transform.position;
Quaternion rot = block.blockObject.transform.rotation;
UpdateNetwork(1, block.blockType, pos, rot);
}
player.PlayCraftingSound();
}
}
yield return null;
}
undoBlocks.Clear();
runningUndo = false;
}
//! Sends instantiated block info to the server in multiplayer games.
private void UpdateNetwork(int destroy, string type, Vector3 pos, Quaternion rot)
{
using(WebClient client = new WebClient())
{
Uri uri = new Uri(PlayerPrefs.GetString("serverURL") + "/blocks");
string position = Mathf.Round(pos.x) + "," + Mathf.Round(pos.y) + "," + Mathf.Round(pos.z);
string rotation = Mathf.Round(rot.x) + "," + Mathf.Round(rot.y) + "," + Mathf.Round(rot.z) + "," + Mathf.Round(rot.w);
client.UploadStringAsync(uri, "POST", "@" + destroy + ":" + type + ":" + position + ":" + rotation);
}
}
//! Saves the game on exit.
public void RequestSaveOperation()
{
if (working == false && GetComponent<StateManager>().saving == false)
{
UnityEngine.Debug.Log("Requesting save operation...");
dataSaveRequested = true;
blocksCombined = false;
}
}
}