UI/CarHornHud.razor

Razor UI panel that renders a small car horn HUD hint. It checks if the local player is in a valid playing game state, not spectating, in control of a local car with a CarHorn component, and if so shows a pill with an icon and an InputHint for the Horn action, toggling a beeping CSS class when the horn is active.

Networking
@using Sandbox;
@using Sandbox.UI;
@using Machines.Player;
@using Machines.Components;
@using Machines.GameModes;

@namespace Machines.UI
@inherits Panel

<root class="car-horn-hud">
    @{
        var car = VisibleCar();
        if ( car is not null )
        {
            var horn = car.GetComponent<CarHorn>();
            var beeping = horn.IsValid() && horn.IsBeeping;

            <div class="horn-pill @(beeping ? "beeping" : "")">
                <Label class="horn-icon">📣</Label>
                <InputHint Action="Horn" class="horn-glyph" />
            </div>
        }
    }
</root>

@code
{
    /// <summary>
    /// Local car to show the horn hint for; null hides the pill.
    /// </summary>
    private Car VisibleCar()
    {
        if ( !BaseGameMode.Current.IsValid() || BaseGameMode.Current.State != GameModeState.Playing )
            return null;

        if ( Game.ActiveScene?.Get<SpectatorCamera>()?.IsActive ?? false )
            return null;

        var car = Car.Local;
        if ( !car.IsValid() || car.Autopilot )
            return null;

        if ( !car.GetComponent<CarHorn>().IsValid() )
            return null;

        return car;
    }

    protected override int BuildHash() => HashCode.Combine( Time.Now );
}