Files
GameDevTVRocketBoost/Assets/Scripts/PlayerStats.cs
2026-01-09 17:21:32 +00:00

91 lines
2.5 KiB
C#

using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerStats : MonoBehaviour
{
float curHealth;
[SerializeField] float maxHealth = 100;
float curFuel;
[SerializeField] float maxFuel = 100;
float curTime;
[SerializeField] float timeLimit = 300; // in seconds
[SerializeField] TextMeshProUGUI healthText, fuelText, timeText;
void Start()
{
curHealth = maxHealth;
curFuel = maxFuel;
curTime = timeLimit;
healthText.text = "Health: " + curHealth.ToString("F0");
fuelText.text = "Fuel: " + curFuel.ToString("F0");
timeText.text = "Time: " + timeLimit.ToString("F0");
}
void Update()
{
if (curTime > 0)
{
curTime -= 1 * Time.deltaTime;
int minutes = Mathf.FloorToInt(curTime / 60F);
int seconds = Mathf.FloorToInt(curTime - minutes * 60);
timeText.text = string.Format("{0:0}:{1:00}", minutes, seconds);
}
else
{
curTime = 0;
Destroy(gameObject);
ReloadLevel();
}
}
public void TakeDamage(int damage)
{
curHealth -= damage;
healthText.text = "Health: " + curHealth.ToString("F0");
if (curHealth <= 0)
{
curHealth = 0;
Destroy(gameObject);
ReloadLevel();
}
}
public void UseFuel(float amount)
{
curFuel -= amount;
fuelText.text = "Fuel: " + curFuel.ToString("F0");
if (curFuel <= 0)
{
curFuel = 0;
Destroy(gameObject);
ReloadLevel();
}
}
public void StopTime()
{
curTime = 0;
timeText.text = "0:00";
}
public void AddTime(float amount)
{
curTime += amount;
int minutes = Mathf.FloorToInt(curTime / 60F);
int seconds = Mathf.FloorToInt(curTime - minutes * 60);
timeText.text = string.Format("{0:0}:{1:00}", minutes, seconds);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
public void NextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
}