-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
275 lines (222 loc) · 9.7 KB
/
GameManager.cs
File metadata and controls
275 lines (222 loc) · 9.7 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
// For other objects
// gameManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>();
// When the game begins, the game manager will go through and SetActive(true)
// everything in the array. Game objects are expected to place themselves inside.
// This is also makes it easier to prefab the GameManager and these types of game objects
public List<GameObject> objectsToEnable;
public bool gameBegan = false;
// Used when placing back wires
public GameObject wirePrefab;
private static bool loadedFromAnotherScene = false;
// The 'score' of the current level
int collectedCollecibles = 0;
// All controllers, TODO: Make Player class that keeps track of these
InventoryController invController;
Drag dragScript;
AdjustObjects adjustScript;
WireEditor wireEditor;
List<InventoryController.SpawnedObject> cached_spawnedObjects;
List<InventoryController.SpawnedWire> cached_spawnedWires;
void Awake()
{
MusicController.Instance.PlayMainMusic();
// Check the static variable and if another GameManager exists destroy self
if (loadedFromAnotherScene)
{
Destroy(gameObject);
}
else
{
loadedFromAnotherScene = true; // for future game managers
DontDestroyOnLoad(gameObject);
if (GameObject.Find("Canvas").transform.Find("TitleScreen"))
{
GameObject.Find("Canvas").transform.Find("TitleScreen").gameObject.SetActive(true);
}
}
}
// Called when going into another scene, and should only be
// called in that situation
public void PrepareForNewLevel()
{
loadedFromAnotherScene = false;
Destroy(gameObject);
}
void Start()
{
Init();
}
// Since the GameManager doesnt delete itself between scene reloads, we need a function to cache what
// is needed again, since those objects will be deleted. Basically this is a Start() function more or less
void Init()
{
gameBegan = false;
collectedCollecibles = 0; // reset score
invController = GameObject.FindGameObjectWithTag("InventoryController").GetComponent<InventoryController>();
GameObject p = GameObject.FindGameObjectWithTag("Player");
dragScript = p.GetComponent<Drag>();
adjustScript = p.GetComponent<AdjustObjects>();
wireEditor = p.GetComponent<WireEditor>();
}
// Called when the build phase is over. Activates physics for objects that need it, etc.
public void StartGame()
{
gameBegan = true;
foreach (GameObject gameObj in objectsToEnable)
{
if (gameObj != null)
{
gameObj.SetActive(true);
}
}
// Find all InteractiveObjects in the game and BeginGame on each one
GameObject[] allInteractiveObjects = GameObject.FindGameObjectsWithTag("InteractiveObject");
foreach (GameObject gameObj in allInteractiveObjects)
{
InteractiveObject interObj = gameObj.GetComponent<InteractiveObject>();
interObj.BeginGame();
}
// Get the final positions of all player placed objects
invController.GetFinalValues();
invController.RestrictInventory(true);
// No player actions after beginning
dragScript.ResetDraggedObjectValues();
dragScript.enabled = false;
adjustScript.HideButtons();
adjustScript.enabled = false;
wireEditor.enabled = false;
}
public void AddCollectible()
{
if (collectedCollecibles < 3)
{
collectedCollecibles++;
}
else
{
Debug.LogError("3 or more collectibles have already been collected!");
}
}
// Can be called by anything in the scene which completes the level
public void LevelWin()
{
// Save the score if its bigger than the previous one
int oldScore = PlayerPrefs.GetInt("Score_" + SceneManager.GetActiveScene().name);
if (collectedCollecibles > oldScore)
{
PlayerPrefs.SetInt("Score_" + SceneManager.GetActiveScene().name, collectedCollecibles);
// And also add it to the collected total
int diff = collectedCollecibles - oldScore;
int total = PlayerPrefs.GetInt("TotalGearsCollected");
PlayerPrefs.SetInt("TotalGearsCollected", total + diff);
}
// Since the level could be restarted many times, we don't need to grab
// the reference to the level complete panel until we actually need to show it
GameObject levelCompletePanel;
levelCompletePanel = GameObject.Find("Canvas").transform.Find("LevelCompletePanel").gameObject;
levelCompletePanel.SetActive(true); // The level panel has its own methods which continue from here
LevelComplete lvlComplete = levelCompletePanel.transform.Find("InnerPanel").GetComponent<LevelComplete>();
lvlComplete.ShowAcquiredCollectibles(collectedCollecibles, oldScore);
}
// Reloads the scene and uses InventoryController's (cached as invController here)
// spawnedObjects to return player placed objects back to where they were
public void BackToBuildMode()
{
SceneLoader.Instance.ShowLoading();
// Cache spawnedObjects since they'll be deleted in the scene reset
cached_spawnedObjects = invController.spawnedObjects;
cached_spawnedWires = invController.spawnedWires;
// Before the new scene loads, we empty this list to remove references that will be gone
// after the scene reset. Has to happen now as some objects add themeselves to objectsToEnable
// on their Start() method, which happens before our Init() most likely
objectsToEnable.Clear();
SceneLoader.Instance.LoadScene(SceneManager.GetActiveScene().buildIndex, AfterReturnToBuild);
}
void AfterReturnToBuild()
{
StartCoroutine(WaitForFramesThenExecute(1, Init, PlaceBackSpawnedObjects));
}
// Returns all objects placed by the player in the previous build mode
void PlaceBackSpawnedObjects()
{
for (int i = 0; i < cached_spawnedObjects.Count; i++)
{
// Because C# is smart and doesnt let me edit spawnedObjects[i] directly
InventoryController.SpawnedObject thisSpawnedObject;
thisSpawnedObject = cached_spawnedObjects[i];
GameObject newActualObject = Instantiate(thisSpawnedObject.Prefab, thisSpawnedObject.V3Position, thisSpawnedObject.Rotation) as GameObject;
newActualObject.GetComponent<InteractiveObject>().draggable = true;
thisSpawnedObject.ActualObject = newActualObject;
if (newActualObject.GetComponentInChildren<ConnectionPoint>() != null)
{
newActualObject.GetComponentInChildren<ConnectionPoint>().connectionID = thisSpawnedObject.connectionID;
}
// Go through all inventory slots in the scene, find the ones with a matching id,
// and decrease their startingAmount
foreach (InvItem invItem in invController.itemSlots)
{
if (invItem.uniqueName == thisSpawnedObject.OriginInvSlot)
{
invItem.startingAmount--;
invItem.UpdateAmountCounter();
// Small safety measure
if (invItem.startingAmount < 0)
{
Debug.LogError("PlaceBackSpawnedObjects has made an item slots amount below 0. Something went wrong.");
}
break; // stop the loop since we've found what we were looking for
}
}
cached_spawnedObjects[i] = thisSpawnedObject;
}
ConnectionPoint[] conPoints = GameObject.FindObjectsOfType<ConnectionPoint>();
for (int i = 0; i < cached_spawnedWires.Count; i++)
{
InventoryController.SpawnedWire thisSpawnedWire;
thisSpawnedWire = cached_spawnedWires[i];
GameObject newWire = Instantiate(wirePrefab);
Wire wireComp = newWire.GetComponent<Wire>();
// Finding connection one
for (int j = 0; j < conPoints.Length; j++)
{
if (conPoints[j].connectionID == thisSpawnedWire.connectionOne)
{
wireComp.connectionOne = conPoints[j];
conPoints[j].connectedWire = wireComp;
}
}
// Finding connection two
for (int j = 0; j < conPoints.Length; j++)
{
if (conPoints[j].connectionID == thisSpawnedWire.connectionTwo)
{
wireComp.connectionTwo = conPoints[j];
conPoints[j].connectedWire = wireComp;
}
}
}
// Init is called just before this, so we have a new invController that need a list of spawnedObjects
invController.spawnedObjects = cached_spawnedObjects;
SceneLoader.Instance.HideLoading();
}
// Take an amount of frames to wait, then execute all given methods/functions
// USE WITH StartCoroutine (look in BackToBuildMode as an example)
IEnumerator WaitForFramesThenExecute(int frameCount, params Action[] methodsToCall)
{
while (frameCount > 0)
{
frameCount--;
yield return null;
}
foreach (Action method in methodsToCall)
{
method();
}
}
}