public class Checkpoint : MonoBehaviour
{
private CheckpointManager cm;
+ private Cheese cheese;
public Sprite red;
public Sprite green;
void Start()
{
cm = GameObject.FindGameObjectWithTag("CheckpointManager").GetComponent<CheckpointManager>();
+ cheese = GameObject.FindGameObjectWithTag("Player").GetComponent<Cheese>();
}
void OnTriggerEnter2D(Collider2D collision)
if(collision.CompareTag("Player"))
{
cm.lastCheckpointPos = transform.position;
+ cm.lastCheeseCount = cheese.cheesecount;
GetComponent<SpriteRenderer>().sprite = green;
}
}
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()
{
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;
+ }
}
public class Cheese : MonoBehaviour
{
public Text countText;
- private int cheesecount;
+
+ [HideInInspector]
+ public int cheesecount;
void Start()
{
}
}
- void SetCountText()
+ public void SetCountText()
{
countText.text = cheesecount.ToString();
}
{
private CheckpointManager checkpointManager;
+ private Cheese cheese;
+
Abyss abyss;
Health health;
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()
--- /dev/null
+fileFormatVersion: 2
+guid: 0970b2798f249f24f9dc2311e8279558
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--- /dev/null
+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;
+ }
+}
--- /dev/null
+fileFormatVersion: 2
+guid: cab4986a81a240748b61461a34b05671
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--- /dev/null
+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;
+ }
+ }
+}
--- /dev/null
+fileFormatVersion: 2
+guid: d331515f73d4299428e09df2083506c8
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: