UI/CarBoostHud.razor

A UI razor component that renders the car boost HUD. It shows the held item icon if present, a boost pill with input hints, and a fuel bar that fills based on the car's boost fraction. It hides itself when there is no local playable car or when in spectator camera or non-playing game state.

Http CallsNetworking
@using Sandbox;
@using Sandbox.UI;
@using Machines.Player;
@using Machines.Components;
@using Machines.GameModes;

@namespace Machines.UI
@inherits Panel

<root class="car-boost-hud">
    @{
        var car = VisibleCar();
        if ( car is not null )
        {
            var boost = car.Boost;
            var fuelCost = CarBoost.DashFuelCost * boost.MaxFuel;
            var fraction = MathX.Clamp( boost.BoostFraction, 0f, 1f );
            var canBoost = !boost.IsLockedOut && boost.CurrentFuel >= fuelCost;
            var itemIcon = car.Inventory.IsValid() ? car.Inventory.Held?.Icon : null;

            @if ( itemIcon is not null )
            {
                <div class="item-pill">
                    <InputHint Action="UseItem" class="item-glyph" />
                    <div class="item-badge">
                        <ItemIcon Icon=@itemIcon class="item-icon" />
                    </div>
                </div>
            }
            <div class="boost-pill @(canBoost ? "" : "depleted") @(boost.IsDashing ? "dashing" : "")">
                <InputHint Action="Boost" class="boost-glyph" />
                <div class="fuel-bar">
                    <div class="fuel-fill" style="width: @(fraction * 100f)%;"></div>
                </div>
            </div>
        }
    }
</root>

@code
{
    /// <summary>
    /// Local car to show; null hides the pill. Lockout shows greyed, not hidden.
    /// </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.Boost.IsValid() )
            return null;

        return car;
    }

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