The lizard's bite attack, driven by the left mouse button. Each click plays a wind-up, snap and recovery cycle, and at the snap frame it sphere-traces along the camera aim line to find a Health component, damages it, recoils the ReptileController, plays hit or miss sounds, shakes the camera and briefly slow-motions the scene on a kill.
using System;
/// <summary>
/// The lizard's bite, on the left mouse button. Every click is a full lunge-bite: a short wind-up,
/// the head and claws snapping forward, then a quick recovery. Whatever has a <see cref="Health"/>
/// in front of it at the moment of the snap is hurt.
/// </summary>
[Title( "Lizard Attack" ), Category( "Combat" ), Icon( "pets" )]
public sealed class LizardAttack : Component
{
/// <summary>How far in front of the lizard a bite reaches, in inches.</summary>
[Property] public float MeleeRange { get; set; } = 170f;
[Property] public float Damage { get; set; } = 40f;
/// <summary>Seconds between bites.</summary>
[Property] public float Cooldown { get; set; } = 0.45f;
/// <summary>Thickness of the bite's sweep, in inches.</summary>
[Property] public float HitRadius { get; set; } = 14f;
/// <summary>Seconds a whole bite takes, wind-up to recovered.</summary>
[Property] public float SwingSeconds { get; set; } = 0.34f;
/// <summary>How hard a bite throws the lizard forward, in inches per second.</summary>
[Property] public float LungeSpeed { get; set; } = 320f;
[Property] public string HitSound { get; set; } = "sounds/opening/latch_thud.sound";
/// <summary>Sound of a bite through empty air.</summary>
[Property] public string MissSound { get; set; } = "sounds/opening/claw_step.sound";
/// <summary>Real seconds of slow motion after a kill.</summary>
[Property] public float KillSlowSeconds { get; set; } = 0.55f;
// When in the swing the jaws close, as a fraction of the whole bite.
const float SnapAt = 0.32f;
/// <summary>0 when idle, otherwise how far through the current bite it is (0..1).</summary>
public float Phase { get; private set; }
/// <summary>
/// The shape of the bite for animation: dips slightly negative in the wind-up, whips to 1 at
/// the snap, then eases back to 0.
/// </summary>
public float Strike
{
get
{
if ( Phase <= 0f ) return 0f;
if ( Phase < 0.14f ) return -0.3f * (Phase / 0.14f);
if ( Phase < SnapAt )
{
float t = (Phase - 0.14f) / (SnapAt - 0.14f);
return -0.3f + 1.3f * t * t;
}
float back = (Phase - SnapAt) / (1f - SnapAt);
return (1f - back) * (1f - back);
}
}
TimeSince _sinceAttack = 10f;
bool _swinging, _snapped;
float _slowUntil;
Vector3 _aim;
protected override void OnUpdate()
{
EndSlowMotion( false );
var controller = Components.Get<ReptileController>();
bool canClick = !controller.IsValid() || (controller.MouseCaptured && controller.SinceRecapture > 0.2f && !controller.InputLocked && !controller.IsPinned);
if ( canClick && Input.Pressed( "Attack1" ) && _sinceAttack >= Cooldown && Scene.Camera.IsValid() )
{
_sinceAttack = 0f;
_swinging = true;
_snapped = false;
// Bite where the camera looks, flattened so the lunge stays on the surface.
_aim = Scene.Camera.WorldRotation.Forward;
controller?.Lunge( _aim, LungeSpeed * 0.6f );
}
if ( !_swinging ) { Phase = 0f; return; }
Phase = Math.Clamp( _sinceAttack / MathF.Max( SwingSeconds, 0.01f ), 0f, 1f );
if ( !_snapped && Phase >= SnapAt ) { _snapped = true; Snap( controller ); }
if ( Phase >= 1f ) { _swinging = false; Phase = 0f; }
}
/// <summary>The jaws close: find what is in front and hurt it.</summary>
void Snap( ReptileController controller )
{
var cam = Scene.Camera;
if ( !cam.IsValid() ) return;
// The camera trails well behind the lizard. Start the sweep on the aim line where it
// passes the lizard, so nothing between the camera and the lizard gets in the way.
var forward = cam.WorldRotation.Forward;
float along = MathF.Max( Vector3.Dot( WorldPosition - cam.WorldPosition, forward ), 0f );
var from = cam.WorldPosition + forward * along;
var tr = Scene.Trace.Sphere( HitRadius, from, from + forward * MeleeRange )
.IgnoreGameObjectHierarchy( GameObject )
.Run();
var health = tr.Hit && tr.GameObject.IsValid() ? tr.GameObject.Components.Get<Health>( FindMode.EverythingInSelfAndAncestors ) : null;
if ( health is null || health.IsDead || tr.HitPosition.Distance( WorldPosition ) > MeleeRange )
{
// Teeth on nothing.
var miss = StorySound.Play( MissSound, WorldPosition );
if ( miss is not null ) { miss.Volume = 0.7f; miss.Pitch = 0.6f; }
return;
}
health.Damage( Damage, GameObject );
// Contact: thump, and punch the camera into it.
var toward = (tr.HitPosition - WorldPosition).Normal;
controller?.Lunge( toward, LungeSpeed * 0.5f );
controller?.Kick( toward * 14f );
StoryDirector.Get( Scene )?.Shake( 2.5f, 0.18f );
var thud = StorySound.Play( HitSound, tr.HitPosition );
if ( thud is not null ) { thud.Volume = 1.6f; thud.Pitch = health.IsDead ? 0.55f : 0.8f; }
// A kill hangs in the air for a moment.
if ( health.IsDead )
{
StoryDirector.Get( Scene )?.Shake( 5f, 0.35f );
// Only from full speed: never on top of somebody else's slow motion or freeze.
if ( MathF.Abs( Scene.TimeScale - 1f ) < 0.001f )
{
_slowUntil = RealTime.Now + KillSlowSeconds;
Scene.TimeScale = KillTimeScale;
}
}
}
const float KillTimeScale = 0.25f;
/// <summary>
/// Ends the kill's slow motion when its (real) time is up. Called from the frame update, which
/// runs however slow the clock is. If something else has changed the clock since (a cutscene,
/// a memory freezing), it is theirs now and is left alone.
/// </summary>
void EndSlowMotion( bool now )
{
if ( _slowUntil <= 0f || (!now && RealTime.Now < _slowUntil) ) return;
_slowUntil = 0f;
if ( Scene is not null && MathF.Abs( Scene.TimeScale - KillTimeScale ) < 0.001f ) Scene.TimeScale = 1f;
}
protected override void OnDisabled()
{
EndSlowMotion( true );
_swinging = false;
Phase = 0f;
}
}