Stadium/LizardCharge.cs

LizardCharge is a gameplay component on the lizard player. It builds a 0 to 1 charge while sprinting, drains it otherwise, and above a threshold sets ReptileController.PassCrowd so the lizard runs through the crowd. On each fixed update it scans CrowdPerson components nearby and resolves contacts as a shoulder (a cheap, glancing hit with side push and deflection) or a head-on hit (big charge and speed loss, sometimes a wipeout that zeroes the charge and slows the lizard). Jumping near a person tramples them, gives a forward lunge and tops the charge up. Body overlap pushes the lizard back and shoves the person. Counts Bowled, Clean and Wipeouts, and plays HitSound on contacts.

using System;
using System.Linq;

/// <summary>
/// The charge. Sprinting builds it up; while it is up the lizard does not stop for people, it
/// goes through them: everyone it meets is bowled out of the way as a real physics body, and
/// each one costs a little of the charge. Jumping off somebody on the way (the trample) kicks
/// them down and tops the charge back up, so a good run is stride, hit, vault, hit. Let the
/// charge run out in a thick crowd and the lizard is stopped by bodies again and can be grabbed.
/// Put on the lizard by <see cref="StadiumGame"/>.
/// </summary>
[Title( "Lizard Charge" ), Category( "Stadium" ), Icon( "directions_run" )]
public sealed class LizardCharge : Component
{
	/// <summary>Seconds of flat-out sprinting to fill the charge from nothing.</summary>
	[Property] public float BuildSeconds { get; set; } = 1.4f;

	/// <summary>Seconds for a full charge to drain when not sprinting.</summary>
	[Property] public float DrainSeconds { get; set; } = 0.9f;

	/// <summary>Above this much charge the lizard ploughs through people instead of being stopped by them.</summary>
	[Property, Range( 0f, 1f )] public float PloughThreshold { get; set; } = 0.3f;

	/// <summary>Extra speed at full charge, as a fraction (0.5 is half as fast again).</summary>
	[Property] public float SpeedBonus { get; set; } = 0.5f;

	/// <summary>Charge lost for a clean, glancing hit.</summary>
	[Property] public float CostPerHit { get; set; } = 0.04f;

	/// <summary>Charge lost for hitting somebody dead centre.</summary>
	[Property, Group( "Angles" )] public float HeadOnCost { get; set; } = 0.45f;

	/// <summary>
	/// How far off centre a hit has to be to count as clean, as a fraction of the hit radius.
	/// Inside this the lizard runs into them; outside it shoulders them aside.
	/// </summary>
	[Property, Group( "Angles" ), Range( 0f, 1f )] public float CleanOffset { get; set; } = 0.42f;

	/// <summary>Fraction of the lizard's speed lost in a dead-centre hit.</summary>
	[Property, Group( "Angles" ), Range( 0f, 1f )] public float HeadOnSpeedLoss { get; set; } = 0.7f;

	/// <summary>How much a glancing hit knocks the lizard's line sideways, as a fraction of its speed.</summary>
	[Property, Group( "Angles" )] public float Deflection { get; set; } = 0.12f;

	/// <summary>Closer to dead centre than this, at speed, and the lizard wipes out.</summary>
	[Property, Group( "Angles" ), Range( 0f, 1f )] public float WipeoutOffset { get; set; } = 0.14f;

	/// <summary>Seconds the lizard is sprawling after a wipeout, and how slow it is meanwhile.</summary>
	[Property, Group( "Angles" )] public float WipeoutSeconds { get; set; } = 0.9f;
	[Property, Group( "Angles" )] public float WipeoutSpeed { get; set; } = 0.3f;

	/// <summary>Only above this much charge is the lizard too fast to grab.</summary>
	[Property, Group( "Angles" ), Range( 0f, 1f )] public float SlipThreshold { get; set; } = 0.6f;

	/// <summary>Bodies pressed against the lizard push back this hard (inches per second, per second, at full overlap).</summary>
	[Property, Group( "Angles" )] public float BodyPush { get; set; } = 1500f;
	[Property, Group( "Angles" )] public float BodyRadius { get; set; } = 34f;

	/// <summary>Charge gained by jumping off somebody.</summary>
	[Property] public float TrampleGain { get; set; } = 0.3f;

	/// <summary>How close a person has to be to be hit, and to be jumped off.</summary>
	[Property] public float HitRadius { get; set; } = 44f;
	[Property] public float TrampleRadius { get; set; } = 80f;

	/// <summary>How much of the lizard's speed a bowled person is thrown with, forward and sideways, and how hard upward.</summary>
	[Property] public float ThrowForward { get; set; } = 0.85f;
	[Property] public float ThrowSideways { get; set; } = 0.55f;
	[Property] public float ThrowUp { get; set; } = 260f;

	/// <summary>Forward shove the lizard gets from a trample jump.</summary>
	[Property] public float TrampleBoost { get; set; } = 170f;

	[Property] public string HitSound { get; set; } = "sounds/stadium/fat_step.sound";

	/// <summary>0 to 1.</summary>
	public float Charge { get; private set; }
	public bool Ploughing => Charge >= PloughThreshold && !WipedOut;
	public bool WipedOut => Time.Now < _wipedUntil;

	/// <summary>Fast and in control: nobody can get a hand on it.</summary>
	public bool Untouchable => Charge >= SlipThreshold && !WipedOut;
	public float SpeedBoost => WipedOut ? WipeoutSpeed : 1f + SpeedBonus * Charge;

	/// <summary>Clean hits and wipeouts this run.</summary>
	public int Clean { get; private set; }
	public int Wipeouts { get; private set; }
	float _wipedUntil;

	/// <summary>People bowled over this run.</summary>
	public int Bowled { get; private set; }

	ReptileController _player;
	StadiumGame _game;
	bool _jumpPressed;

	protected override void OnStart()
	{
		_player = Components.Get<ReptileController>();
		_game = StadiumGame.Get( Scene );
	}

	protected override void OnUpdate()
	{
		if ( Input.Pressed( "Jump" ) ) _jumpPressed = true;
	}

	protected override void OnFixedUpdate()
	{
		if ( !_player.IsValid() || !_game.IsValid() ) return;
		bool jump = _jumpPressed;
		_jumpPressed = false;

		if ( _game.Phase != StadiumPhase.Escape || _player.InputLocked || _game.Pinned )
		{
			Charge = 0f;
			_player.PassCrowd = false;
			return;
		}

		float dt = Time.Delta;
		var velocity = _player.Velocity;
		var flat = velocity.WithZ( 0f );
		float speed = flat.Length;

		// Sprinting flat out builds it; anything slower lets it drain.
		bool sprinting = speed > _player.SprintSpeed * 0.75f;
		Charge = Math.Clamp( Charge + (sprinting ? dt / MathF.Max( BuildSeconds, 0.05f ) : -dt / MathF.Max( DrainSeconds, 0.05f )), 0f, 1f );
		_player.PassCrowd = Ploughing;

		if ( speed < 1f ) return;
		var forward = flat / speed;
		var side = new Vector3( -forward.y, forward.x, 0f );
		var me = _player.WorldPosition;
		float now = Time.Now;
		bool trampled = false;

		foreach ( var person in Scene.GetAllComponents<CrowdPerson>() )
		{
			if ( person.State == CrowdState.Dead ) continue;
			var to = person.WorldPosition - me;
			if ( MathF.Abs( to.z ) > 90f ) continue;
			float distance = to.WithZ( 0f ).Length;

			// The trample: a jump with somebody underfoot stamps them down and flings the lizard on.
			if ( jump && !trampled && distance < TrampleRadius && Charge > 0.1f )
			{
				trampled = true;
				person.Bowl( -forward * 120f + Vector3.Down * 200f, now );
				_player.Lunge( forward, TrampleBoost );
				_player.Kick( Vector3.Down * 5f );
				Charge = MathF.Min( Charge + TrampleGain, 1f );
				Thud( person.WorldPosition, 0.9f );
				Bowled++;
				continue;
			}

			if ( distance < 1f ) continue;
			var toward = to.WithZ( 0f ) / distance;

			// Bodies are solid: anyone pressed against the lizard pushes it back, and is pushed.
			if ( distance < BodyRadius && person.State != CrowdState.Down )
			{
				float press = (1f - distance / BodyRadius) * BodyPush * dt;
				_player.Lunge( -toward, press );
				person.Push( toward * press, 0f, now );
			}

			if ( !Ploughing || distance > HitRadius || now - person.LastBowled < 0.7f ) continue;
			float along = Vector3.Dot( to, forward );
			if ( along < -10f ) continue; // already behind

			// Where on the lizard they land: 0 is dead centre, 1 is the very edge of a shoulder.
			float across = Vector3.Dot( to, side );
			float sideways = across >= 0f ? 1f : -1f;
			float offset = Math.Clamp( MathF.Abs( across ) / HitRadius, 0f, 1f );

			if ( offset >= CleanOffset )
			{
				// The right way: a shoulder. They go spinning off to that side, still on their feet,
				// and the lizard's line is knocked a little the other way.
				float glance = (offset - CleanOffset) / MathF.Max( 1f - CleanOffset, 0.01f ); // 0 barely clean, 1 a brush
				person.Push( side * (sideways * speed * ThrowSideways * (1.2f - 0.5f * glance)) + forward * (speed * 0.25f) + Vector3.Up * 40f, 0.8f, now );
				_player.Lunge( -side * sideways, speed * Deflection * (1f - glance) );
				Charge = MathF.Max( Charge - CostPerHit, 0f );
				_player.Kick( -side * sideways * 2f );
				Thud( person.WorldPosition, 0.45f );
				Clean++;
				Bowled++;
				continue;
			}

			// The wrong way: into them. The straighter the hit, the more speed and charge it costs.
			float square = 1f - offset / MathF.Max( CleanOffset, 0.01f ); // 1 dead centre, 0 nearly clean
			person.Bowl( forward * (speed * ThrowForward) + side * (sideways * speed * ThrowSideways * 0.4f) + Vector3.Up * ThrowUp, now );
			_player.Lunge( -forward, speed * HeadOnSpeedLoss * square );
			Charge = MathF.Max( Charge - CostPerHit - HeadOnCost * square, 0f );
			_player.Kick( (-forward + Vector3.Up * 0.5f) * (3f + 6f * square) );
			Thud( person.WorldPosition, 0.6f + 0.4f * square );
			Bowled++;

			// Dead centre at full tilt: the lizard goes down too, and the crowd gets its chance.
			if ( offset < WipeoutOffset && speed > _player.SprintSpeed * 0.9f )
			{
				Charge = 0f;
				_wipedUntil = now + WipeoutSeconds;
				_player.PassCrowd = false;
				_player.Kick( Vector3.Random * 14f + Vector3.Down * 10f );
				Wipeouts++;
				break;
			}
		}
	}

	void Thud( Vector3 at, float volume )
	{
		if ( string.IsNullOrEmpty( HitSound ) ) return;
		var sound = Sound.Play( HitSound, at );
		if ( sound.IsValid() ) { sound.Volume = volume; sound.Pitch = Game.Random.Float( 1.3f, 1.7f ); }
	}
}