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

Detailed Description

Functionality of ObjectHealth, specifically for the player. This script inherits from ObjectHealth. Here are the key changes:

  • Some variables get their values from the PlayerStatHolder component instead of inside this script.
  • Support for damage resistance and fire resistance stats.
  • When the player dies, Anubis.unity is loaded, where Anubis tells joke based on how the player died.
  • 5 different events are available for other functions in other scripts to subscribe to. Many of these are used by the data manager.
  • A function is available mainly for abilities to use that instantly heals the player.

Documentation updated 8/30/2024

Author
Stephen Nuttall

Public Member Functions

override void TakeDamage (Transform attacker, int damage)
 Apply damage to the player. If something attacks the player, it will run PlayerHealth.TakeDamage(). See detailed view for key differences between this an ObjectHealth.TakeDamage(), as well as the steps this function takes.
 
void HealInstant (int healValue)
 Instantly heals the player.
 
int GetHealth ()
 Returns the amount of health the player currently has.
 
void InvokeHealthChange ()
 Checks if the player's health is above the max health, and if so, corrects it. Also invokes onPlayerHealthChange. Used to update the player's health when changed from outside the script.
 
- Public Member Functions inherited from ObjectHealth
virtual void ResetHealth ()
 Set currentHealth back to maxHealth.
 
virtual void TakeDamage (int damage)
 If something attacks the object, it will run ObjectHealth.TakeDamage()
 
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.
 

Protected Member Functions

override void FireDamage (int damage)
 Similar functionality to TakeDamage(), but no invincibility is triggered and no animation players. Used when on fire.
 
override void Die ()
 Runs when the player dies to stop gameplay and run AfterDeath(). Runs an animation, disables collision, physics, and player movement, and starts a corountine for AfterDeath().
 
- Protected Member Functions inherited from ObjectHealth
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.
 
IEnumerator FireImmunityTimer (float seconds)
 Make the object immune to fire for the given amount of seconds.
 

Private Member Functions

void OnEnable ()
 Subscribe to health potion used event.
 
void OnDisable ()
 Unsubscribe from health potion used event.
 
void Awake ()
 Initialize references and invincibleFlash. Start/restart healing without delay.
 
void Start ()
 Set the current health of the player to the data manager's copy of it, and invoke onPlayerHealthChange.
 
IEnumerator AfterDeath ()
 After the player has died, load Anubis.unity and renable critical components. See detailed view for steps.
 
void StartHealingCoroutine (bool withDelay=true)
 Stop any ongoing healAfterDamage coroutines and start a new one.
 
IEnumerator healAfterDamage (float delay, float speed, int amount)
 Wait delay seconds, then heal the player by amount every speed seconds. GOes until full health or coroutine is cancelled.
 

Private Attributes

DataManager dataManager
 Reference to the data manager.
 

Player Stats

These variables are retrieved from the PlayerStatHolder component, rather than being stored in here. (see StatsAE for more info)

PlayerStatHolder PStats [get, private set]
 
int MaxHealth [get]
 The maximum health of the player.
 
int DamageResistance [get]
 The amount of damage resistance the player currently has. This value is subtracted from any amount of non-fire damage the player takes. If the resulting number is <= 0, no damage is done at all.
 
int FireResistance [get]
 The amount of fire reistance the player currently has. The value is subtracted from any amount of fire damage the player takes. If the resulting number is <= 0, no damage is done at all.
 

Dying and Post-Death

Information related to the the transition to Anubis.unity after the player dies, and what joke Anubis should tell.

string currentDeathMessage [get, private set]
 The current joke for Anubis to read after the player dies. If the attacking object doesn't have a CustomDeathMessage component, this will either be set to [DEFAULT] or [FIRE].
 
float deadFadeDelay = 1f
 
float deadFadeLength = 1f
 The amount of time given for the fade to black animation to play when loading Anubis.unity after death, in seconds.
 
string deathSceneName = "Anubis"
 The name of the scene to load after the player dies (Anubis.unity).
 
