Firstborn/Assets/RPG Creation Kit/Scripts/Player/PlayerStatus.cs

245 lines
6.8 KiB
C#
Raw Permalink Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPGCreationKit;
namespace RPGCreationKit.Player
{
public class PlayerStatus : PlayerBase, IHittable, IDamageable
{
public bool isAlive = true;
[SerializeField] GameObject bloodParticlePrefab;
[SerializeField] bool recoveringStamina = false;
[SerializeField] float recoverStaminaAmount = 10f;
[SerializeField] protected float recoverAfterActionDelay = 2.5f;
[SerializeField] bool recoveringMana = true;
[SerializeField] float recoverManaAmount = 5f;
[HideInInspector] private IDamageable PlayerDamageable;
public override void Start()
{
base.Start();
// Load/Set initial stats
playerAttributes.CurHealth = playerAttributes.MaxHealth;
playerAttributes.CurStamina = playerAttributes.MaxStamina;
PlayerDamageable = GetComponent<IDamageable>();
// Recover mana if set to
if (recoveringMana)
StartCoroutine(RecoverMana());
}
public void InvokeResetRecover(bool _customAmount = false, float _amount = 0.0f)
{
if (!_customAmount)
_amount = recoverAfterActionDelay;
Invoke("ResetRecover", _amount);
}
public override void Update()
{
base.Update();
if ((!isAlive || playerAttributes.CurHealth <= 0) && !PlayerDeathScreen.instance.ui.activeInHierarchy)
{
isAlive = false;
PlayerDeathScreen.instance.ShowDeathScreen();
}
}
public void ResetRecover()
{
recoveringStamina = true;
StartCoroutine(RecoverStamina());
}
public IEnumerator RecoverStamina()
{
while (recoveringStamina)
{
if (playerAttributes.CurStamina < playerAttributes.MaxStamina)
{
playerAttributes.CurStamina += recoverStaminaAmount * Time.deltaTime;
playerAttributes.CurStamina = Mathf.Clamp(playerAttributes.CurStamina, 0, playerAttributes.MaxStamina);
yield return null;
}
else
yield return null;
}
}
public IEnumerator RecoverMana()
{
while (recoveringMana)
{
if (playerAttributes.CurMana < playerAttributes.MaxMana)
{
playerAttributes.CurMana += recoverManaAmount * Time.deltaTime;
playerAttributes.CurMana = Mathf.Clamp(playerAttributes.CurMana, 0, playerAttributes.MaxMana);
yield return null;
}
else
yield return null;
}
}
public void StopRecoveringStamina()
{
StopCoroutine(RecoverStamina());
CancelInvoke("ResetRecover");
recoveringStamina = false;
}
void IDamageable.Damage(DamageContext damageContext)
{
if (!isAlive)
return;
// TODO do the equipment decrese of the damage (also have to do this for npcs)
float finalDamage = damageContext.amount;
finalDamage -= (finalDamage * equipment.ArmorRate) / 100;
if (finalDamage <= 0)
finalDamage = 1;
playerAttributes.DamageHealth(finalDamage, true, RCKSettings.PLAYER_DAMAGE_SPEED);
if (playerAttributes.CurHealth - finalDamage <= 0)
PlayerDeath();
}
void IDamageable.DamageBlocked(DamageContext damageContext)
{
if (!isAlive)
return;
// TODO do the equipment decrese of the damage (also have to do this for npcs)
float finalDamage = damageContext.amount;
finalDamage -= (finalDamage * equipment.ArmorRate) / 100;
finalDamage *= (equipment.isUsingShield) ? equipment.currentShield.blockingMultiplier : equipment.currentWeapon.BlockingMultiplier;
if (finalDamage <= 0)
finalDamage = 1;
playerAttributes.DamageStamina(finalDamage, true, RCKSettings.DRAIN_STAMINA_ON_ATTACKBLOCKED_SPEEDAMOUNT);
PlayerCombat.instance.fpcAnim.SetTrigger("hasBlockedAttack");
PlayerCombat.instance.tpsAnim.SetTrigger("hasBlockedAttack");
PlayerCombat.instance.fpcAnim.Update(0);
PlayerCombat.instance.tpsAnim.Update(0);
AudioClip blockingSound = (equipment.isUsingShield) ? null : equipment.currentWeapon.blockSound;
if (blockingSound != null)
PlayerCombat.instance.currentWeaponOnHand.PlayOneShot(blockingSound);
playerAttributes.DamageHealth(finalDamage, true, RCKSettings.PLAYER_DAMAGE_SPEED);
if (playerAttributes.CurHealth - finalDamage <= 0)
PlayerDeath();
}
void PlayerDeath()
{
isAlive = false;
PlayerDeathScreen.instance.ShowDeathScreen();
}
void IDamageable.Die()
{
throw new System.NotImplementedException();
}
void IDamageable.GenerateBlood()
{
}
void IHittable.GenerateDecal()
{
}
float IDamageable.GetCurrentHP()
{
return playerAttributes.CurHealth;
}
string IDamageable.GetEntityID()
{
return entityID;
}
string IDamageable.GetEntityName()
{
return entityName;
}
float IDamageable.GetMaxHP()
{
return playerAttributes.MaxHealth;
}
void IHittable.Hit(Vector3 hitDir, float forceAmount, DamageContext damageContext)
{
}
#region Unused
bool IDamageable.IsAlive()
{
return isAlive;
}
bool IDamageable.IsHostile()
{
return false;
}
bool IDamageable.IsHostileAgainstPC()
{
return false;
}
bool IDamageable.IsUnconscious()
{
return false;
}
bool IDamageable.ShouldDisplayHealthIfHostile()
{
return false;
}
#endregion
public bool Bleeds()
{
return bloodParticlePrefab != null;
}
public void InstantiateBlood(Vector3 pos)
{
var blood = Instantiate(bloodParticlePrefab, pos, Quaternion.identity, null);
Destroy(blood, 2.5f);
}
public IDamageable GetIDamageable()
{
return PlayerDamageable;
}
public Entity GetEntity()
{
return this;
}
}
}