Part of the Rampage town controller. Spawns the escalating police, army and tank roadblocks as the rampage tier rises, placing props on unused crossroads near the lizard via PickCorner/Arm, and blinks the town's siren lamps on a timer.
using System;
using System.Collections.Generic;
/// <summary>
/// The town's answer as terror rises, tier by tier: sirens go up, the police park their cars
/// across the road, then the army's trucks and sandbags, then tanks. None of it does anything
/// but stand there (the soldiers belong to another module): it is all just more to smash.
/// Roadblocks go on crossroads a little way from the lizard, on the side it is coming from.
/// </summary>
public sealed partial class RampageWorld
{
/// <summary>Roadblocks are parked on a crossroads about this far from the lizard.</summary>
[Property, Group( "Response" )] public float RoadblockDistance { get; set; } = 2600f;
/// <summary>Never more tanks than this arrive for one tier.</summary>
[Property, Group( "Response" )] public int MaxTanksPerTier { get; set; } = 4;
readonly List<Destructible> _sirens = new();
readonly Queue<int> _tiers = new();
readonly HashSet<int> _usedCorners = new();
float _nextFlash;
bool _flashOn;
// The tier goes up in the middle of something breaking: the reply waits for the next frame.
void OnTierUp( int tier ) => _tiers.Enqueue( tier );
void TickResponse( float now )
{
while ( _tiers.Count > 0 ) Respond( _tiers.Dequeue() );
// Every siren lamp and police light bar in town flashes, once there is something to flash about.
if ( Rampage.Tier < 1 || now < _nextFlash ) return;
_nextFlash = now + 0.28f;
_flashOn = !_flashOn;
for ( int i = _sirens.Count - 1; i >= 0; i-- )
{
var siren = _sirens[i];
if ( !siren.IsValid() || !siren.Alive ) { _sirens.RemoveAt( i ); continue; }
siren.Flash( (i & 1) == 0 ? _flashOn : !_flashOn );
}
}
void Respond( int tier )
{
var player = Rampage.Player;
var lizard = player.IsValid() ? player.WorldPosition.WithZ( 0f ) : PlayerSpawn;
switch ( tier )
{
case 1:
// Sirens on the nearest corners, and the first patrol car turns up.
for ( int i = 0; i < 3; i++ )
if ( PickCorner( lizard, 700f + i * 900f, out var corner ) )
Arrive( Props.SirenPole, corner + new Vector3( 250f, 250f, 0f ), 0f );
PoliceBlock( lizard );
TownKit.Play( "siren", lizard, 1f );
break;
case 2:
PoliceBlock( lizard );
PoliceBlock( lizard );
TownKit.Play( "siren", lizard, 1f );
break;
case 3:
ArmyBlock( lizard );
ArmyBlock( lizard );
PoliceBlock( lizard );
TownKit.Play( "tank_arrive", lizard, 1f );
break;
default:
int tanks = Math.Clamp( tier - 2, 1, MaxTanksPerTier );
for ( int i = 0; i < tanks; i++ ) TankPost( lizard );
ArmyBlock( lizard );
TownKit.Play( "tank_arrive", lizard, 1f );
break;
}
}
/// <summary>
/// A crossroads not used yet, as near as can be to the wanted distance from the lizard and
/// never right on top of it or on the manhole.
/// </summary>
bool PickCorner( Vector3 lizard, float distance, out Vector3 corner )
{
corner = default;
for ( int attempt = 0; attempt < 2; attempt++ )
{
int best = -1;
float bestScore = float.MaxValue;
for ( int i = 0; i <= Blocks; i++ )
for ( int j = 0; j <= Blocks; j++ )
{
int id = i * (Blocks + 1) + j;
if ( _usedCorners.Contains( id ) ) continue;
var at = new Vector3( RoadCentre( i ), RoadCentre( j ), 0f );
float away = at.Distance( lizard );
if ( away < 600f || at.Length < 300f ) continue;
float score = MathF.Abs( away - distance );
if ( score < bestScore ) { bestScore = score; best = id; corner = at; }
}
if ( best >= 0 ) { _usedCorners.Add( best ); return true; }
_usedCorners.Clear(); // every corner has had something: start again
}
return false;
}
/// <summary>Where a roadblock on this crossroads goes: on the road that leads toward the lizard, just past the corner.</summary>
static void Arm( Vector3 corner, Vector3 lizard, out Vector3 centre, out Vector3 along, out Vector3 across )
{
var to = lizard - corner;
along = MathF.Abs( to.x ) >= MathF.Abs( to.y ) ? new Vector3( MathF.Sign( to.x ), 0f, 0f ) : new Vector3( 0f, MathF.Sign( to.y ), 0f );
if ( along.LengthSquared < 0.5f ) along = Vector3.Forward;
across = new Vector3( -along.y, along.x, 0f );
centre = corner + along * (RoadWidth * 0.5f + 160f);
}
static float YawOf( Vector3 direction ) => MathF.Atan2( direction.y, direction.x ) * 180f / MathF.PI;
/// <summary>A new prop turns up, in a puff of dust.</summary>
Destructible Arrive( PropKind kind, Vector3 at, float yaw )
{
if ( MathF.Abs( at.x ) > HalfSize - 200f || MathF.Abs( at.y ) > HalfSize - 200f ) return null;
var prop = SpawnProp( kind, at, yaw );
Fx.Dust( at, new Vector3( 70f, 70f, 30f ), 2 );
return prop;
}
/// <summary>Two police cars nose to tail across the road, a row of barricades in front and a siren beside them.</summary>
void PoliceBlock( Vector3 lizard )
{
if ( !PickCorner( lizard, RoadblockDistance, out var corner ) ) return;
Arm( corner, lizard, out var centre, out var along, out var across );
float sideways = YawOf( across );
Arrive( Props.PoliceCar, centre + across * 100f, sideways + Game.Random.Float( -12f, 12f ) );
Arrive( Props.PoliceCar, centre - across * 100f, sideways + 180f + Game.Random.Float( -12f, 12f ) );
for ( int k = -2; k <= 2; k++ ) Arrive( Props.Barricade, centre + along * 150f + across * (k * 120f), sideways );
Arrive( Props.SirenPole, centre + across * 270f, 0f );
_barricades.Add( centre );
}
/// <summary>An army truck across the road behind a wall of sandbags.</summary>
void ArmyBlock( Vector3 lizard )
{
if ( !PickCorner( lizard, RoadblockDistance + 500f, out var corner ) ) return;
Arm( corner, lizard, out var centre, out var along, out var across );
float sideways = YawOf( across );
Arrive( Props.ArmyTruck, centre, sideways );
foreach ( float offset in new[] { -210f, -70f, 70f, 210f } )
Arrive( Props.Sandbags, centre + along * 170f + across * offset, sideways );
Arrive( Props.Barricade, centre + along * 260f + across * 60f, sideways );
Arrive( Props.Barricade, centre + along * 260f - across * 60f, sideways );
_barricades.Add( centre );
}
/// <summary>A tank parked in the road, pointing at where the lizard was, with sandbags either side.</summary>
void TankPost( Vector3 lizard )
{
if ( !PickCorner( lizard, RoadblockDistance + 900f, out var corner ) ) return;
Arm( corner, lizard, out var centre, out var along, out var across );
Arrive( Props.Tank, centre, YawOf( along ) );
Arrive( Props.Sandbags, centre + along * 180f + across * 150f, YawOf( across ) );
Arrive( Props.Sandbags, centre + along * 180f - across * 150f, YawOf( across ) );
_barricades.Add( centre );
}
}