54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
using UnityEditor.Search;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class PlayerStats : MonoBehaviour
|
|
{
|
|
[SerializeField] int maxHealth = 100;
|
|
int currentHealth;
|
|
[SerializeField] float timeRemaining = 60f;
|
|
[SerializeField] TextMeshProUGUI healthText, timeText;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
currentHealth = maxHealth;
|
|
healthText.text = "Health: " + currentHealth.ToString();
|
|
timeText.text = "Time: " + Mathf.RoundToInt(timeRemaining).ToString();
|
|
InvokeRepeating("CountdownTimer", 1f, 1f);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
public void TakeDamage(int damageAmount)
|
|
{
|
|
currentHealth -= damageAmount;
|
|
healthText.text = "Health: " + currentHealth.ToString();
|
|
if (currentHealth <= 0)
|
|
{
|
|
Die();
|
|
}
|
|
}
|
|
void Die()
|
|
{
|
|
Debug.Log("Player has died.");
|
|
gameObject.SetActive(false);
|
|
}
|
|
void CountdownTimer()
|
|
{
|
|
if (timeRemaining > 0)
|
|
{
|
|
timeRemaining -= Time.deltaTime;
|
|
timeText.text = "Time: " + Mathf.RoundToInt(timeRemaining).ToString();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Time's up!");
|
|
Die();
|
|
}
|
|
}
|
|
}
|