From 84db97ac3843703acbd659b797bef164dc4f472f Mon Sep 17 00:00:00 2001 From: KG0104 <76601263+KG0104@users.noreply.github.com> Date: Wed, 11 Aug 2021 11:58:07 +0200 Subject: [PATCH] SaveSystem v0.1 NICHT FUNKTIONAL!!! --- 2DGame/Assets/Scripts/Maus/Checkpoint.cs | 3 ++ .../Assets/Scripts/Maus/CheckpointManager.cs | 25 +++++++++++- 2DGame/Assets/Scripts/Maus/Cheese.cs | 6 ++- 2DGame/Assets/Scripts/Maus/MousePos.cs | 4 ++ 2DGame/Assets/Scripts/Saving.meta | 8 ++++ 2DGame/Assets/Scripts/Saving/PlayerData.cs | 18 +++++++++ .../Assets/Scripts/Saving/PlayerData.cs.meta | 11 +++++ 2DGame/Assets/Scripts/Saving/SaveSystem.cs | 40 +++++++++++++++++++ .../Assets/Scripts/Saving/SaveSystem.cs.meta | 11 +++++ 9 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 2DGame/Assets/Scripts/Saving.meta create mode 100644 2DGame/Assets/Scripts/Saving/PlayerData.cs create mode 100644 2DGame/Assets/Scripts/Saving/PlayerData.cs.meta create mode 100644 2DGame/Assets/Scripts/Saving/SaveSystem.cs create mode 100644 2DGame/Assets/Scripts/Saving/SaveSystem.cs.meta diff --git a/2DGame/Assets/Scripts/Maus/Checkpoint.cs b/2DGame/Assets/Scripts/Maus/Checkpoint.cs index 7064863..5814fe9 100644 --- a/2DGame/Assets/Scripts/Maus/Checkpoint.cs +++ b/2DGame/Assets/Scripts/Maus/Checkpoint.cs @@ -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(); + cheese = GameObject.FindGameObjectWithTag("Player").GetComponent(); } 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().sprite = green; } } diff --git a/2DGame/Assets/Scripts/Maus/CheckpointManager.cs b/2DGame/Assets/Scripts/Maus/CheckpointManager.cs index 671d147..afb7c2f 100644 --- a/2DGame/Assets/Scripts/Maus/CheckpointManager.cs +++ b/2DGame/Assets/Scripts/Maus/CheckpointManager.cs @@ -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; + } } diff --git a/2DGame/Assets/Scripts/Maus/Cheese.cs b/2DGame/Assets/Scripts/Maus/Cheese.cs index 2106544..bfebef8 100644 --- a/2DGame/Assets/Scripts/Maus/Cheese.cs +++ b/2DGame/Assets/Scripts/Maus/Cheese.cs @@ -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(); } diff --git a/2DGame/Assets/Scripts/Maus/MousePos.cs b/2DGame/Assets/Scripts/Maus/MousePos.cs index ecbb065..756b04e 100644 --- a/2DGame/Assets/Scripts/Maus/MousePos.cs +++ b/2DGame/Assets/Scripts/Maus/MousePos.cs @@ -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(); abyss = GetComponent(); health = GetComponent(); + cheese = GetComponent(); 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 index 0000000..0512b79 --- /dev/null +++ b/2DGame/Assets/Scripts/Saving.meta @@ -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 index 0000000..9c742bb --- /dev/null +++ b/2DGame/Assets/Scripts/Saving/PlayerData.cs @@ -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 index 0000000..61ea3ed --- /dev/null +++ b/2DGame/Assets/Scripts/Saving/PlayerData.cs.meta @@ -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 index 0000000..7bfe50a --- /dev/null +++ b/2DGame/Assets/Scripts/Saving/SaveSystem.cs @@ -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 index 0000000..041d4ae --- /dev/null +++ b/2DGame/Assets/Scripts/Saving/SaveSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d331515f73d4299428e09df2083506c8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- 2.44.0