UI/Vitals/Vitals.razor

A UI Razor component for the player vitals HUD. It renders health, optional armour, and weapon ammo icons/values, and updates a CSS class when the spawn menu is open. It computes a hash for dirty-checking based on health, armour and weapon ammo.

Native Interop
@using Sandbox;
@using Sandbox.UI;
@inherits PanelComponent
@namespace Sandbox

@if ( !Player.IsValid() || Player.WantsHideHud ) 
    return;

<root>
    <div class="vitals">
        @if ( Player.Armour > 0 )
        {
            <div class="stat armour hud-panel">
                <Image class="icon" Texture=@ArmourIcon />
                <label class="value">@(DisplayArmour)</label>
            </div>
        }
        <div class="stat health hud-panel">
            <Image class="icon" Texture=@HealthIcon />
            <label class="value">@(DisplayHealth)</label>
        </div>
    </div>

    @if ( Weapon.IsValid() && Weapon.UsesAmmo )
    {
        <div class="ammo hud-panel">
            <label class="value">@(Weapon.UsesClips ? Weapon.Clip1.ToString() : Weapon.Ammo1.ToString())</label>
            @if ( Weapon.UsesClips )
            {
                <label class="alternate">@(Weapon.Ammo1.ToString())</label>
            }
            <Image class="icon" Texture=@AmmoIcon />
        </div>
    }
</root>

@code
{
    [Property] public Texture HealthIcon { get; set; }
    [Property] public Texture ArmourIcon { get; set; }
    [Property] public Texture AmmoIcon { get; set; }

    Player Player => Player.FindLocalPlayer();
    BaseSandboxWeapon Weapon => Player?.GetComponent<PlayerInventory>()?.ActiveWeapon as BaseSandboxWeapon;

    int DisplayHealth => (int)Player.Health;
    int DisplayArmour => (int)Player.Armour;

    bool IsSpawnMenuOpen()
    {
        var host = Game.ActiveScene.Get<SpawnMenuHost>();
        return host?.Panel?.HasClass( "open" ) ?? false;
    }

    protected override void OnUpdate()
    {
        Panel.SetClass( "spawnmenu-open", IsSpawnMenuOpen() );
    }

    protected override int BuildHash()
    {
        if ( !Player.IsValid() || Player.WantsHideHud ) return 0;

        return System.HashCode.Combine( Player.Health, Player.Armour, Weapon?.Clip1, Weapon?.Ammo1 );
    }
}