Timer eingebaut, Inventory Script überarbeitet, InventoryScript dank Hr. Neuhaus durch Speicherfunktion erweiter, GameOver und GameOverlay ready and working
This commit is contained in:
@@ -11,9 +11,15 @@ public class GameOver : MonoBehaviour
|
||||
|
||||
[SerializeField] GameObject deathPanel;
|
||||
private float duration = 4f;
|
||||
[SerializeField] float timer;
|
||||
[SerializeField] public float timer;
|
||||
[SerializeField] TMP_Text deathTime;
|
||||
[SerializeField] bool recentDeath;
|
||||
[SerializeField] TMP_Text infoText;
|
||||
[SerializeField] public bool recentDeath;
|
||||
|
||||
bool timerOn = false;
|
||||
|
||||
public float etwas;
|
||||
public float zeit;
|
||||
|
||||
[Header("ScriptPile")]
|
||||
|
||||
@@ -33,39 +39,53 @@ public class GameOver : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
recentDeath = false; //Eigentlicher Wert
|
||||
deathPanel.SetActive(false);
|
||||
recentDeath = true;
|
||||
timer = 10;
|
||||
|
||||
recentDeath = true; //ZUM TESTEN
|
||||
timer = 0;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
recentDeath = true;
|
||||
if (recentDeath)
|
||||
{
|
||||
deathTime.text = Convert.ToInt32(timer -= Time.deltaTime).ToString();
|
||||
|
||||
if(timer >= 0)
|
||||
{
|
||||
StopBothering();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
StopBothering();
|
||||
ShowDeathScreen();
|
||||
CheckDeathTime();
|
||||
timerOn = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowDeathScreen()
|
||||
{
|
||||
deathPanel.SetActive(true);
|
||||
infoText.text = "Try again?";
|
||||
infoText.text = inventory.Gems.ToString(); //ZUM DEBUGGEN
|
||||
|
||||
//OPTION INS MAINMENUE ZURÜCKZUKEHREN
|
||||
}
|
||||
|
||||
public void GiveNewChance()
|
||||
{
|
||||
if (inventory.Gems > 100)
|
||||
{
|
||||
NewChance();
|
||||
inventory.Gems -= 100;
|
||||
}
|
||||
|
||||
else //EINBAUEN, DASS MAN DORT GEMS KAUFEN KANN
|
||||
{
|
||||
infoText.text = "You don´t have enough gems to do that.";
|
||||
}
|
||||
}
|
||||
|
||||
public void StopBothering()
|
||||
{
|
||||
recentDeath = false;
|
||||
Time.timeScale = 1; //UNSICHER OB LAden VON SCENE DEN WERT AUTO. AUF 1 SETZT
|
||||
SceneManager.LoadSceneAsync(0);
|
||||
Time.timeScale = 1; //UNSICHER OB LADEN VON SCENE DEN WERT AUTO. AUF 1 SETZT
|
||||
Debug.Log("Jetzt würdest du wieder in die MainMenueScene geschmissen werden");
|
||||
//SceneManager.LoadSceneAsync(0); //MUSS AM ENDE WIEDER EINGEBAUT WERDEN
|
||||
}
|
||||
|
||||
|
||||
@@ -73,10 +93,33 @@ public class GameOver : MonoBehaviour
|
||||
{
|
||||
inventory.Health = healthScript.standardHealth;
|
||||
|
||||
if (battery.standardEnergy <= (inventory.Energy + battery.addEnergyValue))
|
||||
if (battery.standardEnergy >= (inventory.Energy + battery.addEnergyValue))
|
||||
{
|
||||
inventory.Energy += battery.addEnergyValue;
|
||||
}
|
||||
|
||||
//Logik: bei Retry, soll der Player nochmal neue Energie bekommen
|
||||
|
||||
}
|
||||
|
||||
public void CheckDeathTime()
|
||||
{
|
||||
if(timerOn == true)
|
||||
{
|
||||
timer = 10;
|
||||
zeit -= Time.deltaTime;
|
||||
|
||||
deathTime.text = Convert.ToInt32(zeit + timer).ToString();
|
||||
|
||||
if (zeit >= timer)
|
||||
{
|
||||
recentDeath = false;
|
||||
StopBothering();
|
||||
|
||||
timerOn = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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