string defaultDeathMessage = "[DEFAULT]"
 If currentDeathMessage is set to this, AnubisJokeTextbox will know to read from the default list of jokes.
 
string fireDeathMessage = "[FIRE]"
 

Healing

The player slowly heals after taking damage. These are variables related to that.

float healDelay = 5f
 
float healSpeed = 0.2f
 How many seconds should be between each health increase when healing.
 
int healAmount = 1
 How much to heal the player by when healing.
 
Coroutine healingCoroutine
 Reference to the coroutine responsible for healing the player over time.
 

Events

When these events occur, other scripts subscribed to these events can be notified and trigger their functionality

static Action onPlayerDeath
 
static Action onPlayerRespawn
 Triggers any subscribes functions when the player respawns.
 
static Action< int > onPlayerDamage
 Triggers any subscribes functions when the player takes damage.
 
static Action< int > onPlayerHealthChange
 Triggers any subscribes functions when the player's health changes.
 
static Action< string > deathMessageChange
 Triggers any subscribes functions when the player's custom death message changes.
 

Additional Inherited Members

- Public Attributes inherited from ObjectHealth
UnityEvent OnDeath
 Invokes any subscribed functions when the object dies.
 
- Protected Attributes inherited from ObjectHealth
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 inherited from ObjectHealth
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 inherited from ObjectHealth
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.
 

Member Function Documentation

◆ AfterDeath()

IEnumerator PlayerHealth.AfterDeath ( )
private

After the player has died, load Anubis.unity and renable critical components. See detailed view for steps.

Steps:

  • Wait a brief amount of time after dying to allow the user to see what happened.
  • After the fade to black animation is over, re-enable collision, physics, and player movement.
  • Let any other objects subscribed to onPlayerRespawn and onPlayerHealthChange know that these have happened.

◆ Awake()

void PlayerHealth.Awake ( )
private

Initialize references and invincibleFlash. Start/restart healing without delay.

◆ Die()

override void PlayerHealth.Die ( )
protectedvirtual

Runs when the player dies to stop gameplay and run AfterDeath(). Runs an animation, disables collision, physics, and player movement, and starts a corountine for AfterDeath().

Reimplemented from ObjectHealth.

◆ FireDamage()

override void PlayerHealth.FireDamage ( int damage)
protectedvirtual

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

Parameters
damageThe amount of damage being delt to the player.
  • Stop any ongoing healing and start a new one with a delay.

Reimplemented from ObjectHealth.

◆ GetHealth()

int PlayerHealth.GetHealth ( )

Returns the amount of health the player currently has.

◆ healAfterDamage()

IEnumerator PlayerHealth.healAfterDamage ( float delay,
float speed,
int amount )
private

Wait delay seconds, then heal the player by amount every speed seconds. GOes until full health or coroutine is cancelled.

◆ HealInstant()

void PlayerHealth.HealInstant ( int healValue)

Instantly heals the player.

Parameters
healValueAmount of health to heal the player by.

◆ InvokeHealthChange()

void PlayerHealth.InvokeHealthChange ( )

Checks if the player's health is above the max health, and if so, corrects it. Also invokes onPlayerHealthChange. Used to update the player's health when changed from outside the script.

◆ OnDisable()

void PlayerHealth.OnDisable ( )
private

Unsubscribe from health potion used event.

◆ OnEnable()

void PlayerHealth.OnEnable ( )
private

Subscribe to health potion used event.

◆ Start()

void PlayerHealth.Start ( )
private

Set the current health of the player to the data manager's copy of it, and invoke onPlayerHealthChange.

◆ StartHealingCoroutine()

void PlayerHealth.StartHealingCoroutine ( bool withDelay = true)
private

Stop any ongoing healAfterDamage coroutines and start a new one.

◆ TakeDamage()

override void PlayerHealth.TakeDamage ( Transform attacker,
int damage )
virtual

Apply damage to the player. If something attacks the player, it will run PlayerHealth.TakeDamage(). See detailed view for key differences between this an ObjectHealth.TakeDamage(), as well as the steps this function takes.

