Partial DongWeapon class for the visual side of the weapon: pooled streak boxes behind fast chain points, crack flash balls, slingshot markers, road scars and craters, plus the first-minute lesson prompts. The pool is created lazily under a NotSaved root object and bits are moved, scaled and faded each frame.
using System;
// What the weapon shows of itself: streaks behind a fast chain, the flash of a crack, the line
// of markers for the slingshot, the scars and craters it leaves in the road, and the lessons of
// the first minute. All of it is a few pooled boxes and balls, made once and moved about.
// See DongWeapon.cs for the whole picture.
public sealed partial class DongWeapon
{
/// <summary>A part of the chain going faster than this (inches a second at the lizard's ordinary size) leaves a streak behind it.</summary>
[Property, Group( "Show" )] public float StreakSpeed { get; set; } = 250f;
/// <summary>Seconds a streak lasts, and how much of the last moment's travel it covers.</summary>
[Property, Group( "Show" )] public float StreakLife { get; set; } = 0.14f;
[Property, Group( "Show" )] public float StreakTrail { get; set; } = 0.05f;
[Property, Group( "Show" )] public Color StreakColour { get; set; } = new( 1f, 0.96f, 0.85f, 0.45f );
[Property, Group( "Show" )] public Color MarkerColour { get; set; } = new( 1f, 0.85f, 0.2f, 0.8f );
[Property, Group( "Show" )] public Color ScarColour { get; set; } = new( 0.1f, 0.09f, 0.08f );
/// <summary>Seconds each first-minute lesson stays up if it is not done.</summary>
[Property, Group( "Show" )] public float LessonSeconds { get; set; } = 10f;
[Property, Group( "Show" )] public bool Lessons { get; set; } = true;
const int Streaks = 18, Markers = 9, Scars = 56, Flashes = 3;
sealed class Bit
{
public GameObject Object;
public ModelRenderer Renderer;
public float Born = -100f;
public Vector3 Size;
}
GameObject _showRoot;
Bit[] _streaks, _markers, _scars, _flashes;
int _nextStreak, _nextScar, _nextFlash;
bool _markersShown;
// The lessons.
int _lesson;
float _lessonT, _lessonGap;
bool _lessonDone;
/// <summary>One scheme at a time, each with the event that shows it has been done.</summary>
static readonly (string Text, string Done)[] LessonList =
{
("HOLD LEFT MOUSE and move the mouse: it follows your hand like a rope. FLICK it to make it crack.", "crack"),
("TAP LEFT, three times: whip, whip, overhead slam.", "slam"),
("TAP RIGHT: the balls, once round. Mind the houses.", "flail"),
("HOLD RIGHT to draw them back. Aim with the camera. LET GO.", "sling"),
("HOLD LEFT + SHIFT (or circle the mouse) to wind up the helicopter. LET GO to spend it.", "spin_release"),
("HOLD BOTH to charge. Let go for the MEGA-SWING. Do not miss.", "mega"),
("Two taps of LEFT, then RIGHT CLICK as the second lands: THE CLAP.", "clap"),
};
// ---- The pool ----------------------------------------------------------------------------------
/// <summary>The bits are made the first time they are wanted, and again if a hotload has lost them.</summary>
bool ShowReady()
{
if ( _showRoot.IsValid() && _streaks is not null && _markers is not null && _scars is not null && _flashes is not null ) return true;
if ( Scene is null ) return false;
ClearShow();
_showRoot = new GameObject( true, "Dong Weapon Show" );
_showRoot.Flags |= GameObjectFlags.NotSaved;
_streaks = MakeBits( Streaks, "Streak", false, StreakColour );
_markers = MakeBits( Markers, "Sling Marker", true, MarkerColour );
_scars = MakeBits( Scars, "Scar", false, ScarColour );
_flashes = MakeBits( Flashes, "Crack Flash", true, Color.White );
return true;
}
Bit[] MakeBits( int count, string name, bool ball, Color tint )
{
var bits = new Bit[count];
for ( int i = 0; i < count; i++ )
{
var go = PopulaceFx.Block( _showRoot, name, Vector3.Zero, new Vector3( 10f ), tint, ball );
go.Enabled = false;
bits[i] = new Bit { Object = go, Renderer = go.Components.Get<ModelRenderer>() };
}
return bits;
}
void ClearShow()
{
if ( _showRoot.IsValid() ) _showRoot.Destroy();
_showRoot = null;
_streaks = _markers = _scars = _flashes = null;
_markersShown = false;
}
/// <summary>Puts one bit somewhere: a box (50 across) or a ball (64 across) stretched to a size in inches, lying along a direction.</summary>
static void Place( Bit bit, Vector3 at, Rotation turn, Vector3 size, bool ball )
{
if ( bit is null || !bit.Object.IsValid() ) return;
bit.Object.Enabled = true;
bit.Object.WorldPosition = at;
bit.Object.WorldRotation = turn;
bit.Object.WorldScale = size / (ball ? 64f : 50f);
bit.Size = size;
bit.Born = Time.Now;
}
// ---- Every frame -------------------------------------------------------------------------------
void Show( float dt )
{
WatchTip();
if ( !ShowReady() ) return;
float now = Time.Now;
// Streaks: the fastest few points of the chain (and the balls) each leave a stretched,
// see-through after-image along the way they have just come. It thins to nothing.
var points = _rig.DongPoints;
var speeds = _rig.DongVelocities;
int count = Math.Min( points.Count, speeds.Count );
float fast = StreakSpeed * G;
if ( Live && count >= 2 )
{
for ( int k = count - 1; k >= 1 && k >= count - 5; k -= 2 )
{
var velocity = speeds[k];
float speed = velocity.Length;
if ( speed < fast ) continue;
Streak( points[k], velocity, _rig.DongRadiusAt( k ) * 1.5f );
}
var sack = _rig.SackVelocity;
if ( sack.Length >= fast ) Streak( _rig.SackPoint, sack, _rig.SackRadius * 1.6f );
}
for ( int i = 0; i < _streaks.Length; i++ )
{
var bit = _streaks[i];
if ( bit is null || !bit.Object.IsValid() || !bit.Object.Enabled ) continue;
float age = (now - bit.Born) / MathF.Max( StreakLife, 0.01f );
if ( age >= 1f || age < 0f ) { bit.Object.Enabled = false; continue; }
bit.Object.WorldScale = new Vector3( bit.Size.x, bit.Size.y * (1f - age), bit.Size.z * (1f - age) ) / 50f;
if ( bit.Renderer.IsValid() ) bit.Renderer.Tint = StreakColour.WithAlpha( StreakColour.a * (1f - age) );
}
// The flash of a crack: a white ball that swells and is gone.
for ( int i = 0; i < _flashes.Length; i++ )
{
var bit = _flashes[i];
if ( bit is null || !bit.Object.IsValid() || !bit.Object.Enabled ) continue;
float age = (now - bit.Born) / 0.12f;
if ( age >= 1f || age < 0f ) { bit.Object.Enabled = false; continue; }
bit.Object.WorldScale = bit.Size * (0.4f + 1.2f * age) / 64f;
if ( bit.Renderer.IsValid() ) bit.Renderer.Tint = Color.White.WithAlpha( 1f - age );
}
ShowMarkers();
}
void Streak( Vector3 at, Vector3 velocity, float thickness )
{
float length = velocity.Length * StreakTrail;
if ( length < 1f ) return;
var bit = _streaks[_nextStreak++ % _streaks.Length];
Place( bit, at - velocity * (StreakTrail * 0.5f), Rotation.LookAt( velocity.Normal ), new Vector3( length, thickness, thickness ), false );
}
/// <summary>The crack, where it happened.</summary>
void CrackFlash( Vector3 at )
{
if ( !ShowReady() ) return;
float size = Reach * 0.3f;
Place( _flashes[_nextFlash++ % _flashes.Length], at, Rotation.Identity, new Vector3( size ), true );
}
/// <summary>
/// While the slingshot is drawn: a line of markers from the hips out along the line of sight
/// to where the shot will end, the last one big. They follow the camera and the draw.
/// </summary>
void ShowMarkers()
{
bool drawing = _joint == Joint.None && _balls == BallMode.Draw && Draw01 > 0f;
if ( !drawing )
{
if ( !_markersShown ) return;
_markersShown = false;
for ( int i = 0; i < _markers.Length; i++ )
if ( _markers[i] is not null && _markers[i].Object.IsValid() ) _markers[i].Object.Enabled = false;
return;
}
_markersShown = true;
float yaw = _controller.Look.yaw;
var forward = Dir( yaw, 0f, 0f );
float ground = _controller.WorldPosition.z;
var from = _rig.DongRoot;
float range = SlingRangeAt( Draw01 );
float ball = _rig.SackRadius;
int last = _markers.Length - 1;
for ( int i = 0; i <= last; i++ )
{
float t = (i + 1f) / (last + 1f);
var at = from + forward * (range * t);
at = at.WithZ( Lerp( from.z, ground + ball * 0.6f, t ) );
float size = i == last ? ball * 2.6f : ball * (0.35f + 0.25f * t);
Place( _markers[i], at, Rotation.Identity, i == last ? new Vector3( size, size, size * 0.15f ) : new Vector3( size ), true );
if ( _markers[i].Renderer.IsValid() )
_markers[i].Renderer.Tint = Draw01 >= 1f && ((int)(Time.Now * 10f) & 1) == 0 ? Color.White.WithAlpha( MarkerColour.a ) : MarkerColour;
}
}
/// <summary>A dark scar on the road: so long, so wide, lying along a compass direction. The oldest is used again.</summary>
void Scar( Vector3 at, float yaw, float length, float width )
{
if ( !ShowReady() || length < 1f || width < 1f ) return;
var bit = _scars[_nextScar++ % _scars.Length];
Place( bit, at.WithZ( RampageWorld.GroundHeightAt( at ) + 2f ), Rotation.FromYaw( yaw ), new Vector3( length, width, 1.5f ), false );
if ( bit.Renderer.IsValid() ) bit.Renderer.Tint = ScarColour;
}
/// <summary>A crater: a round scar, with slabs of road thrown up round it.</summary>
void Crater( Vector3 at, float radius )
{
Scar( at, Game.Random.Float( 0f, 90f ), radius * 1.5f, radius * 1.5f );
Scar( at, Game.Random.Float( 0f, 180f ), radius * 2.4f, radius * 0.12f );
Scar( at, Game.Random.Float( 0f, 180f ), radius * 2.1f, radius * 0.1f );
var world = RampageWorld.Current;
if ( !world.IsValid() ) return;
world.Fx?.Rubble( at + Vector3.Up * 10f, new Vector3( radius * 0.6f, radius * 0.6f, 6f ), Vector3.Up * 700f, 6, MathF.Min( radius * 0.12f, 70f ), Tone.Asphalt, Tone.Pavement );
}
/// <summary>Dust, from the town's own pool.</summary>
static void Puff( Vector3 at, int count, float spread )
{
var world = RampageWorld.Current;
if ( !world.IsValid() ) return;
world.Fx?.Dust( at, new Vector3( spread, spread, MathF.Max( spread * 0.2f, 6f ) ), count );
}
// ---- The first minute --------------------------------------------------------------------------
/// <summary>
/// One way of throwing it about at a time, as a prompt, each staying up until it has been done
/// (or its time is up). Nothing is locked: they are only said in order.
/// </summary>
void Teach( float dt )
{
if ( !Lessons || !Rampage.Started || _lesson >= LessonList.Length ) { Lesson = ""; return; }
// A breath between one and the next.
if ( _lessonGap > 0f )
{
_lessonGap -= dt;
Lesson = "";
return;
}
_lessonT += dt;
Lesson = LessonList[_lesson].Text;
if ( string.IsNullOrEmpty( Rampage.Prompt ) ) Rampage.Prompt = Lesson;
if ( !_lessonDone && _lessonT < LessonSeconds ) return;
_lesson++;
_lessonT = 0f;
_lessonDone = false;
_lessonGap = 1.2f;
}
/// <summary>Something has been done: if it is what the lesson showing was about, that lesson is over.</summary>
void Learned( string what )
{
if ( _lesson < LessonList.Length && _lessonGap <= 0f && LessonList[_lesson].Done == what ) _lessonDone = true;
}
}