Rampage/Weapon/DongWeapon.Balls.cs

Partial of DongWeapon covering the secondary attack, a pair of balls on a chain. Tap right to swing a heavy flail across the front that bounces off anything still standing, hold right to draw a slingshot between the legs and fire it along the line of sight, with a blast, shockwave, crater and a knock-back on the return.

using System;

// The balls, on Attack2: a wrecking ball on a short chain when tapped, a slingshot when held.
// They are their own weapon, with their own button, and can be going while the dong is doing
// something else entirely. See DongWeapon.cs for the whole picture.
public sealed partial class DongWeapon
{
	/// <summary>Right released this soon was a tap: the flail. Held longer, it is the slingshot being drawn.</summary>
	[Property, Group( "Balls" )] public float BallTapSeconds { get; set; } = 0.2f;

	// ---- Flail (tap right) ----

	/// <summary>Seconds for the half-circle from one side to the other. Slow: it is very heavy.</summary>
	[Property, Group( "Flail" )] public float FlailSeconds { get; set; } = 0.85f;
	[Property, Group( "Flail" )] public float FlailStrength { get; set; } = 520f;
	/// <summary>How far they are let out on their chain, as a share of the dong's reach.</summary>
	[Property, Group( "Flail" )] public float FlailChain { get; set; } = 0.55f;
	/// <summary>Power and knock-back next to a whip's 1: this is the one that takes a house.</summary>
	[Property, Group( "Flail" )] public float FlailPower { get; set; } = 2.5f;
	[Property, Group( "Flail" )] public float FlailKnock { get; set; } = 1.7f;

	// ---- Slingshot (hold right) ----

	/// <summary>Seconds to draw it right back.</summary>
	[Property, Group( "Slingshot" )] public float DrawSeconds { get; set; } = 1.1f;
	/// <summary>How far back between the legs they stretch at full draw, as a share of the reach.</summary>
	[Property, Group( "Slingshot" )] public float DrawStretch { get; set; } = 0.45f;
	/// <summary>How far they fly, in reaches: barely drawn, and that much further again fully drawn.</summary>
	[Property, Group( "Slingshot" )] public float SlingMin { get; set; } = 1.4f;
	[Property, Group( "Slingshot" )] public float SlingMax { get; set; } = 3.4f;
	[Property, Group( "Slingshot" )] public float SlingOutSeconds { get; set; } = 0.3f;
	[Property, Group( "Slingshot" )] public float SlingBackSeconds { get; set; } = 0.45f;
	[Property, Group( "Slingshot" )] public float SlingStrength { get; set; } = 900f;
	[Property, Group( "Slingshot" )] public float SlingPower { get; set; } = 2.2f;
	/// <summary>Where it lands at full stretch: damage (times might, more the further it was drawn).</summary>
	[Property, Group( "Slingshot" )] public float SlingDamage { get; set; } = 900f;
	/// <summary>How much the giant can still move while braced to fire.</summary>
	[Property, Group( "Slingshot" )] public float SlingBrace { get; set; } = 0.3f;

	float _ballPressAt, _ballYaw;
	float _flailYaw, _flailDir = 1f;
	bool _flailBounced, _ballHitBig, _drawSounded;
	float _slingRange, _slingPower, _slingFrom;

	const float FlailFrom = -105f, FlailTo = 78f;

	Angles UpdateBalls( float dt, ref float factor, ref WeaponHint lit )
	{
		switch ( _balls )
		{
			case BallMode.Draw: return UpdateDraw( dt, ref factor, ref lit );
			case BallMode.Flail: lit |= WeaponHint.Flail; factor *= 0.6f; return UpdateFlail( dt );
			case BallMode.Snap:
			case BallMode.Out:
			case BallMode.Back: lit |= WeaponHint.Sling; factor *= SlingBrace; return UpdateSling();
		}

		HangBalls();
		_ballPower = DragPower;
		_ballKnock = 0.6f;
		Draw01 = 0f;
		BallsState = "HANGING";
		return default;
	}

	void StartDraw()
	{
		_balls = BallMode.Draw;
		_ballT = 0f;
		_ballPressAt = Time.Now;
		_ballHitBig = false;
		_drawSounded = false;
		Draw01 = 0f;
	}

	// SLINGSHOT: drawn back between the legs on their elastic while right is held. The camera
	// still turns, so it is aimed by looking; a line of markers shows where it will go.

