Timer eingebaut, Inventory Script überarbeitet, InventoryScript dank Hr. Neuhaus durch Speicherfunktion erweiter, GameOver und GameOverlay ready and working
This commit is contained in:
@@ -25,31 +25,38 @@ public class ChangeHealth : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
inventory.Health = standardHealth;
|
||||
healthHUD.text = inventory.Health.ToString();
|
||||
|
||||
Debug.Log(inventory.Health);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
healthHUD.text = inventory.Health.ToString();
|
||||
|
||||
if (inventory.Health <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
public void GetHit()
|
||||
public void GetHit() //MUSS NOCH MIT ENEMIES VERKNÜPFT WERDEN
|
||||
{
|
||||
inventory.Health -= 1; //MAGIC NUMBER BITTE BEACHTEN
|
||||
if (inventory.Health > 0)
|
||||
{
|
||||
inventory.Health -= 1; //MAGIC NUMBER BITTE BEACHTEN
|
||||
}
|
||||
|
||||
Debug.Log("Ouch, you bastard.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Die()
|
||||
{
|
||||
Time.timeScale = 0;
|
||||
|
||||
//Time.timeScale = 0; //Wieder umlegen
|
||||
end.recentDeath = true;
|
||||
end.ShowDeathScreen();
|
||||
Debug.Log("rest in pizza");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -30,17 +30,23 @@ public class HandleEnergy : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
inventory.Energy = standardEnergy;
|
||||
energyHUD.text = inventory.Energy.ToString();
|
||||
Debug.Log(inventory.Energy);
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
energyHUD.text = inventory.Energy.ToString();
|
||||
}
|
||||
|
||||
public void LoseEnergy()
|
||||
{
|
||||
if (inventory.Energy > 0)
|
||||
inventory.Energy -= 1; //MAGIC NUMBER BEACHTEN ERSTMAL AUSPROBIEREN
|
||||
else
|
||||
{
|
||||
Debug.Log("Alle alle.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,31 @@
|
||||
using System.IO;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryData
|
||||
{
|
||||
// energy
|
||||
public int energy;
|
||||
|
||||
//Health
|
||||
public int health;
|
||||
|
||||
//Gems
|
||||
public int gems;
|
||||
|
||||
public InventoryData(int energy, int health, int gems)
|
||||
{
|
||||
this.energy = energy;
|
||||
this.health = health;
|
||||
this.gems = gems;
|
||||
}
|
||||
}
|
||||
|
||||
public class InventoryScript : MonoBehaviour
|
||||
{
|
||||
|
||||
string filename = "inventoryData.txt";
|
||||
string path;
|
||||
InventoryData inventoryData;
|
||||
|
||||
[Header("Variables")]
|
||||
//Energy
|
||||
@@ -16,25 +38,65 @@ public class InventoryScript : MonoBehaviour
|
||||
[SerializeField] int gems;
|
||||
|
||||
|
||||
public InventoryScript(int energy, int health, int gems)
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
this.energy = energy;
|
||||
this.health = health;
|
||||
this.gems = gems;
|
||||
path = Path.Combine(Application.persistentDataPath, filename);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
Debug.Log("erzeuge leeres File");
|
||||
File.WriteAllText(path, "");
|
||||
}
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
ReadInventoryData();
|
||||
}
|
||||
|
||||
public int Energy { get => energy; set => energy = value; }
|
||||
public int Health { get => health; set => health = value; }
|
||||
public int Gems { get => gems; set => gems = value; }
|
||||
|
||||
|
||||
void Start()
|
||||
public int Energy
|
||||
{
|
||||
|
||||
get { return energy; }
|
||||
set { energy = value; ValChanged(); }
|
||||
}
|
||||
|
||||
void Update()
|
||||
public int Health
|
||||
{
|
||||
|
||||
get { return health; }
|
||||
set { health = value; ValChanged(); }
|
||||
}
|
||||
public int Gems
|
||||
{
|
||||
get { return gems; }
|
||||
set { gems = value; ValChanged(); }
|
||||
}
|
||||
|
||||
// Achtung, ValChanged wird NICHT aufgerufen, wenn man die Werte im Inspector verändert,
|
||||
// weil das nur die Felder, nicht die Properties setzt.
|
||||
// Ändert man aber per Script die Property, wird ValChanged aufgerufen und sofort die Werte ins File gespeichert.
|
||||
// Solche Werte sollte man dann auch nicht in jedem Frame ändern, sondern nur, wenn der Spieler klickt.
|
||||
private void ValChanged()
|
||||
{
|
||||
Debug.Log("ValChanged");
|
||||
SaveInventoryData();
|
||||
}
|
||||
private void ReadInventoryData()
|
||||
{
|
||||
string datastring = File.ReadAllText(path);
|
||||
if (datastring == "")
|
||||
{
|
||||
Debug.Log("File ist noch leer, bitte erst etwas schreiben");
|
||||
return;
|
||||
}
|
||||
inventoryData = JsonUtility.FromJson<InventoryData>(datastring);
|
||||
energy = inventoryData.energy;
|
||||
health = inventoryData.health;
|
||||
gems = inventoryData.gems;
|
||||
}
|
||||
private void SaveInventoryData()
|
||||
{
|
||||
inventoryData = new InventoryData(energy, health, gems); // erstellt ein neues Objekt InventoryData aus den Werten dieses Scripts
|
||||
string datastring = JsonUtility.ToJson(inventoryData); // macht einen JSON String aus diesem Objekt (dafür brauchen wir es auch nur)
|
||||
File.WriteAllText(path, datastring);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user