Eye of the Nile Docs
Everything you need to know to get started!
Loading...
Searching...
No Matches
ObjectHealth Class Reference

Detailed Description

Handles health, damage, and death of an object/entity. If you want an object to have health, take damage, and die, put this script on it.

Documentation updated 9/15/2024

Author
Stephen Nuttall

Public Member Functions

virtual void ResetHealth ()
 Set currentHealth back to maxHealth.
 
virtual void TakeDamage (int damage)
 If something attacks the object, it will run ObjectHealth.TakeDamage()
 
virtual void TakeDamage (Transform attacker, int damage)
 If something attacks the object, it will run ObjectHealth.TakeDamage() This overload allows for damage particles to come from the direction of the attack.
 
IEnumerator SetOnFire (int damageCount, float damageSpeed, int damage)
 Starts damaging the object (with FireDamage()) repeatedly. A fire sprite will appear over the object while it's on fire.

Parameters
damageCountThe amount of times the fire will deal damage before the object stops being on fire.
damageSpeedThe amount of seconds between each round of dealing damage.
damageThe amount of damage to deal each time.

 
void FireImmunity (float seconds)
 Start a coroutine for FireImmunityTimer().
 
int GetMaxHealth ()
 \breif Return the object's max health.
 

Public Attributes

UnityEvent OnDeath
 Invokes any subscribed functions when the object dies.
 

Protected Member Functions

IEnumerator Invincibility ()
 Makes object invincible for invincibleDuration seconds. Toggles transparency every invincibleFlash seconds.
 
void ToggleTransparency (bool isOn)
 Toggles the reduced opacity of the sprite that is used to create the flashing effect when invincible.
 
virtual void FireDamage (int damage)
 Similar functionality to TakeDamage(), but no invincibility is triggered and no animation players. Used when on fire.
 
virtual void Die ()
 Triggered when the object runs out of health. Plays a death sound and animation, drops souls/god souls, and disables the object.
 
IEnumerator FireImmunityTimer (float seconds)
 Make the object immune to fire for the given amount of seconds.
 

Protected Attributes

SpriteRenderer sRenderer
 Reference to the object’s sprite renderer.
 
Animator animator
 
Transform hurtEffect
 
SpriteRenderer onFireSprite
 
EventReference deathSfxName
 Reference to the sound effect the object should play when it dies. Nothing will play if left blank.
 
bool enableDamageParticles = true
 If enabled, the object will spawn the given hurtEffect particles when taking damage.
 
bool disableColliderOnDeath = true
 If enabled, the object's Collider2D component will be disabled when it dies.
 
bool disableRigidbodyOnDeath = true
 If enabled, the object's Rigidbody2D component will be disabled when it dies.
 
bool destroyOnDeath = false
 If enabled, the object will be destroyed when it dies.
 
int maxHealth = 100
 The maximum health the object can have.
 
int soulsDroppedOnDeath = 0
 The amount of souls the object should grant the player upon death. To disable, set to <= 0.
 
int godSoulsDroppedOnDeath = 0
 The amount of god souls the object should grant the player upon death. To disable, set to <= 0.
 
bool canBeInvincible = false
 If enabled, the object will be invincible for a short duration after taking damage (invincibility frames).
 
float invincibleDuration = 3f
 In seconds, how long invincibility lasts for after taking damage.
 
float flashDuration = 0.25f
 In seconds, how often the sprite should swap between being opaque and transparent (creating a flashing effect).
 
WaitForSeconds invincibleFlash
 The WaitForSeconds variable that is used to implement flashDuration.
 
bool isInvincible = false
 If the object is currently invincible.
 
bool canBeOnFire = true
 Whether or not the object can be set on fire.
 
bool onFire = false
 Whether or not the object is currently on fire.
 
bool fireImmune = false
 Whether or not the object currently has immunity to fire (usually from an ability).
 

Properties

bool IsDead [get]
 The object is dead if the currentHealth falls to 0.
 
int currentHealth [get, protected set]
 The amount of health the object currently has.
 

Events

static Action< int > soulsDropped
 Triggered when the object dies (if soulsDroppedOnDeath > 0), telling the DataManager that the amount of souls the player has collected has increased.
 
