Rampage/Game/RampageHud.razor

The in-game HUD for Rampage, a Razor panel. Draws the crosshair, terror meter, tally, score feed, combo counter, weapon cards, crew list, ticker, tier-up splash and the end-of-run results sheet, reading state from RampageGame, RampageLinks and GiantRiders. A fingerprint hash of the rounded numbers decides when the panel is rebuilt, with a minimum redraw interval.

@using System;
@using System.Collections.Generic;
@using Sandbox;
@using Sandbox.UI;
@inherits PanelComponent
@namespace RampageUi

@*
	The Rampage's whole HUD: loud, comic, arcade. RampageGame puts it on the Rampage object next
	to a ScreenPanel. It draws from the Rampage bus, from what RampageGame has worked out (the
	feed, the combo, the ticker, the stamp, the results), from RampageLinks (the town and the two
	weapons) and from the riders (their commentary and the crew list). Nothing calls it. Every frame BuildHash takes a fingerprint of the numbers it would
	show (no text is made for that); only when the fingerprint changes is the text written again
	and the panel rebuilt.
	Sizes are in panel pixels (the screen is 1080 high). It has its own namespace so that the
	generated class cannot hide anything else called Rampage.
*@

<root>
	@if ( ShowHud )
	{
		<div class="crosshair"><div class="dot"></div></div>

		@if ( !string.IsNullOrEmpty( Prompt ) )
		{
			<div class="prompt"><div class="text">@Prompt</div></div>
		}

		<!-- Top middle: the terror meter. -->
		<div class="meter">
			<div class="titles">
				<div class="tier">@TierText</div>
				<div class="youare">YOU ARE:</div>
				<div class="name">@TierName</div>
			</div>
			<div class="@BarClass"><div class="fill" style="width: @(TerrorPercent)%"></div></div>
			<div class="under">
				<div class="label">DAMAGES</div>
				<div class="@CounterClass">@DestructionText</div>
				<div class="next">@NextText</div>
			</div>
		</div>

		<!-- Top left: the tally. -->
		<div class="tally">
			@foreach ( var row in TallyRows )
			{
				<div class="row"><div class="count">@row.Text</div><div class="what">@row.Extra</div></div>
			}
		</div>

		<!-- Left, under the tally: the score feed, newest at the bottom, and the combo under it. -->
		<div class="feed">
			@foreach ( var pop in PopRows )
			{
				<div class="@pop.Class" style="@pop.Style">
					<div class="what">@pop.Text</div>
					<div class="points">@pop.Extra</div>
				</div>
			}
			@if ( !string.IsNullOrEmpty( ComboText ) )
			{
				<div class="@ComboClass">
					<div class="times" style="@ComboStyle">@ComboText</div>
					<div class="word">@ComboWord</div>
					<div class="timer"><div class="fill" style="width: @(ComboPercent)%"></div></div>
				</div>
			}
		</div>

		<!-- Bottom right: the two weapons, each with its own card, and what they share under them. -->
		<div class="weapons">
			@if ( _weaponKnown )
			{
				<div class="arms">
					<div class="@DongClass">
						<div class="head">THE DONG</div>
						<div class="button">LEFT MOUSE</div>
						<div class="state">@DongText</div>
						<div class="line">@TipText</div>
						<div class="@TipClass"><div class="fill" style="width: @(TipPercent)%"></div></div>
						<div class="line">@SpinText</div>
						<div class="@SpinClass"><div class="fill" style="width: @(SpinPercent)%"></div></div>
					</div>
					<div class="@BallsClass">
						<div class="head">THE BALLS</div>
						<div class="button">RIGHT MOUSE</div>
						<div class="state">@BallsText</div>
						<div class="line">@DrawText</div>
						<div class="@DrawClass"><div class="fill" style="width: @(DrawPercent)%"></div></div>
					</div>
				</div>
			}
			<div class="shared">
				<div class="head">@WeaponHead</div>
				@foreach ( var line in WeaponLines )
				{
					<div class="line">@line</div>
				}
				@if ( ChargePercent >= 0 )
				{
					<div class="@ChargeClass"><div class="fill" style="width: @(ChargePercent)%"></div></div>
				}
			</div>
		</div>

		@if ( !string.IsNullOrEmpty( RiderLine ) )
		{
			<!-- The riders' running commentary: who, and what they said. -->
			<div class="rider">
				<div class="who">@RiderName</div>
				<div class="says">@RiderLine</div>
			</div>
		}

		@if ( !string.IsNullOrEmpty( HeldText ) )
		{
			<!-- Somebody has hold of the lizard: how to get them off, and how long until the weapon does it for you. -->
			<div class="@HeldClass">
				<div class="text">@HeldText</div>
				<div class="how">THE CREW IS FLICKING THEM OFF. OR JUST WALK: THEY FALL OFF.</div>
			</div>
		}

		@if ( !string.IsNullOrEmpty( Banner ) )
		{
			<div class="banner"><div class="text">@Banner</div></div>
		}

		@if ( ShowControls )
		{
			<!-- Bottom middle, always there, small: every way to throw it about, four to a row, with the one in use lit. -->
			<div class="controls">
				<div class="head">HOW TO BE UNREASONABLE</div>
				@for ( int first = 0; first < ControlRows.Length; first += 4 )
				{
					<div class="strip">
						@for ( int i = first; i < first + 4 && i < ControlRows.Length; i++ )
						{
							<div class="@(ControlLit( i ) ? "hint lit" : "hint")"><div class="key">@ControlRows[i].Text</div><div class="does">@ControlRows[i].Extra</div></div>
						}
					</div>
				}
			</div>
		}

		@if ( CrewRows.Count > 0 )
		{
			<!-- Right edge: who is riding (the dot goes hollow when they have fallen off), and what each is good for. -->
			<div class="crew">
				<div class="head">@CrewHead</div>
				@foreach ( var row in CrewRows )
				{
					<div class="@(row.First ? "row off" : "row")">
						<div class="dot"></div>
						<div class="who"><div class="name">@row.Text</div><div class="does">@row.Extra</div></div>
					</div>
				}
			</div>
		}

		<!-- Bottom: the news. -->
		<div class="ticker">
			<div class="tag">COMEUPPANCE NEWS</div>
			<div class="@HeadlineClass">@Headline</div>
			<div class="hint">HOLD TAB  the bill so far</div>
		</div>
	}

	<div class="fade" style="opacity: @FadeOpacity"></div>

	@if ( CardLines.Count > 0 || !string.IsNullOrEmpty( CardTitle ) )
	{
		<div class="card">
			@foreach ( var line in CardLines )
			{
				<div class="line">@line</div>
			}
			@if ( !string.IsNullOrEmpty( CardTitle ) )
			{
				<div class="title">@CardTitle</div>
				<div class="sub">@CardSub</div>
			}
		</div>
	}

	@if ( !string.IsNullOrEmpty( SplashClass ) )
	{
		<div class="@SplashClass">
			<div class="tier">@SplashTier</div>
			<div class="now">YOU ARE NOW</div>
			<div class="name">@SplashName</div>
			<div class="news">@SplashNews</div>
		</div>
	}

	@if ( ShowResults )
	{
		<div class="results">
			<div class="sheet">
				<div class="head">@ResultsHead</div>
				<div class="graderow">
					<div class="grade">@GradeText</div>
					<div class="gradeline">@GradeLine</div>
				</div>
				@foreach ( var row in ResultRows )
				{
					<div class="@(row.First ? "row lit" : "row")"><div class="what">@row.Text</div><div class="value">@row.Extra</div></div>
				}
				<div class="keys">
					<div class="keyrow">
						<div class="key">hold F</div><div class="does">do it all again, worse</div>
						<div class="hold"><div class="fill" style="width: @(RestartPercent)%"></div></div>
					</div>
					<div class="keyrow">
						<div class="key">hold G</div><div class="does">back down to the Sump, smug</div>
						<div class="hold"><div class="fill" style="width: @(LeavePercent)%"></div></div>
					</div>
					<div class="keyrow">
						<div class="key">@(ResultsFinal ? "TAB" : "let go")</div><div class="does">carry on. Nobody is brave enough to stop you.</div>
					</div>
				</div>
			</div>
		</div>
	}
