Skip to main content

PlayerHealth

A class in Root.Township, the global namespace.

A player's health. The server applies PvP, damage multiplier and immortality rules; reaching zero health downs the player instead of killing them. The local client shows hurt, hunger and nightmare effects.

Constructors​

MemberWhat it is
PlayerHealth()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
static ToggleBool ArePlayersAvoidingDamageThe immortals toggle. While on, players' health can't drop below its maximum.
17 private and internal fields
MemberWhat it is
private float baseSaturationUnused saturation value (1).
private Coroutine hungerBlurCoroutineRunning BlurEffect, or null.
private Stat hungerStatThe player's hunger stat, local player only.
private Coroutine hurtVignetteRunning HurtVignette, or null.
private bool isInvincibleToImpactsServer: true during GrantInvincibilityToImpacts; impact damage is ignored.
private int lastHungerLevelHunger level last seen by UpdateHunger.
private PlayerController playerBacking field for Player.
private IPlayerControllerMultiLockModel playerControllerMultiLockModelInjected model that says whether the player's controls are locked.
private DamageState playerDamageStateServer: reused state passed to HandleDamage handlers so they can block damage.
private IPostProcessController postProcessControllerInjected controller for blur, vignette and saturation.
private Stat recentDamageStatServer: stat set to 1 while the player was recently damaged.
private float recentDamageTimerSeconds left before the recent damage stat clears.
private Coroutine recentDamageTimerCoroutineRunning RecentDamageTimer, or null.
private bool repeatBlurryTrue while the hunger blur should keep repeating.
private PlayerHealthVisualEffectsSettings settingsVisual effect settings: hunger, saturation, vignette and blur curves.
private Coroutine visionSaturationLerpRunning SaturationLerp, or null.
private Coroutine woundedRegenCoroutineDeclared but not used in this class.

Properties​

MemberWhat it is
PlayerController Player { get; }The player controller this health belongs to.
protected override HealthObject.DeathAnalytics AnalyticsRecord { get; }A death analytics record with the player's name, last hit and position.

Events​

MemberWhat it is
event Action<DamageData> DownedRaised with the last hit when the player is downed.
event Action<DamageState> HandleDamageServer: raised before damage is applied. Mark the state handled to block the damage.

Methods​

MemberWhat it is
async void GrantInvincibilityToImpacts(float invincibilityTimeInSeconds)Server: ignores impact damage for a number of seconds.
void OnDowned()Raises Downed.
override void ReceiveDamage(float healthDamage, float crippleDamage, DamageData impact = null)Server: applies damage unless blocked, dead, ignoring damage or invincible. Player hits need PvP on and use the PvP multipliers.
void SetOwner(PlayerController player)Sets the player controller.
void StopPlayerEffects()Client: sets the dead colour saturation and stops the hunger blur.
void UpdateSaturation(float saturationTarget)Client: fades the screen colour saturation to a value.
protected override void Awake()Sets up local player effects once linked to the local player.
protected override void HandleObjectReachedZeroHealth()Downs the player instead of the default death.
protected void Inject(IPostProcessController postProcessController, IPlayerControllerMultiLockModel playerControllerMultiLockModel)Receives the post processing and controller lock services by injection.
protected override void Start()Server: sets up the recent damage stat, immortals toggle and the block while controls are locked.
15 private and internal methods
MemberWhat it is
private void BlockDamageIfControlsAreLocked(DamageState damageState)Server: marks damage as handled (ignored) while the player's controls are locked.
private IEnumerator BlurEffect() (iterator)Client coroutine for hunger: blurs the screen corners with a curve and plays stomach rumbles, repeating while very hungry.
private void CheckForDamage(HealthObject healthObject, float changeAmount, DamageData impact)Server: on any health loss, sets the recent damage stat to 1 and restarts its cooldown timer.
private void DisableResetVisuals()Local player: stops the effect coroutines and resets post processing and the atmosphere.
private void HandleLocalPlayerHit(ImpactHit impact)Local player: records combat with another player in recent players when they hit you.
private IEnumerator HurtVignette() (iterator)Client coroutine that flashes the injured vignette along the settings curve, then clears it.
private void OnDestroy()Unhooks the immortals toggle and resets visuals.
private void OnDisable()Resets the local player's post-processing visuals.
private void OnLinkedToplayer()Local player only: hooks health and hunger to the screen effects and sets the starting saturation.
private IEnumerator RecentDamageTimer() (iterator)Server coroutine that counts down, then sets the recent damage stat back to 0.
private IEnumerator SaturationLerp(float saturationTarget) (iterator)Client coroutine that eases the screen colour saturation to a target.
private void StopBlur()Lets the hunger blur finish its current cycle and stop.
private void ToggleImmortals(bool arePlayersImmortal)Handles the immortals toggle: sets the health minimum to the maximum (no damage) or back to 0.
private void UpdateHunger(Stat stat)Starts or stops the hunger blur when the hunger level changes.
private void UpdateVisuals(Stat stat)On health loss plays the hurt vignette; always updates saturation from the health curve.

Used in​

Members of other types that take, return or hold a PlayerHealth: