HealthComponent.cs

This file defines the HealthComponent, a component that manages a player's health and lifecycle. It handles damage application, passive health regeneration, invulnerability periods, and death sequences like ragdoll creation and respawning.

Networking
using Sandbox;
using System;

public sealed class HealthComponent : Component
{
	[RequireComponent] private Player Player { get; set; }
	[Property] public float MaxHealth { get; set; } = 100f;
	/// <summary>
	/// Amount of health regenerated per second
	/// </summary>
	[Property] public float RegenPerSec { get; set; } = 20f;
	/// <summary>
	/// Seconds without taking damage for health regen to start
	/// </summary>
	[Property] public float RegenTime { get; set; } = 5f;

	/// <summary>Seconds of invulnerability after each spawn. Set to zero to disable.</summary>
	[Property, Group( "Spawn Protection" ), Range( 0f, 10f )]
	public float SpawnProtectionDuration { get; set; } = 3f;

	[Sync] private TimeUntil SpawnProtectionEnd { get; set; }

	public float SpawnProtectionRemaining => IsDead || _deathHandled ? 0f : MathF.Max( 0f, SpawnProtectionEnd );
	public bool IsInvulnerable => SpawnProtectionRemaining > 0f;

	// ---- THE FIX: Use a Change Hook for network synchronization ----
	[Sync, Change( nameof( OnHealthChanged ) )]
	public float CurrentHealth { get; private set; }

	public bool IsDead => CurrentHealth <= 0f;

	public Action OnDeath { get; set; }

	public bool IsRegening { get; set; }

	private float _respawnTime = 10f;
	private bool _deathHandled;
	private bool _corpseDestroyPending;
	private float _corpseDestroyAt;
	private Guid _lastAttackerId;
	private float _lastDamagedTime;
	private Vector3 _lastDamageImpulse;
	private string _lastWeaponIconPath = "";

	protected override void OnStart()
	{
		if ( !IsProxy )
		{
			CurrentHealth = MaxHealth;
			SpawnProtectionEnd = MathF.Max( 0f, SpawnProtectionDuration );
		}
	}

	/// <summary>
	/// Applies damage and remembers the impact impulse so a lethal hit can transfer
	/// its momentum to the spawned ragdoll.
	/// </summary>
	[Rpc.Broadcast]
	public void TakeDamage( float amount, Vector3 impulse, bool headshot, string weaponIconPath = "", Guid botAttackerId = default )
	{
		// Reject protected hits before they affect health, regeneration or kill attribution.
		if ( IsDead || IsInvulnerable || DeathmatchGame.Instance.State != MatchState.IN_PROGRESS ) return;

		// Bots have no connection: only the host may attribute damage to their session.
		var attackerId = Rpc.Caller.Id;
		if ( botAttackerId != Guid.Empty )
		{
			if ( !Rpc.Caller.IsHost || NetworkManager.Instance.GetPlayerSession( botAttackerId ) is not { IsBot: true } )
				return;
			attackerId = botAttackerId;
		}

		_lastAttackerId = attackerId;
		_lastDamagedTime = Time.Now;
		_lastDamageImpulse = impulse;
		_lastWeaponIconPath = weaponIconPath ?? "";

		// The player owner is the single authority for their health. Having both
		// owner and host process this can produce duplicate/out-of-order deaths.
		if ( IsProxy )
			return;

		var newHealth = MathF.Max( 0f, CurrentHealth - amount );
		CurrentHealth = newHealth;

		if ( newHealth <= 0f )
			ConfirmDeath( attackerId, impulse, headshot, _lastWeaponIconPath );
	}

	/// <summary>
	/// Health replication updates UI/state; death effects are driven by ConfirmDeath
	/// so the lethal impulse and attacker identity arrive atomically.
	/// </summary>
	private void OnHealthChanged( float oldVal, float newVal )
	{
		// Intentionally empty. See ConfirmDeath.
	}

	[Rpc.Broadcast]
	private void ConfirmDeath( Guid attackerId, Vector3 impulse, bool headshot, string weaponIconPath = "" )
	{
		_lastAttackerId = attackerId;
		_lastDamageImpulse = impulse;
		_lastWeaponIconPath = weaponIconPath ?? "";
		HandleDeathShared( headshot );
	}

	private void HandleDeathShared(bool headshot)
	{
		if ( _deathHandled )
			return;

		_deathHandled = true;
		OnDeath?.Invoke();

		Tags.Add( "dead" );

		var isSuicide = _lastAttackerId == Player.SessionId;
		KillFeed.Add( _lastAttackerId, Player.SessionId, headshot, isSuicide, _lastWeaponIconPath );

		if ( !IsProxy )
		{
			Player.Inventory?.DropAllItems();
			var victimSession = NetworkManager.Instance.GetPlayerSession( Player.SessionId );
			if ( victimSession.IsValid() )
			{
				victimSession.LastKiller = _lastAttackerId;
				DeathmatchGame.Instance.HandleDeath( isSuicide, victimSession );
			}

			// Create one authoritative ragdoll on the dead player's owning client.
			// Its ModelPhysics transforms are then replicated to everyone instead
			// of each client running a divergent local ragdoll simulation.
			var ragdoll = Player.CreateRagdoll();
			var impulseApplier = ragdoll.AddComponent<RagdollImpulseApplier>();
			impulseApplier.Impulse = _lastDamageImpulse;

			var temporaryEffect = ragdoll.AddComponent<TemporaryEffect>();
			temporaryEffect.DestroyAfterSeconds = _respawnTime;
			temporaryEffect.WaitForChildEffects = false;

			ragdoll.NetworkSpawn();
		}

		var attackerSession = NetworkManager.Instance.GetPlayerSession( _lastAttackerId );
		if ( !isSuicide && attackerSession.IsValid() && !attackerSession.IsProxy )
		{
			DeathmatchGame.Instance.HandleKill( headshot, attackerSession );
			if ( attackerSession == PlayerSession.CurrentSession && Player.SessionId == attackerSession.LastKiller )
			{
				Sandbox.Services.Achievements.Unlock( $"revenge" );
			}
		}

		Player.HideBody( showShadows: false );

		if ( Networking.IsHost )
		{
			_corpseDestroyPending = true;
			_corpseDestroyAt = Time.Now + _respawnTime;
		}
	}

	protected override void OnUpdate()
	{
		HandleHealthRegen();
		HandleDestroyCorpse();
	}

	private void HandleHealthRegen()
	{
		if ( IsProxy || IsDead )
			return;

		IsRegening = false;

		if ( CurrentHealth >= MaxHealth )
			return;

		// Wait until we've gone RegenTime seconds without taking damage.
		if ( Time.Now - _lastDamagedTime < RegenTime )
			return;

		IsRegening = true;

		CurrentHealth = Math.Min(
			MaxHealth,
			CurrentHealth + RegenPerSec * Time.Delta
		);
	}

	private void HandleDestroyCorpse()
	{
		if ( !_corpseDestroyPending || Time.Now < _corpseDestroyAt )
			return;

		_corpseDestroyPending = false;

		if ( !Networking.IsHost || !GameObject.IsValid() )
			return;

		NetworkManager.Instance.RespawnPlayer( NetworkManager.Instance.GetPlayerSession( Player.SessionId ) );
	}
}