Created
November 5, 2019 00:26
-
-
Save awhiskin/1e171609969d7782208f43b082ab2e08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Entities | |
{ | |
public class LivingEntity : BaseEntity, IEntityHealth | |
{ | |
[SerializeField] | |
protected float maxHealth = 100f; | |
public float currentHealth { get; protected set; } | |
public bool isAlive { get; protected set; } | |
protected override void Awake() | |
{ | |
base.Awake(); | |
InitialiseHealth(); | |
} | |
public bool IsAlive() { return isAlive; } | |
protected virtual void InitialiseHealth() | |
{ | |
currentHealth = maxHealth; | |
isAlive = true; | |
} | |
public virtual void AddHealth(float amount) | |
{ | |
if (isAlive) | |
{ | |
if (currentHealth == maxHealth) | |
return; | |
float absAmount = Mathf.Abs(amount); | |
currentHealth += absAmount; | |
Debug.Log(this.gameObject.name + " had " + absAmount + " health added."); | |
ClampHealth(); | |
} | |
} | |
public virtual void TakeDamage(float amount, DamageType type) | |
{ | |
if (isAlive) | |
{ | |
if (currentHealth <= 0f) | |
return; | |
float absAmount = Mathf.Abs(amount); | |
currentHealth -= absAmount; | |
// Debug.Log(this.gameObject.name + " took " + absAmount + " damage."); | |
ClampHealth(); | |
if (currentHealth <= 0) | |
{ | |
this.Die(type); | |
} | |
} | |
} | |
public virtual void Die(DamageType type) | |
{ | |
if (isAlive) | |
isAlive = false; | |
} | |
private void ClampHealth() | |
{ | |
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth); | |
} | |
public float GetMaxHealth() | |
{ | |
return this.maxHealth; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment