PlayerHealth

 avatar
unknown
plain_text
6 months ago
2.0 kB
9
Indexable
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class PlayerHealth : MonoBehaviour
{
    public int lives = 3;

    public Image Heart1;
    public Image Heart2;
    public Image Heart3;
    private float BounceNadeCooldown = 7;
    [SerializeField] TextMeshProUGUI cooldown1;
    //public Text cooldown1;


    private PlayerSpawnManager spawnManager;
     [SerializeField] private FirstPersonController fpController;


    void Start()
    {
        spawnManager = PlayerSpawnManager.Instance;
         if (fpController == null)
        fpController = GetComponent<FirstPersonController>();
    }

void Update()
{
    if (fpController == null)
    {
        Debug.LogWarning("fpController is null!");
        return;
    }


    Debug.Log(fpController.BounceNadeAvailable);

    if (BounceNadeCooldown > 0)
    {
        BounceNadeCooldown -= Time.deltaTime;
        DisplayTime(BounceNadeCooldown);
    }
}

    void DisplayTime(float timeToDisplay)
    {
        // Ensure time doesn't go negative in display
        if (timeToDisplay < 0)
        {
            timeToDisplay = 0;
        }

        //float minutes = Mathf.FloorToInt(timeToDisplay / 60);
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);

        cooldown1.text = seconds.ToString();
    }

    public void UDied()
    {
        if (lives == 3){
            Heart3.gameObject.SetActive(false);
        }
        else if (lives == 2){
            Heart2.gameObject.SetActive(false);
        }
        else if (lives == 1){
            Heart1.gameObject.SetActive(false);
        }

        lives--;        //Does this mean lives -=1?!

        if (lives > 0){
            // Respawn THIS PLAYER only
            spawnManager.RespawnPlayer(gameObject);
        }
        else{
            // Player is dead forever OR destroy only this player
            Destroy(gameObject);
        }
    }
}
Editor is loading...
Leave a Comment