A PlayerController MoveMode for a roleplay framework that handles noclip and frozen states. It scores itself highest when RpPlayer.MoveType is Noclip or None, disables body motion, gravity and colliders while active, and moves the owner's body directly at NoclipSpeed or NoclipFastSpeed along the eye rotation, with Jump and Duck adding vertical input.
using System.Linq;
using Sandbox.Movement;
namespace RoleplayFramework.Players;
/// <summary>
/// Extra movement for Lua's <c>ply:SetMoveType</c>: noclip (fly through walls, Shift faster, Space up, Ctrl down) and
/// frozen (can look around but not move). The host sets <see cref="RpPlayer.MoveType"/>; the owning client, which
/// simulates its own movement, picks this mode up through the PlayerController's mode scoring.
/// While active, the body is kinematic (no collisions, no gravity) and is moved directly.
/// </summary>
public sealed class RpMoveMode : MoveMode
{
public const int MoveTypeNone = 0, MoveTypeWalk = 2, MoveTypeNoclip = 8;
[Property] public float NoclipSpeed { get; set; } = 500f;
[Property] public float NoclipFastSpeed { get; set; } = 1400f;
RpPlayer _rp;
RpPlayer Rp => _rp ??= Components.Get<RpPlayer>( FindMode.EverythingInSelfAndAncestors );
int Type => Rp?.MoveType ?? MoveTypeWalk;
public override int Score( PlayerController controller ) => Type is MoveTypeNoclip or MoveTypeNone ? 100000 : -1000;
public override void OnModeBegin()
{
SetSolid( false );
}
public override void OnModeEnd( MoveMode next )
{
SetSolid( true );
}
void SetSolid( bool solid )
{
var body = Controller?.Body;
if ( body is not null )
{
body.MotionEnabled = solid;
body.Gravity = solid;
body.Velocity = Vector3.Zero;
}
// Noclip passes through everything; frozen players stay solid for others.
foreach ( var c in GameObject.Components.GetAll<Collider>( FindMode.EverythingInSelfAndDescendants ) )
c.Enabled = solid || Type != MoveTypeNoclip;
}
public override Vector3 UpdateMove( Rotation eyes, Vector3 input )
{
if ( Type != MoveTypeNoclip ) return Vector3.Zero;
var dir = eyes * input;
if ( Input.Down( "Jump" ) ) dir += Vector3.Up;
if ( Input.Down( "Duck" ) ) dir += Vector3.Down;
if ( dir.Length > 1 ) dir = dir.Normal;
return dir * (Input.Down( "Run" ) ? NoclipFastSpeed : NoclipSpeed);
}
public override void PrePhysicsStep()
{
if ( IsProxy || Controller is null ) return;
if ( Type == MoveTypeNoclip )
{
var wish = UpdateMove( Controller.EyeAngles.ToRotation(), Input.AnalogMove );
WorldPosition += wish * Time.Delta;
}
if ( Controller.Body is not null ) Controller.Body.Velocity = Vector3.Zero;
}
}