A player item component that gives a long-jump ability. It monitors when the local player ducks while moving, times that state, and when the player jumps within 0.5s it applies a strong forward and upward impulse and optionally plays a sound.
public sealed class LongJump : Item, IPlayerEvent
{
[Property] float HorizontalForce { get; set; } = 1000;
[Property] float VerticalForce { get; set; } = 350;
[Property] SoundEvent JumpSound { get; set; }
TimeSince timeSinceDucked { get; set; }
protected override void OnAdded( BaseInventoryComponent inventory )
{
base.OnAdded( inventory );
// The inventory stows what it adds, but this is passive - it has to keep running while
// nothing is holding it.
GameObject.Enabled = true;
GameObject.Network.Refresh();
}
protected override void OnFixedUpdate()
{
if ( !Owner.IsValid() ) return;
if ( !Owner.IsLocalPlayer ) return;
if ( !Owner.Controller.IsValid() || !Owner.Controller.IsOnGround ) return;
if ( Owner.Controller.Velocity.Length > 0 && Input.Pressed( "duck" ) )
{
timeSinceDucked = 0;
}
}
void IPlayerEvent.OnJump()
{
if ( timeSinceDucked < 0.5f )
{
var rot = Owner.EyeTransform.Rotation;
rot = rot.Angles().WithPitch( 0f );
Owner.Controller.Jump( rot.Forward * HorizontalForce + Vector3.Up * VerticalForce );
if ( JumpSound.IsValid() )
{
GameObject.PlaySound( JumpSound );
}
}
}
}