static Action< int > godSoulsDropped
 Triggered when the object dies (if godSoulsDroppedOnDeath > 0), telling the DataManager that the amount of god souls the player has collected has increased.
 

Private Member Functions

void Awake ()
 Initialize invincibleFlash.
 
void Start ()
 Set currentHealth to maxHealth.
 

Private Attributes

EventReference setOnFireSFX
 Reference to the sound effect that plays when the object is set on fire.
 

Member Function Documentation

◆ Awake()

void ObjectHealth.Awake ( )
private

Initialize invincibleFlash.

◆ Die()

virtual void ObjectHealth.Die ( )
protectedvirtual

Triggered when the object runs out of health. Plays a death sound and animation, drops souls/god souls, and disables the object.

Reimplemented in PlayerHealth.

◆ FireDamage()

virtual void ObjectHealth.FireDamage ( int damage)
protectedvirtual

Similar functionality to TakeDamage(), but no invincibility is triggered and no animation players. Used when on fire.

Reimplemented in PlayerHealth.

◆ FireImmunity()

void ObjectHealth.FireImmunity ( float seconds)

Start a coroutine for FireImmunityTimer().

◆ FireImmunityTimer()

IEnumerator ObjectHealth.FireImmunityTimer ( float seconds)
protected

Make the object immune to fire for the given amount of seconds.

◆ GetMaxHealth()

int ObjectHealth.GetMaxHealth ( )

\breif Return the object's max health.

◆ Invincibility()

IEnumerator ObjectHealth.Invincibility ( )
protected

Makes object invincible for invincibleDuration seconds. Toggles transparency every invincibleFlash seconds.

◆ ResetHealth()

virtual void ObjectHealth.ResetHealth ( )
virtual

Set currentHealth back to maxHealth.

◆ SetOnFire()

IEnumerator ObjectHealth.SetOnFire ( int damageCount,
float damageSpeed,
int damage )

Starts damaging the object (with FireDamage()) repeatedly. A fire sprite will appear over the object while it's on fire.

Parameters
damageCountThe amount of times the fire will deal damage before the object stops being on fire.
damageSpeedThe amount of seconds between each round of dealing damage.
damageThe amount of damage to deal each time.

◆ Start()

void ObjectHealth.Start ( )
private

Set currentHealth to maxHealth.

◆ TakeDamage() [1/2]

virtual void ObjectHealth.TakeDamage ( int damage)
virtual

If something attacks the object, it will run ObjectHealth.TakeDamage()

Steps:

Parameters
damageThe amount of damage to deal to this object.
  • If the object is currently invincible, skip this function.
  • Subtract the damage done and play the damage animation.
  • If the health is now beneath 0, die (respectfully).
  • If not, start invinciblity frames (if enabled).

◆ TakeDamage() [2/2]

virtual void ObjectHealth.TakeDamage ( Transform attacker,
int damage )
virtual

If something attacks the object, it will run ObjectHealth.TakeDamage() This overload allows for damage particles to come from the direction of the attack.

Steps:

Parameters
attackerReference to the transform of the attacker. Used to determine which direction particles should come from.
damageThe amount of damage to deal to this object.
  • If the object is currently invincible, skip this function.
  • Subtract the damage done and play the damage animation.
  • Generate hurt particles (if enabled and assigned).
  • If the health is now beneath 0, die (respectfully).
  • If not, start invinciblity frames (if enabled).

Reimplemented in PlayerHealth, and PotHealth.

◆ ToggleTransparency()

void ObjectHealth.ToggleTransparency ( bool isOn)
protected

Toggles the reduced opacity of the sprite that is used to create the flashing effect when invincible.

Member Data Documentation

◆ animator

Animator ObjectHealth.animator
protected

Reference to the object’s animator.

Note
Animations will be skipped if an animator is not assigned in the Unity Editor. No risk of unassigned errors!

◆ canBeInvincible

bool ObjectHealth.canBeInvincible = false
protected

If enabled, the object will be invincible for a short duration after taking damage (invincibility frames).

◆ canBeOnFire