	Angles UpdateDraw( float dt, ref float factor, ref WeaponHint lit )
	{
		float held = Time.Now - _ballPressAt;
		if ( !_down2 )
		{
			if ( held < BallTapSeconds ) StartFlail();
			else if ( Draw01 > 0.08f ) Fire();
			else _balls = BallMode.Hang;
			return default;
		}
		if ( held < BallTapSeconds )
		{
			HangBalls();
			return default;
		}

		lit |= WeaponHint.Sling;
		factor *= SlingBrace;
		Draw01 = MathF.Min( Draw01 + dt / MathF.Max( DrawSeconds * Slow, 0.05f ) * GiantRiders.DrawRate, 1f );
		_ballYaw = _controller.Look.yaw;
		if ( !_drawSounded ) { _drawSounded = true; Play( "sack_stretch", _rig.SackPoint, 0.9f, SizePitch( 1f ) ); }

		_rig.SackDrive = Dir( _ballYaw, 180f, -28f );
		_rig.SackDriveStrength = SlingStrength * 0.4f;
		_rig.SackChain = Draw01 * DrawStretch * Reach;
		_ballPower = DragPower;
		_ballKnock = 0.6f;
		BallsState = Draw01 >= 1f ? "FULLY DRAWN" : "DRAWING";

		// Braced: leaning into it, and trembling the further back it goes.
		return new Angles( 8f * Draw01 + MathF.Sin( Time.Now * 41f ) * 1.5f * Draw01, 0f, 0f );
	}

	/// <summary>How far a shot drawn this far will fly, in inches.</summary>
	float SlingRangeAt( float draw ) => Reach * (SlingMin + SlingMax * Math.Clamp( draw, 0f, 1f ));

	void Fire()
	{
		_slingPower = Draw01;
		_slingRange = SlingRangeAt( Draw01 );
		_slingFrom = _rig.SackChain;
		_ballYaw = _controller.Look.yaw;
		_ballHitBig = false;
		_balls = BallMode.Snap;
		_ballT = 0f;
		Play( "sack_twang", _rig.SackPoint, 1f, SizePitch( 1.1f ) );
		_controller.Kick( Dir( _ballYaw, 0f, 0f ) * (6f + 8f * _slingPower) );
		Tell( "sling", _slingPower );
	}

	/// <summary>
	/// Fired: gathered in for an instant, out along the line of sight to the end of the elastic, and
	/// snapped back. The rig holds the balls at exactly the length of chain they are given, so the
	/// flight is that length growing and shrinking; whatever the ball passes through on the way out
	/// and on the way back is hit by the sweep in DongWeapon.Hits.cs.
	/// </summary>
	Angles UpdateSling()
	{
		var forward = Dir( _ballYaw, 0f, 0f );
		float hip = MathF.Max( _rig.DongRoot.z - _controller.WorldPosition.z, 1f );
		float down = -MathF.Atan2( hip * 0.8f, MathF.Max( _slingRange, 1f ) ) * (180f / MathF.PI);

		_rig.SackDrive = Dir( _ballYaw, 0f, down );
		_rig.SackDriveStrength = SlingStrength;
		_ballPower = SlingPower * (0.6f + _slingPower);
		_ballKnock = 1.6f;
		Draw01 = _balls == BallMode.Back ? 0f : _slingPower;

		switch ( _balls )
		{
			case BallMode.Snap:
			{
				float p = _ballT / 0.1f;
				_rig.SackChain = Lerp( _slingFrom, 0f, p );
				BallsState = "FIRED";
				if ( p >= 1f ) { _balls = BallMode.Out; _ballT = 0f; }
				return new Angles( 14f, 0f, 0f );
			}
			case BallMode.Out:
			{
				float p = Math.Clamp( _ballT / MathF.Max( SlingOutSeconds * Slow, 0.01f ), 0f, 1f );
				_rig.SackChain = _slingRange * (1f - (1f - p) * (1f - p));
				BallsState = "FIRED";

				// A building in the way stops it short, and it comes back from there.
				if ( _ballHitBig || p >= 1f )
				{
					if ( !_ballHitBig ) SlingImpact();
					else Play( "sack_boing", _rig.SackPoint, 1f, SizePitch( 1f ) );
					_ballHitBig = false;
					_slingFrom = _rig.SackChain;
					_balls = BallMode.Back;
					_ballT = 0f;
				}
				return new Angles( 20f, 0f, 0f );
			}
			default:
			{
				float p = Math.Clamp( _ballT / MathF.Max( SlingBackSeconds * Slow, 0.01f ), 0f, 1f );
				_rig.SackChain = _slingFrom * (1f - p * p);
				BallsState = "SNAPPING BACK";
				if ( p >= 1f )
				{
					// Home, with everything they had: the giant is knocked back a step.
					_rig.SackChain = 0f;
					_balls = BallMode.Hang;
					_ballRestUntil = Time.Now + 0.25f;
					Shove( -forward, MaxShove * G * (0.3f + 0.7f * _slingPower) );
					_controller.Kick( -forward * (8f + 10f * _slingPower) );
					Play( "sack_boing", _rig.SackPoint, 1f, SizePitch( 0.9f ) );
					Tell( "sling_back", _slingPower );
				}
				return new Angles( Lerp( 20f, -12f, p ), 0f, 0f );
			}
		}
	}

