]> git.lizzy.rs Git - SuperMouseAdventure.git/commitdiff
SaveSystem v0.1 NICHT FUNKTIONAL!!!
authorKG0104 <76601263+KG0104@users.noreply.github.com>
Wed, 11 Aug 2021 09:58:07 +0000 (11:58 +0200)
committerKG0104 <76601263+KG0104@users.noreply.github.com>
Wed, 11 Aug 2021 09:58:07 +0000 (11:58 +0200)
2DGame/Assets/Scripts/Maus/Checkpoint.cs
2DGame/Assets/Scripts/Maus/CheckpointManager.cs
2DGame/Assets/Scripts/Maus/Cheese.cs
2DGame/Assets/Scripts/Maus/MousePos.cs
2DGame/Assets/Scripts/Saving.meta [new file with mode: 0644]
2DGame/Assets/Scripts/Saving/PlayerData.cs [new file with mode: 0644]
2DGame/Assets/Scripts/Saving/PlayerData.cs.meta [new file with mode: 0644]
2DGame/Assets/Scripts/Saving/SaveSystem.cs [new file with mode: 0644]
2DGame/Assets/Scripts/Saving/SaveSystem.cs.meta [new file with mode: 0644]

index 7064863b4f9ea60ec36310d71f048ed7a5a22f3f..5814fe95ca8b90908237113fa3d9ded4a488db7c 100644 (file)
@@ -5,6 +5,7 @@ using UnityEngine;
 public class Checkpoint : MonoBehaviour
 {
     private CheckpointManager cm;
+    private Cheese cheese;
 
     public Sprite red;
     public Sprite green;
@@ -12,6 +13,7 @@ public class Checkpoint : MonoBehaviour
     void Start()
     {
         cm = GameObject.FindGameObjectWithTag("CheckpointManager").GetComponent<CheckpointManager>();
+        cheese = GameObject.FindGameObjectWithTag("Player").GetComponent<Cheese>();
     }
 
     void OnTriggerEnter2D(Collider2D collision)
@@ -19,6 +21,7 @@ public class Checkpoint : MonoBehaviour
         if(collision.CompareTag("Player"))
         {
             cm.lastCheckpointPos = transform.position;
+            cm.lastCheeseCount = cheese.cheesecount;
             GetComponent<SpriteRenderer>().sprite = green;
         }
     }
index 671d147a1ebcdbd43d97f64e2e609d1f1616c8fb..afb7c2fc8bdebf6150d1be92345271181ed11262 100644 (file)
@@ -5,7 +5,10 @@ using UnityEngine;
 public class CheckpointManager : MonoBehaviour
 {
     private static CheckpointManager instance;
-    public Vector2 lastCheckpointPos;
+    public Vector3 lastCheckpointPos;
+    public Cheese cheese;
+    public int lastCheeseCount;
+    public Transform mouse;
 
     void Awake()
     {
@@ -19,4 +22,24 @@ public class CheckpointManager : MonoBehaviour
             Destroy(gameObject);
         }
     }
+    //cheese muss noch definiert werden
+
+
+    public void SavePlayer ()
+    {
+        SaveSystem.SavePlayer(instance, cheese);
+    }
+
+    public void LoadPlayer()
+    {
+        PlayerData data = SaveSystem.LoadPlayer();
+
+        cheese.cheesecount = data.collectedCheese;
+
+        Vector3 position;
+        position.x = data.position[0];
+        position.y = data.position[1];
+        position.z = data.position[2];
+        mouse.transform.position = position;
+    }
 }
index 210654409cda70a6b3d86f31bc0574acd2c7f579..bfebef807979884f559f1a538aa7bca2e3fe5753 100644 (file)
@@ -6,7 +6,9 @@ using UnityEngine.UI;
 public class Cheese : MonoBehaviour
 {
     public Text countText;
-    private int cheesecount;
+    
+    [HideInInspector]
+    public int cheesecount;
 
     void Start()
     {
@@ -27,7 +29,7 @@ public class Cheese : MonoBehaviour
         }
     }
 