bool ObjectHealth.canBeOnFire = true
protected

Whether or not the object can be set on fire.

◆ deathSfxName

EventReference ObjectHealth.deathSfxName
protected

Reference to the sound effect the object should play when it dies. Nothing will play if left blank.

◆ destroyOnDeath

bool ObjectHealth.destroyOnDeath = false
protected

If enabled, the object will be destroyed when it dies.

Note
If enabled, disableColliderOnDeath and disableRigidbodyOnDeath will be ignored because they would be redundant.

◆ disableColliderOnDeath

bool ObjectHealth.disableColliderOnDeath = true
protected

If enabled, the object's Collider2D component will be disabled when it dies.

◆ disableRigidbodyOnDeath

bool ObjectHealth.disableRigidbodyOnDeath = true
protected

If enabled, the object's Rigidbody2D component will be disabled when it dies.

◆ enableDamageParticles

bool ObjectHealth.enableDamageParticles = true
protected

If enabled, the object will spawn the given hurtEffect particles when taking damage.

◆ fireImmune

bool ObjectHealth.fireImmune = false
protected

Whether or not the object currently has immunity to fire (usually from an ability).

◆ flashDuration

float ObjectHealth.flashDuration = 0.25f
protected

In seconds, how often the sprite should swap between being opaque and transparent (creating a flashing effect).

◆ godSoulsDroppedOnDeath

int ObjectHealth.godSoulsDroppedOnDeath = 0
protected

The amount of god souls the object should grant the player upon death. To disable, set to <= 0.

◆ hurtEffect

Transform ObjectHealth.hurtEffect
protected

Reference to the particle effects the object should spawn when taking damage. Can be toggled with enableDamageParticles.

Note
This will not be instantiated if a particle effect is not assigned in the Unity Editor. No risk of unassigned errors!

◆ invincibleDuration

float ObjectHealth.invincibleDuration = 3f
protected

In seconds, how long invincibility lasts for after taking damage.

◆ invincibleFlash

WaitForSeconds ObjectHealth.invincibleFlash
protected

The WaitForSeconds variable that is used to implement flashDuration.

◆ isInvincible

bool ObjectHealth.isInvincible = false
protected

If the object is currently invincible.

◆ maxHealth

int ObjectHealth.maxHealth = 100
protected

The maximum health the object can have.

◆ OnDeath

UnityEvent ObjectHealth.OnDeath

Invokes any subscribed functions when the object dies.

◆ onFire

bool ObjectHealth.onFire = false
protected

Whether or not the object is currently on fire.

◆ onFireSprite

SpriteRenderer ObjectHealth.onFireSprite
protected

Reference to the sprite that should be overlaid the object’s sprite when the object is on fire.

Note
This will not be displayed if a sprite renderer is not assigned in the Unity Editor. No risk of unassigned errors!

◆ setOnFireSFX

EventReference ObjectHealth.setOnFireSFX
private

Reference to the sound effect that plays when the object is set on fire.

◆ soulsDroppedOnDeath

int ObjectHealth.soulsDroppedOnDeath = 0
protected

The amount of souls the object should grant the player upon death. To disable, set to <= 0.

◆ sRenderer

SpriteRenderer ObjectHealth.sRenderer
protected

Reference to the object’s sprite renderer.

Note
This will default to this object's sprite renderer if one not assigned in the Unity Editor. No risk of unassigned errors!

Property Documentation

◆ currentHealth

int ObjectHealth.currentHealth
getprotected set

The amount of health the object currently has.

◆ IsDead

bool ObjectHealth.IsDead
get

The object is dead if the currentHealth falls to 0.

Event Documentation

◆ godSoulsDropped

Action<int> ObjectHealth.godSoulsDropped
static

Triggered when the object dies (if godSoulsDroppedOnDeath > 0), telling the DataManager that the amount of god souls the player has collected has increased.

◆ soulsDropped

Action<int> ObjectHealth.soulsDropped
static

Triggered when the object dies (if soulsDroppedOnDeath > 0), telling the DataManager that the amount of souls the player has collected has increased.


The documentation for this class was generated from the following file: