UnityGameProjectsCode / Rise2Point0Game / Player / PlayerInvulnAbility.cs
PlayerInvulnAbility.cs
Raw
using UnityEngine;

public class PlayerInvulnAbility : MonoBehaviour
{
    //called from picked up invuln items to temporarily make the player immune to death by obstacles
    //while the timer is running, obstacles that come in contact with the player will not kill the player
    //a barrier of some kind will surround the player to show the effect is active
    //the gameplay canvas will show a timer for how long the invuln will last
    //once the time runs out, the invuln effect will end and the player can die again.
    //picking up invuln items while the effect is active will increase the time

    private float currentInvulnTime;
    public GameObject barrierEffect;
    private AchievementSystem achSys;

    private void Start()
    {
        achSys = ObjectManager.GetObject(4).GetComponent<AchievementSystem>();
    }

    private void Update()
    {
        if (currentInvulnTime > 0)
        {
            currentInvulnTime -= Time.deltaTime;
            GUIManager.ChangeInvulnText(Mathf.RoundToInt(currentInvulnTime));

            if (currentInvulnTime <= 0)
                DisableInvulnAbility();
        }
        else
            DisableInvulnAbility();
    }

    public void ActivateInvulnAbility(int secondsActive)
    {
        //called from item to enable the invuln ability
        currentInvulnTime += secondsActive;
        barrierEffect.SetActive(true);
        GUIManager.SetInvulnTextState(true);
        GUIManager.ChangeInvulnText(Mathf.RoundToInt(currentInvulnTime));
        achSys.IncrementInvulnTime(secondsActive);
    }

    public void DisableInvulnAbility()
    {
        currentInvulnTime = 0;
        barrierEffect.SetActive(false);
        GUIManager.SetInvulnTextState(false);
    }

    public bool GetInvulnActiveState()
    {
        if (currentInvulnTime > 0)
            return true;
        else return false;
    }
}