Stadium/StadiumHud.razor

The HUD for the Stadium game, built as a Razor PanelComponent. Draws the crosshair, phase messages, struggle and charge bars, a grab warning, the cheek/crush fade overlay and the game over and win cards, with BuildHash rebuilding only when visible values change.

@using System;
@using Sandbox;
@using Sandbox.UI;
@inherits PanelComponent

<root>
	<div class="crosshair"></div>

	@if ( Stadium is not null )
	{
		@if ( Stadium.MessageVisible && Stadium.Phase != StadiumPhase.GameOver && !Stadium.Cutscene.HasValue )
		{
			<div class="message"><div class="text">@Stadium.Message</div></div>
		}

		@if ( Stadium.Held && Stadium.Phase == StadiumPhase.Escape && !Stadium.Cutscene.HasValue )
		{
			<div class="struggle">
				<div class="label">@(Stadium.Pinned ? "PINNED" : "HELD")  (@Stadium.Grabbers of @Stadium.PinGrips pairs of hands)</div>
				<div class="bar"><div class="fill" style="width: @(StrugglePercent)%"></div></div>
				<div class="prompt">SPACE  buck them off</div>
			</div>
		}

		@if ( !Stadium.Held && !Stadium.Cutscene.HasValue && Stadium.Phase == StadiumPhase.Escape && Stadium.Charge.IsValid() && (ChargePercent > 0 || Stadium.Charge.WipedOut) )
		{
			<div class="struggle">
				<div class="label">@(Stadium.Charge.WipedOut ? "WIPEOUT" : Stadium.Charge.Untouchable ? "CHARGE  untouchable" : Stadium.Charge.Ploughing ? "CHARGE" : "SHIFT  sprint to charge")</div>
				<div class="bar"><div class="fill" style="width: @(ChargePercent)%"></div></div>
			</div>
		}

		@if ( Warning && !Stadium.Cutscene.HasValue )
		{
			<div class="warning">ARMS UP  somebody is about to lunge. Be elsewhere</div>
		}

		@if ( Stadium.Phase == StadiumPhase.GameOver || Stadium.CrushFade > 0f )
		{
			<!-- The dark closes in as two cheeks. -->
			@if ( Stadium.CheekFade > 0f )
			{
				<div class="cheek" style="@Cheek( -1f )"></div>
				<div class="cheek" style="@Cheek( 1f )"></div>
			}
			@if ( Stadium.CheekFade >= 1f )
			{
				<div class="fade" style="opacity: 1"></div>
			}
		}
		else
		{
			<div class="fade" style="opacity: @Stadium.Fade"></div>
		}
		<div class="flash" style="opacity: @Stadium.Flash"></div>

		@if ( Stadium.Phase == StadiumPhase.GameOver && Stadium.TimeInPhase > Stadium.CardDelay )
		{
			<div class="card">
				<div class="title">GAME OVER</div>
				<div class="line">The Fat Guy sat on you. He has not noticed. He is still sitting.</div>
				<div class="prompt">SPACE  peel yourself off</div>
			</div>
		}

		@if ( Stadium.Phase == StadiumPhase.Won )
		{
			<div class="card">
				<div class="title win">YOU GOT OUT</div>
				<div class="line">The floor past the door is not there. Of course it is not.</div>
			</div>
		}
	}
</root>

@code
{
	StadiumGame Stadium => _stadium ??= StadiumGame.Get( Scene );
	StadiumGame _stadium;

	int StrugglePercent => Stadium is null || Stadium.StruggleNeeded <= 0f ? 0 : (int)Math.Clamp( Stadium.Struggle / Stadium.StruggleNeeded * 100f, 0f, 100f );

	/// <summary>One black cheek, growing from the middle of the screen until the pair cover it. Sizes are in panel pixels (1080 high).</summary>
	string Cheek( float side )
	{
		float fade = Stadium?.CheekFade ?? 0f;
		float diameter = 60f + fade * fade * 4600f;
		float width = 1080f * Screen.Width / MathF.Max( Screen.Height, 1f );
		int d = (int)diameter;
		// They start apart and come together: a clap, not a bloom.
		float apart = 0.43f + 0.55f * (1f - fade) * (1f - fade);
		int left = (int)(width * 0.5f + side * diameter * apart - diameter * 0.5f);
		int top = (int)(600f - diameter * 0.5f);
		return $"left: {left}px; top: {top}px; width: {d}px; height: {d}px;";
	}

	int ChargePercent => Stadium is null || !Stadium.Charge.IsValid() ? 0 : (int)(Stadium.Charge.Charge * 100f);

	/// <summary>Somebody has their arms up and is about to grab.</summary>
	bool Warning => Stadium is not null && Stadium.Phase == StadiumPhase.Escape && Stadium.GrabWarning > 0f;

	// The flash and fade change every frame while they run, so rebuild on their rounded values.
	protected override int BuildHash() => HashCode.Combine(
		Stadium?.Phase, Stadium?.MessageVisible, Stadium?.Message, Stadium?.Grabbers, HashCode.Combine( StrugglePercent, ChargePercent, Stadium?.Charge?.Ploughing, Stadium?.Charge?.Untouchable, Stadium?.Charge?.WipedOut, Warning, Stadium?.Pinned ),
		(int)((Stadium?.Flash ?? 0f) * 100f), (int)((Stadium?.Fade ?? 0f) * 400f) + (int)((Stadium?.CrushFade ?? 0f) * 400f) * 1000 + (Stadium?.Cutscene.HasValue == true ? 100000 : 0), Stadium?.TimeInPhase > Stadium?.CardDelay );
}