	/// <summary>The end of the elastic: what is there is thrown, and a shockwave goes out.</summary>
	void SlingImpact()
	{
		var at = _rig.SackPoint.WithZ( _controller.WorldPosition.z );
		float radius = _rig.SackRadius;
		Blast( at, radius * 2.2f, 1000f, 800f, SlingDamage * (0.5f + _slingPower) * Rampage.Might );
		Rampage.SendShockwave( at, radius * 5f * (0.5f + _slingPower) * GiantRiders.ShockBonus, 1300f );
		Thump( 0.3f + 0.4f * _slingPower );
		Play( "sack_impact", at, 1f, SizePitch( 1f ) );
		Puff( at, 4, radius );
		Crater( at, radius * 2.4f );
	}

	// FLAIL: let out on a short chain and brought once round the front, low, from the right to
	// the left. It takes its time. If it meets a building that does not fall, it bounces off and
	// comes back the way it came, through everything again.

	void StartFlail()
	{
		_balls = BallMode.Flail;
		_ballT = 0f;
		_flailYaw = FlailFrom;
		_flailDir = 1f;
		_flailBounced = false;
		_ballHitBig = false;
		_ballYaw = _controller.Look.yaw;
		Draw01 = 0f;
		Play( "sack_swing", _rig.SackPoint, 0.9f, SizePitch( 1f ) );
		Tell( "flail", 0.5f );
	}

	Angles UpdateFlail( float dt )
	{
		// Slow to start: the weight has to be got moving.
		float rate = (FlailTo - FlailFrom) / MathF.Max( FlailSeconds * Slow, 0.05f ) * 1.3f;
		float ramp = MathF.Min( 0.3f + _ballT * 2.6f, 1f );
		_flailYaw += rate * ramp * _flailDir * (_flailBounced ? 1.25f : 1f) * dt;

		if ( _ballHitBig && !_flailBounced )
		{
			_flailBounced = true;
			_flailDir = -1f;
			_rig.SackImpulse( -_rig.SackVelocity * 1.4f );
			Shove( -Dir( _ballYaw, _flailYaw, 0f ), MaxShove * G * 0.6f );
			_controller.Kick( Vector3.Random * 8f );
			Play( "sack_boing", _rig.SackPoint, 1f, SizePitch( 0.9f ) );
			Tell( "flail_bounce", 1f );
		}
		_ballHitBig = false;

		_rig.SackDrive = Dir( _ballYaw, _flailYaw, 5f );
		_rig.SackDriveStrength = FlailStrength / Slow;
		_rig.SackChain = FlailChain * Reach * MathF.Min( _ballT * 5f, 1f );
		_ballPower = FlailPower;
		_ballKnock = FlailKnock;
		BallsState = _flailBounced ? "BOUNCED" : "FLAILING";

		// The weight on the end tugs the giant round after it.
		Shove( Dir( _ballYaw, _flailYaw, 0f ), PullPerSpeed * 0.5f * Heft * MathF.Sqrt( G ) * dt );

		if ( (_flailDir > 0f && _flailYaw >= FlailTo) || (_flailDir < 0f && _flailYaw <= FlailFrom) )
		{
			_balls = BallMode.Hang;
			_ballRestUntil = Time.Now + 0.25f;
		}
		return new Angles( 0f, Math.Clamp( _flailYaw / 105f, -1f, 1f ) * BodyTwist * 0.8f, 0f );
	}
}