| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using StarterAssets;
- public class GameManager : MonoBehaviour
- {
- public static GameManager Instance;
- public GameObject GameOverUI;
- public GameObject VictoryUI;
- [Header("Orb Settings")]
- public int totalOrbs = 20;
- private int collectedOrbs = 0;
- public List<EnergyOrb> allOrbs = new List<EnergyOrb>();
- [Header("UI")]
- public TMP_Text orbsCounter;
- public TMP_Text timertext;
- [Header("Nightfall Timer")]
- public float timeRemaining = 60f;
- private bool gameOver = false;
- //public EnergyOrb[] allOrbs;
- [Header("References")]
- public ThirdPersonController player;
- public TimeChanger timeManager;
- [Header("Procedural")]
- public int worldSeed;
- [HideInInspector] public SaveData inMemorySave = null;
- public bool HasInMemorySave => inMemorySave != null;
- void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- } else
- {
- Destroy(gameObject);
- }
- }
- void Start()
- {
- //allOrbs = FindObjectsOfType<EnergyOrb>();
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- UpdateOrbUI();
- UpdateTimerUI();
- }
- void Update()
- {
- if (gameOver) return;
- timeRemaining -= Time.deltaTime;
- if (timeRemaining <= 0f)
- {
- timeRemaining = 0f;
- GameOver();
- }
- UpdateTimerUI();
- if (Input.GetKeyDown(KeyCode.F5))
- {
- SaveSystem.Save(CreateSaveData());
- }
- if (Input.GetKeyDown(KeyCode.F9))
- {
- LoadGame(SaveSystem.Load());
- }
- }
- public void CollectOrb()
- {
- if (gameOver) return;
- collectedOrbs++;
- UpdateOrbUI();
- if (collectedOrbs >= totalOrbs)
- {
- Victory();
- }
- }
- void UpdateOrbUI()
- {
- if (orbsCounter != null)
- {
- orbsCounter.text = $"{collectedOrbs} / {totalOrbs}";
- }
- }
- void UpdateTimerUI()
- {
- if (timertext != null)
- {
- timertext.text = Mathf.Ceil(timeRemaining).ToString(); ;
- }
- }
- void Victory()
- {
- VictoryUI.SetActive(true);
- gameOver = true;
- Time.timeScale = 0f;
- if (player != null)
- player.enabled = false;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- Debug.Log("YOU WIN!");
- }
- void GameOver()
- {
- GameOverUI.SetActive(true);
- gameOver = true;
- Time.timeScale = 0f;
- if (player != null)
- player.enabled = false;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- Debug.Log("YOU LOSE!");
- }
- public void RegisterOrbs(List<EnergyOrb> newOrbs)
- {
- allOrbs.AddRange(newOrbs);
- totalOrbs = allOrbs.Count; // Sync the total count
- UpdateOrbUI();
- }
- public void StartNewGame()
- {
- gameOver = false;
- timeRemaining = 60f;
- UpdateTimerUI();
- foreach (var orb in allOrbs)
- {
- orb.Collected = false;
- orb.gameObject.SetActive(true);
- }
- collectedOrbs = 0;
- UpdateOrbUI();
- if (player != null)
- {
- player.transform.position = Vector3.zero;
- player.transform.rotation = Quaternion.identity;
- player.Stamina = player.maxStamina;
- }
- }
- /* Saving works
- * Loading works too, both ingame. sometimes.
- */
- public void RestartGame()
- {
- Time.timeScale = 1f;
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- public SaveData CreateSaveData()
- {
- SaveData data = new SaveData();
- // Player
- data.playerPosition = player.transform.position;
- data.playerRotation = player.transform.rotation;
- data.stamina = player.Stamina;
- // Time / orbs
- data.orbsCollected = collectedOrbs;
- data.remainingTime = timeRemaining;
- // Orbs
- data.orbs.Clear();
- foreach (var orb in allOrbs)
- {
- data.orbs.Add(new OrbData
- {
- id = orb.ID,
- collected = orb.Collected
- });
- }
- // ENEMIES (THIS IS WHERE IT GOES)
- data.enemies.Clear();
- foreach (EnemyAI enemy in FindObjectsOfType<EnemyAI>())
- {
- data.enemies.Add(enemy.GetSaveData());
- }
- return data;
- }
- public void SaveInMemory()
- {
- inMemorySave = CreateSaveData();
- Debug.Log("Game saved in memory!");
- }
- public void LoadGame(SaveData data)
- {
- if (data == null) return;
- StartCoroutine(LoadGameNextFrame(data));
- }
- public void LoadFromMemory()
- {
- if (inMemorySave != null)
- {
- StartCoroutine(LoadGameNextFrame(inMemorySave));
- Debug.Log("Loaded game from memory!");
- }
- else
- {
- Debug.LogWarning("No in-memory save found!");
- }
- }
- public IEnumerator LoadGameNextFrame(SaveData data)
- {
- yield return new WaitForEndOfFrame(); // wait for scene & map to generate
- // Restore procedural map seed
- WorldSettings.seed = data.worldSeed;
- WorldSettings.generateNewWorld = false;
- // In LoadGameNextFrame or directly in PauseMenu
- Rigidbody rb = player.GetComponent<Rigidbody>();
- if (rb != null)
- {
- rb.position = data.playerPosition; // directly sets physics position
- rb.rotation = data.playerRotation;
- rb.linearVelocity = Vector3.zero; // reset velocity
- }
- else
- {
- player.transform.position = data.playerPosition;
- player.transform.rotation = data.playerRotation;
- }
- player.Stamina = data.stamina;
- // Player
- player.transform.position = data.playerPosition;
- player.transform.rotation = data.playerRotation;
- player.Stamina = data.stamina;
- // Timer
- timeRemaining = data.remainingTime;
- UpdateTimerUI();
- // Orbs
- collectedOrbs = data.orbsCollected;
- for (int i = 0; i < allOrbs.Count; i++)
- {
- if (i >= data.orbs.Count) break;
- allOrbs[i].Collected = data.orbs[i].collected;
- allOrbs[i].gameObject.SetActive(!data.orbs[i].collected);
- }
- UpdateOrbUI();
- Debug.Log("Game Loaded!");
- }
- }
|