-    void SetCountText()
+    public void SetCountText()
     {
         countText.text = cheesecount.ToString();
     }
index ecbb065da8951785b9cbf084cb217151d140df08..756b04e0ebb1b7d9504c6191d1645e8651670136 100644 (file)
@@ -7,6 +7,8 @@ public class MousePos : MonoBehaviour
 {
     private CheckpointManager checkpointManager;
 
+    private Cheese cheese;
+
     Abyss abyss;
 
     Health health;
@@ -17,7 +19,9 @@ public class MousePos : MonoBehaviour
         checkpointManager = GameObject.FindGameObjectWithTag("CheckpointManager").GetComponent<CheckpointManager>();
         abyss = GetComponent<Abyss>();
         health = GetComponent<Health>();
+        cheese = GetComponent<Cheese>();
         transform.position = checkpointManager.lastCheckpointPos;
+        cheese.cheesecount = checkpointManager.lastCheeseCount;
     }
 
     void Update()
diff --git a/2DGame/Assets/Scripts/Saving.meta b/2DGame/Assets/Scripts/Saving.meta
new file mode 100644 (file)
index 0000000..0512b79
--- /dev/null
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 0970b2798f249f24f9dc2311e8279558
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/2DGame/Assets/Scripts/Saving/PlayerData.cs b/2DGame/Assets/Scripts/Saving/PlayerData.cs
new file mode 100644 (file)
index 0000000..9c742bb
--- /dev/null
@@ -0,0 +1,18 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class PlayerData
+{
+    public float [] position;
+    public int collectedCheese;
+
+    public PlayerData (CheckpointManager cm, Cheese cheese)
+    {
+        collectedCheese = cheese.cheesecount;
+        position = new float[3];
+        position[0] = cm.lastCheckpointPos.x;
+        position[1] = cm.lastCheckpointPos.y;
+        position[2] = cm.lastCheckpointPos.z;
+    }
+}
diff --git a/2DGame/Assets/Scripts/Saving/PlayerData.cs.meta b/2DGame/Assets/Scripts/Saving/PlayerData.cs.meta
new file mode 100644 (file)
index 0000000..61ea3ed
--- /dev/null
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: cab4986a81a240748b61461a34b05671
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/2DGame/Assets/Scripts/Saving/SaveSystem.cs b/2DGame/Assets/Scripts/Saving/SaveSystem.cs
new file mode 100644 (file)
index 0000000..7bfe50a
--- /dev/null
@@ -0,0 +1,40 @@
+using UnityEngine;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+
+public static class SaveSystem
+{
+    public static void SavePlayer (CheckpointManager cm, Cheese cheese)
+    {
+        BinaryFormatter formatter = new BinaryFormatter();
+        string path = Application.persistentDataPath + "/data.lol";
+        FileStream stream = new FileStream(path, FileMode.Create);
+
+        PlayerData data = new PlayerData(cm, cheese);
+
+        formatter.Serialize(stream, data);
+        stream.Close();
+    }
+
+    public static PlayerData LoadPlayer ()
+    {
+        string path = Application.persistentDataPath + "/data.lol";
+        if (File.Exists(path))
+        {
+            BinaryFormatter formatter = new BinaryFormatter();
+            FileStream stream = new FileStream(path, FileMode.Open);
+
+            formatter.Deserialize(stream);
+
+            PlayerData data = formatter.Deserialize(stream) as PlayerData;
+            stream.Close();
+
+            return data;
+        }
+        else
+        {
+            Debug.LogError("Save file not found in" + path);
+            return null;
+        }
+    }
+}
diff --git a/2DGame/Assets/Scripts/Saving/SaveSystem.cs.meta b/2DGame/Assets/Scripts/Saving/SaveSystem.cs.meta
new file mode 100644 (file)
index 0000000..041d4ae
--- /dev/null
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: d331515f73d4299428e09df2083506c8
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: