Rampage/Weapon/DongWeapon.Mega.cs

Partial class of the giant's DongWeapon. Runs the moves that use both weapons at once: the two-button charged mega-swing with its miss stumble, the clap that ends a combo, and the air ground-pound and sit-down dives, each with its blast, shockwave, sound and camera kick.

using System;

// The moves that take both weapons at once: the charged mega-swing (and the stumble when it
// misses), the clap that ends a combo, and the two dives from the air. While one of these is
// going the two buttons do nothing else. See DongWeapon.cs for the whole picture.
public sealed partial class DongWeapon
{
	// ---- Mega-swing (hold both) ----

	/// <summary>Both buttons held this long starts the charge.</summary>
	[Property, Group( "Mega" )] public float BothSeconds { get; set; } = 0.14f;
	[Property, Group( "Mega" )] public float ChargeSeconds { get; set; } = 2.6f;
	[Property, Group( "Mega" )] public float ChargeStrength { get; set; } = 200f;
	/// <summary>How much both weapons swell at full charge, and how much longer a full mega-swing reaches.</summary>
	[Property, Group( "Mega" )] public float ChargeSwell { get; set; } = 0.3f;
	[Property, Group( "Mega" )] public float MegaReach { get; set; } = 0.5f;
	[Property, Group( "Mega" )] public float ChargeSlow { get; set; } = 0.35f;
	/// <summary>How much time slows at full charge (0.25: to three quarters speed). Never while anything else has the clock.</summary>
	[Property, Group( "Mega" ), Range( 0f, 0.6f )] public float TimeDip { get; set; } = 0.25f;
	[Property, Group( "Mega" )] public float MegaSeconds { get; set; } = 0.95f;
	[Property, Group( "Mega" )] public float MegaStrength { get; set; } = 520f;
	/// <summary>Power of what the weapons themselves touch on the way round, uncharged; a full charge is 2.7 times that.</summary>
	[Property, Group( "Mega" )] public float MegaPower { get; set; } = 3f;
	/// <summary>Degrees the swing goes round at full charge (barely charged, a little over half of it).</summary>
	[Property, Group( "Mega" )] public float MegaArc { get; set; } = 300f;
	/// <summary>
	/// The swing itself, whatever the chain happens to touch: everything in front within this many
	/// reaches (and 0.9 more at full charge) takes this damage (times might; 0.4 of it barely
	/// charged, twice it fully), less at the edge. A full one is a block of houses.
	/// </summary>
	[Property, Group( "Mega" )] public float MegaRadius { get; set; } = 1.1f;
	[Property, Group( "Mega" )] public float MegaDamage { get; set; } = 900f;
	[Property, Group( "Mega" )] public float MegaPush { get; set; } = 1300f;
	[Property, Group( "Mega" )] public float MegaPop { get; set; } = 1500f;
	[Property, Group( "Mega" )] public float MegaShockSpeed { get; set; } = 2600f;
	/// <summary>A swing charged at least this far that touches nothing at all puts the giant on its backside for this long.</summary>
	[Property, Group( "Mega" )] public float StumbleCharge { get; set; } = 0.35f;
	[Property, Group( "Mega" )] public float StumbleSeconds { get; set; } = 1.4f;
	[Property, Group( "Mega" )] public float StumbleCrawl { get; set; } = 0.08f;

	// ---- The clap (right click as the second whip lands) ----

	[Property, Group( "Clap" )] public float ClapSeconds { get; set; } = 0.62f;
	[Property, Group( "Clap" )] public float ClapStrength { get; set; } = 600f;
	/// <summary>How far in front they meet, and how wide the place is, as shares of the reach.</summary>
	[Property, Group( "Clap" )] public float ClapReach { get; set; } = 0.75f;
	[Property, Group( "Clap" )] public float ClapRadius { get; set; } = 0.42f;
	/// <summary>Straight up, this fast, with this much damage (times might).</summary>
	[Property, Group( "Clap" )] public float ClapLaunch { get; set; } = 2800f;
	[Property, Group( "Clap" )] public float ClapDamage { get; set; } = 1100f;

	// ---- From the air ----

	/// <summary>Ground pound (left, in the air): dive speed, shockwave radius in reaches, damage (times might) and pop.</summary>
	[Property, Group( "Air" )] public float PoundDive { get; set; } = 1300f;
	[Property, Group( "Air" )] public float PoundShock { get; set; } = 1.3f;
	[Property, Group( "Air" )] public float PoundDamage { get; set; } = 600f;
	[Property, Group( "Air" )] public float PoundPop { get; set; } = 1300f;
	/// <summary>Sit-down (right, in the air): dive speed; the crater's radius in reaches (the size of the town square); knock-up; damage; the bounce.</summary>
	[Property, Group( "Air" )] public float SackDive { get; set; } = 1100f;
	[Property, Group( "Air" )] public float SitRadius { get; set; } = 1.6f;
	[Property, Group( "Air" )] public float SitKnockUp { get; set; } = 2600f;
	[Property, Group( "Air" )] public float SitDamage { get; set; } = 800f;
	[Property, Group( "Air" )] public float SackBounce { get; set; } = 500f;

	float _charge, _megaCharge, _stumbleSeconds, _stumbleSpin;
	bool _chargeSounded, _fullTold, _stumbleThud;
	int _hitCount;
	SoundHandle _chargeSound;

	// The dip in time while charging.
	bool _dipping, _dipLost;
	float _dipSet = 1f;

	void BeginJoint( Joint joint )
	{
		_joint = joint;
		_jointT = 0f;
		_stage = 0;
		_impactDone = false;
		_holdLook = false;
		_dong = DongMode.Carried;
		_balls = BallMode.Hang;
		_spinRate = 0f;
		_buffered = false;
		Draw01 = 0f;
		Combo = 0;
		_aimYaw = _controller.Look.yaw;
	}

	void EndJoint( float rest )
	{
		_joint = Joint.None;
		Charge01 = 0f;
		_restUntil = Time.Now + rest;
		_ballRestUntil = Time.Now + rest;
	}

	// CHARGE: both weapons hauled back, swelling and trembling, the camera backing away and time
	// itself dragging a little. Still aimed with the camera.

	void StartCharge()
	{
		BeginJoint( Joint.Charge );
		_charge = 0f;
		_chargeSounded = false;
		_fullTold = false;
		_dipLost = false;
		Charge01 = 0f;
		Tell( "charge", 0f );
	}

	Angles UpdateCharge( float dt )
	{
		float rate = GiantRiders.ChargeRate;
		_charge += dt;
		Charge01 = Math.Clamp( _charge / MathF.Max( ChargeSeconds, 0.1f ) * rate, 0f, 1f );
		float c = Charge01;
		_aimYaw = _controller.Look.yaw;
		if ( !_chargeSounded && _charge > 0.2f ) { _chargeSounded = true; _chargeSound = Play( "dong_charge", _rig.DongRoot, 0.9f, 1f ); }
		if ( c >= 1f && !_fullTold )
		{
			_fullTold = true;
			Play( "dong_charge_full", _rig.DongRoot, 1f, 1f );
			Tell( "charge", 1f );
		}
		DipTime( c );

		float now = Time.Now;
		_rig.DongDrive = Dir( _aimYaw, -150f + MathF.Sin( now * 17f ) * 8f * c, 28f + MathF.Sin( now * 23f ) * 6f * c );
		_rig.DongDriveStrength = ChargeStrength / Slow;
		_rig.SackDrive = Dir( _aimYaw, 170f, -20f );
		_rig.SackDriveStrength = ChargeStrength;
		_rig.SackChain = c * 0.2f * Reach;
		_power = _ballPower = DragPower;
		_knock = _ballKnock = 0.6f;
		DongState = BallsState = c >= 1f ? "FULL. LET GO." : "WOUND BACK";

		bool release = !CanAct || !(_down1 && _down2) || _charge > ChargeSeconds / MathF.Max( rate, 0.1f ) + 3.5f;
		if ( release )
		{
			// Barely begun: both buttons brushed together by accident. Nothing happens.
			if ( c < 0.08f ) { StopCharge(); EndTimeDip(); EndJoint( 0.1f ); }
			else StartMega( c );
		}
		return new Angles( -8f - 12f * c + MathF.Sin( now * 47f ) * 2.5f * c, -20f * c + MathF.Sin( now * 39f ) * 2f * c, 0f );
	}

	void StopCharge()
	{
		if ( _chargeSound.IsValid() && !_chargeSound.IsStopped ) _chargeSound.Stop( 0.1f );
		_chargeSound = null;
	}

	/// <summary>
	/// Slows the scene a little as the charge fills. Only ever from full speed, and it lets go at
	/// once if anybody else changes the clock meanwhile (it is theirs then, to put back).
	/// </summary>
	void DipTime( float charge )
	{
		if ( TimeDip <= 0f || _dipLost || Scene is null ) return;
		if ( !_dipping )
		{
			if ( MathF.Abs( Scene.TimeScale - 1f ) > 0.01f ) return;
			_dipping = true;
		}
		else if ( MathF.Abs( Scene.TimeScale - _dipSet ) > 0.01f )
		{
			_dipping = false;
			_dipLost = true;
			return;
		}
		_dipSet = 1f - TimeDip * charge;
		Scene.TimeScale = _dipSet;
	}

	void EndTimeDip()
	{
		if ( !_dipping ) return;
		_dipping = false;
		if ( Scene is not null && MathF.Abs( Scene.TimeScale - _dipSet ) < 0.01f ) Scene.TimeScale = 1f;
	}

	// MEGA-SWING: from wound right back, round through the front and on past it, with the balls
	// let out and following a little behind. The whole body goes with it.

	void StartMega( float charge )
	{
		StopCharge();
		EndTimeDip();
		BeginJoint( Joint.Mega );
		_megaCharge = charge;
		_hitCount = 0;
		Charge01 = charge;
		_power = _ballPower = MegaPower * (1f + 1.7f * charge);
		_knock = _ballKnock = 1.4f;
		Play( "dong_whoosh", _rig.DongRoot, 1f, SizePitch( 0.6f ) );
		Shove( Dir( _aimYaw, 0f, 0f ), WhipLunge * G * (1f + charge) );
	}

	Angles UpdateMega( float dt )
	{
		float c = _megaCharge;
		float duration = MegaSeconds * Slow * (1f + 0.3f * c);
		float p = _jointT / MathF.Max( duration, 0.01f );
		float q = Ease( p );
		float sweep = MegaArc * (0.55f + 0.45f * c);
		float yaw = -150f + sweep * q;

		_rig.DongDrive = Dir( _aimYaw, yaw, Lerp( 14f, 3f, q ) );
		_rig.DongDriveStrength = (p < 0.75f ? MegaStrength * (1f + c) : IdleStrength * 6f) / (Slow * Slow);
		_rig.SackDrive = Dir( _aimYaw, yaw - 40f, 4f );
		_rig.SackDriveStrength = MegaStrength;
		_rig.SackChain = 0.5f * Reach * MathF.Sin( MathF.Min( p * 1.2f, 1f ) * MathF.PI );
		DongState = BallsState = "MEGA-SWING";

		// A full one takes the giant right round with it.
		float twist = Lerp( -BodyTwist, BodyTwist, q );
		if ( c >= 0.9f ) _bodySpin = 360f * Ease( (p - 0.3f) / 0.7f );

		if ( _stage == 0 && p >= 0.5f )
		{
			_stage = 1;
			MegaImpact( c );
		}
		if ( p >= 1f )
		{
			_rig.SackChain = 0f;
			if ( _hitCount == 0 && c >= StumbleCharge ) StartStumble( c );
			else EndJoint( 0.3f );
		}
		return new Angles( Lerp( -14f, 20f, q ), twist, 0f );
	}

	/// <summary>The middle of the swing: everything in front goes, a shockwave follows it, and a full one cracks the street beyond.</summary>
	void MegaImpact( float charge )
	{
		var forward = Dir( _aimYaw, 0f, 0f );
		var at = _controller.WorldPosition;
		float radius = Reach * (MegaRadius + 0.9f * charge);

		_hitCount += ArcBlast( at, forward, radius, MegaDamage * (0.4f + 1.6f * charge) * Rampage.Might, MegaPush, MegaPop * (0.6f + 0.6f * charge) );

		float speed = MegaShockSpeed * (0.5f + charge);
		Rampage.SendShockwave( at + forward * (radius * 0.5f), radius * (0.8f + 0.8f * charge) * GiantRiders.ShockBonus, speed );
		if ( charge >= 0.9f ) StartCracks( at + forward * (radius * 0.6f), forward, radius * 0.55f, speed, 5 + Rampage.Tier );

		Thump( 0.5f + 0.5f * charge );
		_controller.Kick( Vector3.Down * (8f + 14f * charge) );
		Play( "dong_mega", at + forward * (radius * 0.5f), 1f, SizePitch( 1f ) );
		Puff( at + forward * (radius * 0.4f), 8, radius * 0.25f );
		Tell( "mega", charge );
	}

	// STUMBLE: all that weight, and nothing on the end of it to stop it. Round he goes, and down.

	void StartStumble( float charge )
	{
		BeginJoint( Joint.Stumble );
		_stumbleSeconds = StumbleSeconds * (1f + 0.5f * charge);
		_stumbleSpin = 540f * (0.6f + 0.4f * charge);
		_stumbleThud = false;
		Shove( Dir( _aimYaw, 90f, 0f ), MaxShove * G );
		_controller.Kick( Vector3.Random * 14f );
		Play( "giant_stumble", _controller.WorldPosition, 1f, 1f );
		Tell( "miss", charge );
	}

	Angles UpdateStumble( float dt )
	{
		float p = Math.Clamp( _jointT / MathF.Max( _stumbleSeconds, 0.1f ), 0f, 1f );
		float left = 1f - p;
		_bodySpin += _stumbleSpin * left * left * dt;

		_rig.DongDriveStrength = 0f;
		HangBalls();
		_power = _ballPower = DragPower;
		DongState = "DROPPED";
		BallsState = "SAT ON";

		// Down onto his backside, hard, a third of the way through.
		float down = MathF.Sin( MathF.Min( p * 1.3f, 1f ) * MathF.PI );
		_hop = -G * 5f * down;
		if ( !_stumbleThud && p >= 0.35f )
		{
			_stumbleThud = true;
			var at = _controller.WorldPosition;
			Rampage.SendShockwave( at, Reach * 0.6f, 800f );
			Thump( 0.6f );
			Play( "giant_land", at, 1f, 0.8f );
			Puff( at, 5, Reach * 0.1f );
		}

		if ( p >= 1f ) EndJoint( 0.2f );
		float now = Time.Now;
		return new Angles( -38f * down, MathF.Sin( now * 9f ) * 12f * left, MathF.Sin( now * 7f ) * 14f * left );
	}

	// THE CLAP: the dong from the left, the balls from the right, meeting in front. Whatever is
	// between them when they meet goes straight up.

	void StartClap()
	{
		BeginJoint( Joint.Clap );
		_clapUntil = 0f;
		_combo = 0;
		_comboUntil = 0f;
		_power = _ballPower = 1.6f;
		_knock = _ballKnock = 1.2f;
		Play( "dong_whoosh", _rig.DongRoot, 1f, SizePitch( 0.9f ) );
	}

	Angles UpdateClap()
	{
		const float Apart = 0.45f, Meet = 0.74f;
		float p = _jointT / MathF.Max( ClapSeconds * Slow, 0.01f );
		float open = p < Apart ? Lerp( 60f, 100f, Ease( p / Apart ) ) : Lerp( 100f, 6f, Ease( (p - Apart) / (Meet - Apart) ) );
		float lift = p < Apart ? 16f : Lerp( 16f, 2f, (p - Apart) / (Meet - Apart) );

		_rig.DongDrive = Dir( _aimYaw, open, lift );
		_rig.DongDriveStrength = ClapStrength / (Slow * Slow);
		_rig.SackDrive = Dir( _aimYaw, -open, lift * 0.7f );
		_rig.SackDriveStrength = ClapStrength;
		_rig.SackChain = ClapReach * Reach * 0.8f * MathF.Min( p / Apart, 1f );
		DongState = BallsState = "THE CLAP";

		if ( _stage == 0 && p >= Meet )
		{
			_stage = 1;
			var forward = Dir( _aimYaw, 0f, 0f );
			var at = _controller.WorldPosition + forward * (Reach * ClapReach);
			_hitCount += Blast( at, Reach * ClapRadius, ClapLaunch, 0f, ClapDamage * Rampage.Might );
			Rampage.SendShockwave( at, Reach * 0.9f * GiantRiders.ShockBonus, 1500f );
			Thump( 0.8f );
			_controller.Kick( Vector3.Up * 16f );
			Play( "clap_thunder", at, 1f, SizePitch( 1f ) );
			CrackFlash( at + Vector3.Up * (Reach * 0.2f) );
			Puff( at, 6, Reach * 0.12f );
			Tell( "clap", 1f );
		}
		if ( p >= 1f ) EndJoint( 0.3f );
		return new Angles( Lerp( -10f, 22f, p ), 0f, 0f );
	}

	// GROUND POUND: left, in the air. The giant dives and the landing is the blow.

	void StartPound()
	{
		BeginJoint( Joint.Pound );
		_fallSpeed = 0f;
		_power = 2.5f;
		_knock = 1.2f;
		_controller.Lunge( Vector3.Down, PoundDive );
		Play( "dong_whoosh", _rig.DongRoot, 1f, SizePitch( 0.7f ) );
	}

	Angles UpdatePound()
	{
		HangBalls();
		DongState = "GROUND POUND";
		if ( !_impactDone )
		{
			_rig.DongDrive = Dir( _aimYaw, 0f, -70f );
			_rig.DongDriveStrength = WhipStrength / (Slow * Slow);
			if ( _controller.Attached || _jointT > 3f )
			{
				_impactDone = true;
				_jointT = 0f;
				float hard = Math.Clamp( _fallSpeed / MathF.Max( PoundDive, 1f ), 0.6f, 1.8f );
				var at = _controller.WorldPosition;
				float radius = Reach * PoundShock * hard;
				Blast( at, radius * 0.6f, PoundPop * hard, 500f, PoundDamage * Rampage.Might * hard );
				Rampage.SendShockwave( at, radius * GiantRiders.ShockBonus, SlamShockSpeed * 1.4f * hard );
				Thump( 0.8f );
				_controller.Kick( Vector3.Down * 16f );
				Play( "dong_pound", at, 1f, SizePitch( 1f ) );
				Puff( at, 8, radius * 0.2f );
				Crater( at, radius * 0.45f );
				Tell( "pound", hard * 0.5f );
			}
			return new Angles( 30f, 0f, 0f );
		}

		_rig.DongDriveStrength = IdleStrength;
		if ( _jointT >= 0.35f ) EndJoint( 0.1f );
		return new Angles( 18f * (1f - _jointT / 0.35f), 0f, 0f );
	}

	// SIT-DOWN: right, in the air. The giant sits on the balls, on whatever is underneath, and
	// bounces. The crater is the size of the town square.

	void StartSitDown()
	{
		BeginJoint( Joint.SitDown );
		_fallSpeed = 0f;
		_ballPower = 2.5f;
		_ballKnock = 1f;
		_controller.Lunge( Vector3.Down, SackDive );
		Play( "sack_swing", _rig.SackPoint, 0.8f, SizePitch( 0.8f ) );
	}

	Angles UpdateSitDown()
	{
		_rig.SackDrive = Vector3.Down;
		_rig.SackDriveStrength = FlailStrength;
		_rig.SackChain = _rig.SackRadius * 0.5f;
		_rig.DongDrive = Dir( _aimYaw, 0f, 60f );   // held up out of the way
		_rig.DongDriveStrength = ChargeStrength / Slow;
		DongState = "OUT OF THE WAY";
		BallsState = "SIT-DOWN";
		if ( _jointT > 3f ) EndJoint( 0.1f );
		return new Angles( -26f, 0f, 0f );   // sitting back into it
	}

	/// <summary>
	/// Run each physics step while dropping. The landing is taken just before the ground, while
	/// the giant is still in the air, because that is the only time it can be thrown back up:
	/// that is the bounce.
	/// </summary>
	void WatchSitDown( float dt )
	{
		var feet = _controller.WorldPosition;
		float falling = -_controller.Velocity.z;
		bool landed = _controller.Attached;

		if ( !landed && falling > 0f )
		{
			float reach = MathF.Max( 12f, falling * dt * 2f ) + _rig.SackRadius;
			float ground = RampageWorld.GroundHeightAt( feet );
			if ( feet.z - ground > reach )
			{
				var trace = Scene.Trace.Ray( feet + Vector3.Up * 4f, feet - Vector3.Up * reach )
					.IgnoreGameObjectHierarchy( _controller.GameObject ).WithoutTags( "debris", "crowd" ).Run();
				if ( !trace.Hit ) return;
				feet = trace.HitPosition;
			}
			else feet = feet.WithZ( ground );
		}
		else if ( !landed ) return;

		float hard = Math.Clamp( MathF.Max( falling, _fallSpeed ) / MathF.Max( SackDive, 1f ), 0.6f, 1.6f );
		float radius = Reach * SitRadius;
		Blast( feet, radius * 0.7f, SitKnockUp * hard, 300f, SitDamage * Rampage.Might * hard );
		Rampage.SendShockwave( feet, radius * 1.4f * GiantRiders.ShockBonus, 1500f * hard );
		Thump( 0.9f );
		_controller.Kick( Vector3.Down * 14f );
		Play( "sack_drop", feet, 1f, SizePitch( 1f ) );
		Play( "sack_boing", feet, 1f, SizePitch( 1.1f ) );
		Puff( feet, 10, radius * 0.3f );
		Crater( feet, radius );
		Tell( "sitdown", hard * 0.6f );

		// The comedy bounce (only possible while still off the ground).
		if ( !landed ) _controller.Lunge( Vector3.Up, MathF.Min( falling + SackBounce, 2400f ) );
		_fallSpeed = 0f;
		EndJoint( 0.15f );
	}
}