Эх сурвалжийг харах

days of work, didnt push til save was working

Priz 4 өдөр өмнө
parent
commit
6406def66f

+ 3 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scenes/Game.unity

@@ -2709,6 +2709,9 @@ MonoBehaviour:
   orbsCounter: {fileID: 2113331569}
   timertext: {fileID: 805139188}
   timeRemaining: 60
+  player: {fileID: 1708832122}
+  timeManager: {fileID: 1385640938}
+  worldSeed: 0
 --- !u!1 &1099204608
 GameObject:
   m_ObjectHideFlags: 0

+ 9 - 2
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/EnergyOrb.cs

@@ -2,12 +2,19 @@ using UnityEngine;
 
 public class EnergyOrb : MonoBehaviour
 {
+    [Header("Orb Save Data")]
+    public string ID;  // unique ID for each of this orb
+    public bool Collected = false;
+
     private void OnTriggerEnter(Collider other)
     {
-        if (other.CompareTag("Player"))
+        if (other.CompareTag("Player") && !Collected)
         {
+            Collected = true;
+            gameObject.SetActive(false);
+
+            // Send the GameManager that its been collected
             GameManager.Instance.CollectOrb();
-            Destroy(gameObject);
         }
     }
 }

+ 127 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/GameManager.cs

@@ -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();
+    }
+
+
+
+
+
+
+
+
+
 }

+ 37 - 8
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/LoadingManager.cs

@@ -18,6 +18,9 @@ public class LoadingManager : MonoBehaviour
     [TextArea(2, 4)]
     public string[] tips;
 
+    public static bool loadSave = false;
+
+
     void Start()
     {
         StartCoroutine(LoadAsync());
@@ -42,29 +45,34 @@ public class LoadingManager : MonoBehaviour
         if (continueText != null)
             continueText.gameObject.SetActive(false);
 
+        // Show a random tip on-screen
         if (tips.Length > 0 && tipText != null)
         {
             int randomIndex = Random.Range(0, tips.Length);
             tipText.text = "Tip: \n" + tips[randomIndex];
         }
 
+        // Minimum load time
+        float timer = 0f;
+
         while (!operation.isDone)
         {
-            float targetProgress = Mathf.Clamp01(operation.progress / 0.9f);
+            timer += Time.deltaTime;
 
-            displayedProgress = Mathf.MoveTowards(
-                displayedProgress,
-                targetProgress,
-                Time.deltaTime * 0.5f
-            );
+            // Smooth fill through bar
+            float targetProgress = Mathf.Clamp01(operation.progress / 0.9f);
+            displayedProgress = Mathf.MoveTowards(displayedProgress, targetProgress, Time.deltaTime * 0.5f);
 
             if (progressFill != null)
                 progressFill.fillAmount = displayedProgress;
 
-            if (displayedProgress >= 1f)
+            // Show continue text when fully loaded
+            if (displayedProgress >= 1f && timer >= minimumLoadTime)
             {
-                continueText.gameObject.SetActive(true);
+                if (continueText != null)
+                    continueText.gameObject.SetActive(true);
 
+                // Wait for any key press
                 if (Input.anyKeyDown)
                 {
                     operation.allowSceneActivation = true;
@@ -73,7 +81,28 @@ public class LoadingManager : MonoBehaviour
 
             yield return null;
         }
+
+        // Scene is now fully loaded
+        // Wait one frame for GameManager to Awake
+        yield return null;
+
+        if (loadSave)
+        {
+            SaveData data = SaveSystem.Load();
+            if (data != null && GameManager.Instance != null)
+            {
+                GameManager.Instance.LoadGame(data); // restore last save (not fully implemented)
+            }
+            else
+            {
+                Debug.LogWarning("No save file found! Starting new game instead.");
+                GameManager.Instance.StartNewGame();
+            }
+        }
+
+
     }
 
 
+
 }

+ 4 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/MenuManager.cs

@@ -9,15 +9,19 @@ public class MenuManager : MonoBehaviour
     public void StartNewGame()
     {
         LoadingManager.sceneToLoad = "Game";
+        LoadingManager.loadSave = false;  // No loading for new game
         SceneManager.LoadScene("LoadingScreen");
     }
 
     public void LoadGame()
     {
         LoadingManager.sceneToLoad = "Game";
+        LoadingManager.loadSave = true;   // Load the last save
         SceneManager.LoadScene("LoadingScreen");
     }
 
+
+
     public void OpenSettings()
     {
         if (SettingsPanel != null)

+ 3 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/PlayerController.cs

@@ -61,6 +61,7 @@ public class PlayerController : MonoBehaviour
 
     void Camera()
     {
+        // Camera control of player
         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
 
@@ -74,6 +75,7 @@ public class PlayerController : MonoBehaviour
 
     void Movement()
     {
+        // Movement of the player & sprint
         bool isSprinting = Input.GetKey(KeyCode.LeftShift);
 
         isGrounded = controller.isGrounded;
@@ -105,6 +107,7 @@ public class PlayerController : MonoBehaviour
 
         controller.Move(move * currentSpeed * Time.deltaTime);
 
+        // Jumping
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

+ 8 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/Save.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 71da1f44bf4ddb04884588e6e90398bc
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 34 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/SaveData.cs

@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+[Serializable]
+public class SaveData
+{
+    // Player things
+    public Vector3 playerPosition;
+    public Quaternion playerRotation;
+    public int stamina;
+
+    // Game state (Orbs and Time)
+    public int orbsCollected;
+    public float remainingTime;
+
+    // Procedural (seed)
+    public int worldSeed;
+
+    // List of Orbs
+    public List<OrbData> orbs = new();
+
+    // Player Settings
+    public float mouseSensitivity;
+    public float masterVolume;
+}
+
+[Serializable]
+public class OrbData
+{
+    public string id;
+    public Vector3 position;
+    public bool collected;
+}

+ 2 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/SaveData.cs.meta

@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: e803c026a175fdb42b1c4b45dc08dc74

+ 27 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/SaveSystem.cs

@@ -0,0 +1,27 @@
+using System.IO;
+using UnityEngine;
+
+public static class SaveSystem
+{
+    private static string savePath = Application.persistentDataPath + "/save.json";
+
+    public static void Save(SaveData data)
+    {
+        string json = JsonUtility.ToJson(data, true);
+        File.WriteAllText(savePath, json);
+    }
+
+    public static SaveData Load()
+    {
+        string path = Application.persistentDataPath + "/save.json";
+
+        if (File.Exists(path))
+        {
+            string json = File.ReadAllText(path);
+            return JsonUtility.FromJson<SaveData>(json);
+        }
+        return null;
+    }
+
+}
+

+ 2 - 0
GAMEN3-ValleyRunner_SanTi036/Assets/Scripts/SaveSystem.cs.meta

@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: bee0c78cf57e8604f8372bd52673044d

+ 1 - 1
GAMEN3-ValleyRunner_SanTi036/Assets/Skybox.mat

@@ -112,7 +112,7 @@ Material:
     - _DstBlend: 0
     - _DstBlendAlpha: 0
     - _EnvironmentReflections: 1
-    - _Exposure: 0.9757724
+    - _Exposure: 0.9816964
     - _GlossMapScale: 0
     - _Glossiness: 0
     - _GlossyReflections: 0