</root>

@code
{
	/// <summary>Points at which a line of the feed is drawn bigger: middling, big, huge.</summary>
	[Property] public Vector3 PopSizes { get; set; } = new( 150f, 600f, 2000f );

	/// <summary>One row of anything: text, a second text, a style, a class, and whether it is the lit one.</summary>
	public struct Row
	{
		public string Text, Extra, Style, Class;
		public bool First;
	}

	// Every way to throw it about, once. TAB is on the ticker, where it is always in sight.
	static readonly Row[] ControlRows =
	{
		new() { Text = "HOLD LEFT + MOUSE", Extra = "whip by hand. Flick to crack" },
		new() { Text = "TAP LEFT x3", Extra = "whip, whip, overhead slam" },
		new() { Text = "LEFT + SHIFT / CIRCLE", Extra = "helicopter. Let go to fling" },
		new() { Text = "TAP RIGHT", Extra = "the balls, once round" },
		new() { Text = "HOLD RIGHT", Extra = "slingshot. Aim with camera" },
		new() { Text = "HOLD BOTH", Extra = "MEGA-SWING. Do not miss" },
		new() { Text = "RIGHT AFTER 2 WHIPS", Extra = "THE CLAP" },
		new() { Text = "IN THE AIR", Extra = "L: ground pound  R: sit-down" },
	};

	/// <summary>Which of <see cref="WeaponHint"/> each row of the controls is, in order.</summary>
	static readonly WeaponHint[] ControlHints =
	{
		WeaponHint.Aim, WeaponHint.Combo, WeaponHint.Spin, WeaponHint.Flail, WeaponHint.Sling, WeaponHint.Charge, WeaponHint.Clap, WeaponHint.Air,
	};

	bool ControlLit( int row ) => row >= 0 && row < ControlHints.Length && (_lit & (int)ControlHints[row]) != 0;

	static readonly string[] PopClasses = BuildPopClasses();
	static readonly string[] OpacityStyles = { "opacity: 0;", "opacity: 0.20;", "opacity: 0.40;", "opacity: 0.60;", "opacity: 0.80;", "opacity: 1;" };

	/// <summary>Every class a line of the feed can have, made once: size (4) by bonus (2) by fresh (2).</summary>
	static string[] BuildPopClasses()
	{
		var sizes = new[] { "small", "mid", "big", "huge" };
		var all = new string[16];
		for ( int size = 0; size < 4; size++ )
			for ( int bonus = 0; bonus < 2; bonus++ )
				for ( int fresh = 0; fresh < 2; fresh++ )
					all[size * 4 + bonus * 2 + fresh] = "pop " + sizes[size] + (bonus == 1 ? " bonus" : "") + (fresh == 1 ? " fresh" : "");
		return all;
	}

	// ---- What is drawn. Text is written in Rebuild, only when the fingerprint has changed. -------

	bool ShowHud;
	string Prompt;
	string TierText = "", TierName = "", NextText = "", DestructionText = "0";
	string BarClass = "bar", CounterClass = "number";
	int TerrorPercent;
	List<Row> TallyRows = new();
	List<Row> PopRows = new();
	string ComboText = "", ComboWord = "", ComboClass = "combo", ComboStyle = "";
	int ComboPercent;
	string WeaponHead = "";
	List<string> WeaponLines = new();
	int ChargePercent = -1;
	string ChargeClass = "charge";
	string DongClass = "arm dong", BallsClass = "arm balls", DongText = "", BallsText = "", TipText = "", SpinText = "", DrawText = "";
	string TipClass = "gauge tip", SpinClass = "gauge spin", DrawClass = "gauge draw";
	int TipPercent, SpinPercent, DrawPercent;
	string RiderName = "", RiderLine = "";
	List<Row> CrewRows = new();
	string CrewHead = "";
	string Banner = "";
	string HeldText = "", HeldClass = "held";
	int ReflexPercent;
	bool ShowControls;
	string Headline = "", HeadlineClass = "headline";
	string FadeOpacity = "0";
	string CardTitle = "", CardSub = "";
	List<string> CardLines = new();
	string SplashClass = "", SplashTier = "", SplashName = "", SplashNews = "";
	bool ShowResults, ResultsFinal;
	string ResultsHead = "", GradeText = "", GradeLine = "";
	List<Row> ResultRows = new();
	int RestartPercent, LeavePercent;

	// The numbers behind the text, kept by Fingerprint for Rebuild.
	int _tier, _humans, _things, _standing, _combo, _punch, _scaleTenths, _reachFeet, _weaponCombo, _grabbers, _fadeHundredths;
	long _shown;
	bool _pinned, _held, _weaponKnown, _tipCracking;
	string _attack = "", _hooked = "";
	int _lit, _tipMph, _rpm, _crew, _crewOff, _riding;

	int _hash;
	bool _drawn;

	/// <summary>0 to 1 as text for a style, in hundredths ("0.37"), whatever the machine's locale.</summary>
	static string Opacity( int hundredths ) => hundredths >= 100 ? "1" : hundredths <= 0 ? "0" : $"0.{hundredths:00}";

	/// <summary>
	/// A number that changes when anything the panel shows does. It reads and rounds, and makes no
	/// text: that is left to <see cref="Rebuild"/>.
	/// </summary>
	int Fingerprint()
	{
		var hash = new HashCode();
		var game = RampageGame.Current;
		if ( !game.IsValid() ) return 0;
		float now = RealTime.Now;

		ShowHud = game.Phase == RampagePhase.Rampage && game.Fade < 0.9f;
		hash.Add( ShowHud );

		// ---- Prompt and banner ----
		Prompt = game.ShownPrompt;
		Banner = now < Rampage.BannerUntil && !game.SplashShowing ? Rampage.Banner ?? "" : "";
		hash.Add( Prompt ); hash.Add( Banner );

		// ---- The meter ----
		_tier = Rampage.Tier;
		TerrorPercent = (int)MathF.Round( Math.Clamp( Rampage.Terror, 0f, 1f ) * 100f );
		_shown = (long)MathF.Max( game.ShownDestruction, 0f );
		bool rolling = game.ShownDestruction < Rampage.Destruction - 0.5f;
		bool justHit = now - game.LastHitAt < 0.12f;
		CounterClass = rolling ? "number rolling" : "number";
		BarClass = TerrorPercent >= 85 ? (((int)(now * 6f) & 1) == 0 ? "bar nearly beat" : "bar nearly") : justHit ? "bar hit" : "bar";
		hash.Add( _tier ); hash.Add( TerrorPercent ); hash.Add( _shown ); hash.Add( rolling ); hash.Add( BarClass );

		// ---- The tally ----
		_humans = Rampage.HumansSent;
		_things = game.ThingsSmashed;
		_standing = RampageLinks.StandingPercent;
		hash.Add( _humans ); hash.Add( _things ); hash.Add( _standing );

		// ---- The feed ----
		var pops = game.Pops;
		hash.Add( pops.Count );
		for ( int i = 0; i < pops.Count; i++ )
		{
			var pop = pops[i];
			hash.Add( pop.Label ); hash.Add( pop.Count ); hash.Add( (int)pop.Points ); hash.Add( pop.Bonus );
			hash.Add( now - pop.Born < 0.1f ); hash.Add( FadeStep( pop, now ) );
		}

		// ---- The combo ----
		_combo = game.Combo;
		ComboPercent = (int)(game.ComboLeft * 20f) * 5;
		_punch = now - game.LastHitAt < 0.08f ? 14 : 0;
		hash.Add( _combo ); hash.Add( ComboPercent ); hash.Add( _punch );

		// ---- The weapon ----
		_scaleTenths = (int)MathF.Round( MathF.Max( Rampage.WeaponScale, 0f ) * 10f );
		_weaponKnown = RampageLinks.WeaponKnown;
		ChargePercent = -1;
		ChargeClass = "charge";
		_reachFeet = 0; _attack = ""; _weaponCombo = 0;
		if ( _weaponKnown )
		{
			_reachFeet = (int)MathF.Round( RampageLinks.Reach / 12f );
			_attack = RampageLinks.AttackName ?? "";
			_weaponCombo = RampageLinks.WeaponCombo;
			ChargePercent = (int)MathF.Round( RampageLinks.Charge01 * 50f ) * 2;
			ChargeClass = ChargePercent >= 100 ? (((int)(now * 10f) & 1) == 0 ? "charge full beat" : "charge full") : RampageLinks.Swinging ? "charge swing" : "charge";
		}
		hash.Add( _scaleTenths ); hash.Add( _weaponKnown ); hash.Add( _reachFeet ); hash.Add( _attack ); hash.Add( _weaponCombo );
		hash.Add( ChargePercent ); hash.Add( ChargeClass );

		// The two weapons, each for itself. Everything is rounded, so the panel is not rebuilt for a hair's difference.
		_lit = 0; _tipMph = 0; _rpm = 0; _hooked = ""; _tipCracking = false;
		TipPercent = SpinPercent = DrawPercent = 0;
		DongText = BallsText = "";
		DongClass = "arm dong"; BallsClass = "arm balls"; TipClass = "gauge tip"; SpinClass = "gauge spin"; DrawClass = "gauge draw";
		if ( _weaponKnown )
		{
			_lit = (int)RampageLinks.Lit;
			DongText = RampageLinks.DongState;
			BallsText = RampageLinks.BallsState;
			_hooked = RampageLinks.Hooked;
			_tipMph = (int)(RampageLinks.TipSpeed * 0.0568f / 5f) * 5;
			TipPercent = (int)(Math.Clamp( RampageLinks.TipCrack01, 0f, 1f ) * 20f) * 5;
			_rpm = (int)(RampageLinks.SpinRpm / 5f) * 5;
			SpinPercent = (int)(RampageLinks.Spin01 * 25f) * 4;
			DrawPercent = (int)(RampageLinks.Draw01 * 25f) * 4;
			_tipCracking = RampageLinks.TipCracking;
			if ( _tipCracking ) TipClass = "gauge tip crack";
			if ( SpinPercent >= 80 ) SpinClass = ((int)(now * 10f) & 1) == 0 ? "gauge spin full beat" : "gauge spin full";
			if ( DrawPercent >= 100 ) DrawClass = ((int)(now * 10f) & 1) == 0 ? "gauge draw full beat" : "gauge draw full";
			const int dongBits = (int)(WeaponHint.Aim | WeaponHint.Combo | WeaponHint.Spin | WeaponHint.Charge | WeaponHint.Clap);
			const int ballBits = (int)(WeaponHint.Flail | WeaponHint.Sling | WeaponHint.Charge | WeaponHint.Clap);
			if ( (_lit & dongBits) != 0 ) DongClass = "arm dong busy";
			if ( (_lit & ballBits) != 0 ) BallsClass = "arm balls busy";
		}
		hash.Add( _lit ); hash.Add( DongText ); hash.Add( BallsText ); hash.Add( _hooked ); hash.Add( _tipMph ); hash.Add( TipPercent );
		hash.Add( _rpm ); hash.Add( SpinPercent ); hash.Add( DrawPercent ); hash.Add( TipClass ); hash.Add( SpinClass ); hash.Add( DrawClass );

		// ---- The riders ----
		RiderLine = now < Rampage.RiderLineUntil ? Rampage.RiderLine ?? "" : "";
		RiderName = Rampage.RiderName ?? "";
		hash.Add( RiderLine ); hash.Add( RiderName );
		_crew = 0; _crewOff = 0; _riding = 0;
		var riders = GiantRiders.Current;
		if ( riders.IsValid() && riders.Crew is { } crew )
		{
			_crew = crew.Count;
			_riding = riders.Riding;
			for ( int i = 0; i < crew.Count && i < 30; i++ )
			{
				if ( crew[i] is null ) continue;
				if ( crew[i].Fallen ) _crewOff |= 1 << i;
				hash.Add( crew[i].Name ); hash.Add( crew[i].Passive );
			}
		}
		hash.Add( _crew ); hash.Add( _crewOff ); hash.Add( _riding );

		// ---- Grabbed ----
		_held = RampageLinks.Held;
		_pinned = RampageLinks.Pinned;
		_grabbers = RampageLinks.Grabbers;
		HeldClass = _pinned && ((int)(now * 6f) & 1) == 0 ? "held pinned beat" : _pinned ? "held pinned" : "held";
		ReflexPercent = (int)MathF.Round( RampageLinks.ReflexCharge * 50f ) * 2;
		hash.Add( _held ); hash.Add( _grabbers ); hash.Add( HeldClass ); hash.Add( ReflexPercent );

		// ---- Controls, ticker ----
		ShowControls = !game.ShowResults;
		Headline = game.Headline ?? "";
		HeadlineClass = now - game.HeadlineAt < 0.5f ? "headline fresh" : "headline";
		hash.Add( ShowControls ); hash.Add( Headline ); hash.Add( HeadlineClass );

		// ---- Fade and the intro's cards ----
		_fadeHundredths = (int)MathF.Round( Math.Clamp( game.Fade, 0f, 1f ) * 100f );
		CardTitle = game.CardTitle ?? "";
		CardSub = game.CardSub ?? "";
		hash.Add( _fadeHundredths ); hash.Add( CardTitle ); hash.Add( CardSub ); hash.Add( game.CardLines.Count );
		for ( int i = 0; i < game.CardLines.Count; i++ ) hash.Add( game.CardLines[i] );

		// ---- The tier-up stamp: slams in, sits, goes ----
		SplashClass = "";
		if ( game.SplashShowing && game.Phase == RampagePhase.Rampage )
		{
			float age = now - game.SplashAt;
			SplashClass = age < 0.06f ? "splash in" : age > game.SplashSeconds - 0.4f ? "splash out" : "splash";
		}
		hash.Add( SplashClass ); hash.Add( game.SplashTier );

		// ---- Results ----
		ShowResults = game.ShowResults;
		ResultsFinal = game.ResultsFinal;
		RestartPercent = LeavePercent = 0;
		if ( ShowResults )
		{
			RestartPercent = (int)(Math.Clamp( game.RestartHold, 0f, 1f ) * 20f) * 5;
			LeavePercent = (int)(Math.Clamp( game.LeaveHold, 0f, 1f ) * 20f) * 5;
			hash.Add( (int)Rampage.Destruction ); hash.Add( game.BestCombo ); hash.Add( game.SilliestThing ); hash.Add( game.BiggestThing );
			hash.Add( (int)game.BiggestPoints ); hash.Add( game.FavouriteCount ); hash.Add( game.FavouriteThing );
			hash.Add( RestartPercent ); hash.Add( LeavePercent ); hash.Add( ResultsFinal );
			hash.Add( (int)game.BestDestruction ); hash.Add( game.BestTier ); hash.Add( game.BestComboEver );
			hash.Add( RampageLinks.Cracks ); hash.Add( RampageLinks.Megas ); hash.Add( RampageLinks.Misses ); hash.Add( RampageLinks.Flung );
			hash.Add( (int)(RampageLinks.BestTipSpeed * 0.0568f) ); hash.Add( riders.IsValid() ? riders.Falls : 0 );
		}
		hash.Add( ShowResults );

		return hash.ToHashCode();
	}

	/// <summary>How faded a line of the feed is, 0 (gone) to 5 (solid): it goes in its last third.</summary>
	static int FadeStep( RampageGame.ScorePop pop, float now )
	{
		float life = MathF.Max( pop.Until - pop.Born, 0.1f );
		float left = (pop.Until - now) / life;
		return left < 0.3f ? (int)(Math.Clamp( left / 0.3f, 0f, 1f ) * 5f) : 5;
	}

	/// <summary>Writes every piece of text the panel shows, from the numbers Fingerprint has just read.</summary>
	void Rebuild()
	{
		var game = RampageGame.Current;
		if ( !game.IsValid() )
		{
			ShowHud = ShowResults = false;
			CardLines.Clear();
			CardTitle = SplashClass = "";
			FadeOpacity = "0";
			return;
		}
		float now = RealTime.Now;

		// ---- The meter ----
		TierText = $"TIER {_tier + 1}";
		TierName = Rampage.TierName;
		int nextIndex = Math.Min( _tier + 1, Rampage.TierNames.Length - 1 );
		NextText = _tier + 1 < Rampage.TierNames.Length ? $"NEXT: {Rampage.TierNames[nextIndex]}" : "NEXT: WORSE";
		DestructionText = RampageNews.Number( _shown );

		// ---- The tally ----
		TallyRows.Clear();
		TallyRows.Add( new Row { Text = _humans.ToString(), Extra = "PILLOCKS AIRBORNE" } );
		TallyRows.Add( new Row { Text = _things.ToString(), Extra = "THINGS KNACKERED" } );
		if ( _standing >= 0 ) TallyRows.Add( new Row { Text = $"{_standing}%", Extra = "OF TOWN STILL SMUG" } );

		// ---- The feed ----
		PopRows.Clear();
		foreach ( var pop in game.Pops )
		{
			int size = pop.Points >= PopSizes.z ? 3 : pop.Points >= PopSizes.y ? 2 : pop.Points >= PopSizes.x ? 1 : 0;
			bool fresh = now - pop.Born < 0.1f;
			PopRows.Add( new Row
			{
				Text = pop.Count > 1 ? $"{pop.Label} x{pop.Count}" : pop.Label,
				Extra = $"+{RampageNews.Number( pop.Points )}",
				Class = PopClasses[size * 4 + (pop.Bonus ? 2 : 0) + (fresh ? 1 : 0)],
				Style = OpacityStyles[Math.Clamp( FadeStep( pop, now ), 0, 5 )],
			} );
		}

		// ---- The combo ----
		ComboText = _combo >= 2 ? $"x{_combo}" : "";
		ComboWord = RampageNews.ComboWord( _combo );
		ComboStyle = $"font-size: {56 + Math.Min( _combo, 40 ) * 2 + _punch}px;";
		ComboClass = _combo >= 20 ? "combo mad" : _combo >= 8 ? "combo hot" : "combo";

		// ---- The weapon ----
		WeaponLines.Clear();
		WeaponHead = _attack.Length > 0 ? _attack : $"THE TACKLE  x{_scaleTenths / 10}.{_scaleTenths % 10}";
		if ( _weaponKnown )
		{
			WeaponLines.Add( _weaponCombo > 0 ? $"REACH {_reachFeet} FT      COMBO {_weaponCombo} OF 3" : $"REACH {_reachFeet} FT" );
			if ( _hooked.Length > 0 ) WeaponLines.Add( $"ON THE END: {_hooked}" );
			TipText = _tipCracking ? $"TIP {_tipMph} MPH   CRACK!" : $"TIP {_tipMph} MPH";
			SpinText = _rpm > 0 ? $"SPIN {_rpm} RPM" : "SPIN";
			DrawText = DrawPercent > 0 ? $"DRAW {DrawPercent}%" : "DRAW";
		}

		// ---- The crew ----
		CrewRows.Clear();
		var riders = GiantRiders.Current;
		if ( riders.IsValid() && _crew > 0 )
		{
			var crew = riders.Crew;
			CrewHead = $"ON HIS BACK: {_riding} OF {crew.Count}";
			for ( int i = 0; i < crew.Count; i++ )
				if ( crew[i] is not null ) CrewRows.Add( new Row { Text = crew[i].Name, Extra = crew[i].Fallen ? "FELL OFF. RUNNING BEHIND." : crew[i].Passive, First = crew[i].Fallen } );
		}

		// ---- Grabbed ----
		HeldText = !_held ? "" : _pinned ? "PINNED. THE BRASS NECK OF IT."
			: _grabbers > 1 ? $"{_grabbers} CLIMBERS ON YOUR ANKLES" : "SOMEBODY IS CLIMBING YOUR LEG";

		// ---- Fade and the intro's cards ----
		FadeOpacity = Opacity( _fadeHundredths );
		CardLines.Clear();
		CardLines.AddRange( game.CardLines );

		// ---- The tier-up stamp ----
		if ( SplashClass.Length > 0 )
		{
			SplashTier = $"TIER {game.SplashTier + 1}";
			SplashName = game.SplashName;
			SplashNews = game.SplashHeadline;
		}

		// ---- Results ----
		ResultRows.Clear();
		if ( !ShowResults ) return;

		ResultsHead = ResultsFinal ? "THE COMEUPPANCE: DELIVERED" : "THE BILL SO FAR";
		var grade = game.Grade;
		GradeText = grade.Grade;
		GradeLine = grade.Line;
		ResultRows.Add( new Row { Text = "DAMAGES", Extra = RampageNews.Number( Rampage.Destruction ), First = true } );
		ResultRows.Add( new Row { Text = "YOU WERE", Extra = $"{Rampage.TierName}  (TIER {_tier + 1})" } );
		ResultRows.Add( new Row { Text = "PILLOCKS AIRBORNE", Extra = _humans.ToString() } );
		ResultRows.Add( new Row { Text = "THINGS KNACKERED", Extra = _things.ToString() } );
		if ( _standing >= 0 ) ResultRows.Add( new Row { Text = "TOWN STILL STANDING", Extra = _standing <= 0 ? "NOT A SAUSAGE" : $"{_standing}%" } );
		string word = RampageNews.ComboWord( game.BestCombo );
		ResultRows.Add( new Row { Text = "BEST COMBO", Extra = game.BestCombo >= 2 ? $"x{game.BestCombo}  {word}" : "NONE. PACE YOURSELF LESS." } );
		if ( RampageLinks.WeaponKnown )
		{
			ResultRows.Add( new Row { Text = "FASTEST TIP", Extra = $"{(int)(RampageLinks.BestTipSpeed * 0.0568f)} MPH   ({RampageLinks.Cracks} CRACKS)" } );
			int megas = RampageLinks.Megas, missed = RampageLinks.Misses;
			if ( megas + missed > 0 ) ResultRows.Add( new Row { Text = "MEGA-SWINGS", Extra = missed > 0 ? $"{megas + missed}   (MISSED {missed}. WE SAW.)" : megas.ToString() } );
			if ( RampageLinks.Flung > 0 ) ResultRows.Add( new Row { Text = "CARS RETURNED TO SENDER", Extra = RampageLinks.Flung.ToString() } );
		}
		var aboard = GiantRiders.Current;
		if ( aboard.IsValid() && aboard.Crew.Count > 0 )
			ResultRows.Add( new Row { Text = "CREW", Extra = aboard.Falls > 0 ? $"{aboard.Crew.Count} ABOARD   (FELL OFF {aboard.Falls} TIMES)" : $"{aboard.Crew.Count} ABOARD. NOBODY FELL OFF." } );
		if ( !string.IsNullOrEmpty( game.SilliestThing ) ) ResultRows.Add( new Row { Text = "SILLIEST THING FLATTENED", Extra = game.SilliestThing } );
		if ( !string.IsNullOrEmpty( game.BiggestThing ) ) ResultRows.Add( new Row { Text = "BIGGEST SINGLE WALLOP", Extra = $"{game.BiggestThing}  +{RampageNews.Number( game.BiggestPoints )}" } );
		if ( game.FavouriteCount >= 3 ) ResultRows.Add( new Row { Text = "PERSONAL VENDETTA", Extra = $"{game.FavouriteThing}  x{game.FavouriteCount}" } );
		float best = MathF.Max( game.BestDestruction, 0f );
		ResultRows.Add( new Row
		{
			Text = "BEST EVER",
			Extra = Rampage.Destruction >= best && Rampage.Destruction > 0f ? "THIS. RIGHT NOW. FRAME IT."
				: best > 0f ? $"{RampageNews.Number( best )}   TIER {Math.Max( game.BestTier, 0 ) + 1}   x{game.BestComboEver}" : "NOTHING YET",
		} );
	}

	/// <summary>
	/// The panel is rebuilt at most this often. A counter rolling up changes every frame, and
	/// nobody can read it that fast: at a high frame rate most of those rebuilds (and the text
	/// laid out for each) would be for nothing.
	/// </summary>
	[Property] public float MinRedrawSeconds { get; set; } = 0.022f;

	float _lastDraw;

	protected override int BuildHash()
	{
		int hash = Fingerprint();
		if ( hash == _hash && _drawn ) return _hash;

		float now = RealTime.Now;
		if ( _drawn && now - _lastDraw < MinRedrawSeconds && now >= _lastDraw ) return _hash;

		_hash = hash;
		_drawn = true;
		_lastDraw = now;
		Rebuild();
		return _hash;
	}
}