|
|
@@ -1,5 +1,6 @@
|
|
|
using UnityEngine;
|
|
|
using TMPro;
|
|
|
+using System.Collections;
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
public class GameManager : MonoBehaviour
|
|
|
@@ -20,6 +21,17 @@ public class GameManager : MonoBehaviour
|
|
|
public float timeRemaining = 60f;
|
|
|
private bool gameOver = false;
|
|
|
|
|
|
+ public EnergyOrb[] allOrbs;
|
|
|
+
|
|
|
+
|
|
|
+ [Header("References")]
|
|
|
+ public PlayerController player;
|
|
|
+ public TimeChanger timeManager;
|
|
|
+
|
|
|
+ [Header("Procedural")]
|
|
|
+ public int worldSeed;
|
|
|
+
|
|
|
+
|
|
|
void Awake()
|
|
|
{
|
|
|
if (Instance == null)
|
|
|
@@ -31,8 +43,10 @@ public class GameManager : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void Start()
|
|
|
{
|
|
|
+ allOrbs = FindObjectsOfType<EnergyOrb>();
|
|
|
UpdateOrbUI();
|
|
|
UpdateTimerUI();
|
|
|
}
|
|
|
@@ -49,6 +63,17 @@ public class GameManager : MonoBehaviour
|
|
|
GameOver();
|
|
|
}
|
|
|
UpdateTimerUI();
|
|
|
+
|
|
|
+ if (Input.GetKeyDown(KeyCode.F5))
|
|
|
+ {
|
|
|
+ SaveSystem.Save(CreateSaveData());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Input.GetKeyDown(KeyCode.F9))
|
|
|
+ {
|
|
|
+ LoadGame(SaveSystem.Load());
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public void CollectOrb()
|
|
|
@@ -93,6 +118,7 @@ public class GameManager : MonoBehaviour
|
|
|
Debug.Log("YOU WIN!");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void GameOver()
|
|
|
{
|
|
|
GameOverUI.SetActive(true);
|
|
|
@@ -103,10 +129,41 @@ public class GameManager : MonoBehaviour
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
Cursor.visible = true;
|
|
|
|
|
|
-
|
|
|
Debug.Log("YOU LOSE!");
|
|
|
}
|
|
|
|
|
|
+ public void StartNewGame()
|
|
|
+ {
|
|
|
+ gameOver = false;
|
|
|
+
|
|
|
+ // Reseting the timer
|
|
|
+ timeRemaining = 60f;
|
|
|
+ UpdateTimerUI();
|
|
|
+
|
|
|
+ // Reseting all of the orbs
|
|
|
+ foreach (var orb in allOrbs)
|
|
|
+ {
|
|
|
+ orb.Collected = false;
|
|
|
+ orb.gameObject.SetActive(true);
|
|
|
+ }
|
|
|
+ collectedOrbs = 0;
|
|
|
+ UpdateOrbUI();
|
|
|
+
|
|
|
+ // Reset the player position and rotation
|
|
|
+ if (player != null)
|
|
|
+ {
|
|
|
+ player.transform.position = Vector3.zero;
|
|
|
+ player.transform.rotation = Quaternion.identity;
|
|
|
+
|
|
|
+ // Reset stamina
|
|
|
+ player.Stamina = player.maxStamina;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Saving works
|
|
|
+ * Loading works too, both ingame. sometimes.
|
|
|
+ */
|
|
|
+
|
|
|
public void RestartGame()
|
|
|
{
|
|
|
Time.timeScale = 1f;
|
|
|
@@ -115,4 +172,73 @@ public class GameManager : MonoBehaviour
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
Cursor.visible = false;
|
|
|
}
|
|
|
+
|
|
|
+ public SaveData CreateSaveData()
|
|
|
+ {
|
|
|
+ SaveData data = new SaveData();
|
|
|
+
|
|
|
+ data.playerPosition = player.transform.position;
|
|
|
+ data.playerRotation = player.transform.rotation;
|
|
|
+ data.stamina = player.Stamina;
|
|
|
+
|
|
|
+ data.orbsCollected = collectedOrbs;
|
|
|
+ data.remainingTime = timeRemaining;
|
|
|
+
|
|
|
+ data.orbs.Clear();
|
|
|
+ foreach (var orb in allOrbs)
|
|
|
+ {
|
|
|
+ data.orbs.Add(new OrbData
|
|
|
+ {
|
|
|
+ id = orb.ID,
|
|
|
+ collected = orb.Collected
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void LoadGame(SaveData data)
|
|
|
+ {
|
|
|
+ if (data == null) return;
|
|
|
+ StartCoroutine(LoadPlayerNextFrame(data));
|
|
|
+ }
|
|
|
+
|
|
|
+ private IEnumerator LoadPlayerNextFrame(SaveData data)
|
|
|
+ {
|
|
|
+ yield return new WaitForEndOfFrame(); // wait for the scene too finish
|
|
|
+
|
|
|
+ // Restore the player position and rotation
|
|
|
+ player.transform.position = data.playerPosition;
|
|
|
+ player.transform.rotation = data.playerRotation;
|
|
|
+
|
|
|
+ // Restore the stamina
|
|
|
+ player.Stamina = data.stamina;
|
|
|
+
|
|
|
+ // Restore the timer
|
|
|
+ timeRemaining = data.remainingTime;
|
|
|
+ UpdateTimerUI();
|
|
|
+
|
|
|
+ // Restore all of the orbs
|
|
|
+ for (int i = 0; i < allOrbs.Length; i++)
|
|
|
+ {
|
|
|
+ allOrbs[i].Collected = data.orbs[i].collected;
|
|
|
+ allOrbs[i].gameObject.SetActive(!data.orbs[i].collected);
|
|
|
+ }
|
|
|
+
|
|
|
+ collectedOrbs = data.orbsCollected;
|
|
|
+ UpdateOrbUI();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|