Key differences between this an ObjectHealth.TakeDamage():

  • Damage cannot be applied if the damage resistance is greater than or equal to the amount of damage being applied
  • Events are invoked

Steps:

Parameters
attackerReference to the object that is attacking the player.
damageThe amount of damage being delt to the player.
  • Damage can only be applied if the player is not invincible and the damage is more than the damage resistance.
  • Generate hurt particles (if enabled).
  • Let any other objects subscribed to onPlayerDamage or onPlayerHealthChange know that those have happened.
  • Stop any ongoing healing and start a new one with a delay.
  • If the player is at 0 health, they are dead:
  • Using the transform of the attacker passed as a parameter,
  • Then, trigger the sequence of events that happens when the player dies
  • If the player is still alive, start invinciblity frames (if enabled).

Reimplemented from ObjectHealth.

Member Data Documentation

◆ dataManager

DataManager PlayerHealth.dataManager
private

Reference to the data manager.

◆ deadFadeDelay

float PlayerHealth.deadFadeDelay = 1f
private

The amount of time to wait after death before loading Anubis.unity, in seconds.

◆ deadFadeLength

float PlayerHealth.deadFadeLength = 1f
private

The amount of time given for the fade to black animation to play when loading Anubis.unity after death, in seconds.

◆ deathSceneName

string PlayerHealth.deathSceneName = "Anubis"
private

The name of the scene to load after the player dies (Anubis.unity).

◆ defaultDeathMessage

string PlayerHealth.defaultDeathMessage = "[DEFAULT]"
private

If currentDeathMessage is set to this, AnubisJokeTextbox will know to read from the default list of jokes.

◆ fireDeathMessage

string PlayerHealth.fireDeathMessage = "[FIRE]"
private

If currentDeathMessage is set to this, AnubisJokeTextbox will know to read from the fire list of jokes (as in fire related. They are fire jokes though).

◆ healAmount

int PlayerHealth.healAmount = 1
private

How much to heal the player by when healing.

◆ healDelay

float PlayerHealth.healDelay = 5f
private

How long (in seconds) to wait after taking damage to start healing.

◆ healingCoroutine

Coroutine PlayerHealth.healingCoroutine
private

Reference to the coroutine responsible for healing the player over time.

◆ healSpeed

float PlayerHealth.healSpeed = 0.2f
private

How many seconds should be between each health increase when healing.

Property Documentation

◆ currentDeathMessage

string PlayerHealth.currentDeathMessage
getprivate set

The current joke for Anubis to read after the player dies. If the attacking object doesn't have a CustomDeathMessage component, this will either be set to [DEFAULT] or [FIRE].

◆ DamageResistance

int PlayerHealth.DamageResistance
get

The amount of damage resistance the player currently has. This value is subtracted from any amount of non-fire damage the player takes. If the resulting number is <= 0, no damage is done at all.

◆ FireResistance

int PlayerHealth.FireResistance
get

The amount of fire reistance the player currently has. The value is subtracted from any amount of fire damage the player takes. If the resulting number is <= 0, no damage is done at all.

◆ MaxHealth

int PlayerHealth.MaxHealth
get

The maximum health of the player.

◆ PStats

PlayerStatHolder PlayerHealth.PStats
getprivate set

Reference to the PlayerStatHolder itself. Holds various information that ability stat modifiers (StatsAE) can modify.

Event Documentation

◆ deathMessageChange

Action<string> PlayerHealth.deathMessageChange
static

Triggers any subscribes functions when the player's custom death message changes.

◆ onPlayerDamage

Action<int> PlayerHealth.onPlayerDamage
static

Triggers any subscribes functions when the player takes damage.

◆ onPlayerDeath

Action PlayerHealth.onPlayerDeath
static

Triggers any subscribes functions when the player dies.

◆ onPlayerHealthChange

Action<int> PlayerHealth.onPlayerHealthChange
static

Triggers any subscribes functions when the player's health changes.

◆ onPlayerRespawn

Action PlayerHealth.onPlayerRespawn
static

Triggers any subscribes functions when the player respawns.


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