RiftMayhem/Assets/Scripts/Resource.cs
Pedro Gomes 2773ef7d6e Player Death Update
- Players can now die (finally)
- Solo players have a single cheat death per scene (reviving automatically after the first death)
- group players have revive mechanic, where a player faints when his health gets to 0, creating a revive circle around him, other players can stand on it to revive him. if after x seconds they don't get revived they bleed out and stay perma death until scene changes or all players die
- Multiple VFX added using post processing for cheat death, fainting, reviving, and perma death events.
- stopped players from moving and pressing keys when dead
- enemies now change target if they try to attack a dead/fainted target.
2024-07-20 19:49:14 +01:00

101 lines
2.4 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Resource : MonoBehaviour, IPunObservable
{
[SerializeField] protected float currentValue;
[SerializeField] protected float maxValue;
[SerializeField] protected float flatRegen;
[SerializeField] protected float percentRegen;
[SerializeField] protected float timeBetweenRegens;
protected bool canRegen = true;
public UnityEvent<float> onResourceChanged = new UnityEvent<float>();
protected float baseMaxValue;
protected float baseFlatRegen;
// Start is called before the first frame update
protected virtual void Start()
{
currentValue = maxValue;
onResourceChanged.Invoke(currentValue);
StartCoroutine(RegenResource());
}
public virtual void ChangeValue(float value)
{
currentValue += value;
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
onResourceChanged.Invoke(currentValue);
}
public float GetCurrentValue()
{
return currentValue;
}
public virtual float GetMaxValue()
{
return maxValue;
}
public virtual float GetBaseMaxValue()
{
return baseMaxValue;
}
public virtual void SetCurrentValue(float value)
{
currentValue = value;
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
onResourceChanged.Invoke(currentValue);
}
public virtual void SetMaxValue(float value)
{
maxValue = value;
}
protected IEnumerator RegenResource()
{
while (canRegen)
{
yield return new WaitForSeconds(timeBetweenRegens);
ChangeValue(flatRegen + (maxValue * percentRegen/100f));
}
StartCoroutine(RestartRegen());
}
protected IEnumerator RestartRegen()
{
while (!canRegen)
{
yield return new WaitForSeconds(timeBetweenRegens);
}
StartCoroutine(RegenResource());
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(currentValue);
stream.SendNext(maxValue);
}
else if (stream.IsReading)
{
SetCurrentValue((float)stream.ReceiveNext());
SetMaxValue((float)stream.ReceiveNext());
}
}
}