From 1e5c6393a6b29eb00dbb8fb137d86647cb0c356b Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Fri, 28 Jul 2023 22:06:03 +0100 Subject: Add TryUnstuck and death overlay --- code/pawn/Player.Use.cs | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 code/pawn/Player.Use.cs (limited to 'code/pawn/Player.Use.cs') diff --git a/code/pawn/Player.Use.cs b/code/pawn/Player.Use.cs new file mode 100644 index 0000000..3f35052 --- /dev/null +++ b/code/pawn/Player.Use.cs @@ -0,0 +1,117 @@ +using Sandbox; + +namespace MurderGame; + +public partial class Player +{ + public Entity Using { get; protected set; } + + protected virtual void TickPlayerUse() + { + // This is serverside only + if ( !Game.IsServer ) return; + + // Turn prediction off + using ( Prediction.Off() ) + { + if ( Input.Pressed( "use" ) ) + { + Using = FindUsable(); + + if ( Using == null ) + { + UseFail(); + return; + } + } + + if ( !Input.Down( "use" ) ) + { + StopUsing(); + return; + } + + if ( !Using.IsValid() ) + return; + + // If we move too far away or something we should probably ClearUse()? + + // + // If use returns true then we can keep using it + // + if ( Using is IUse use && use.OnUse( this ) ) + return; + + StopUsing(); + } + } + + /// + /// Player tried to use something but there was nothing there. + /// Tradition is to give a disappointed boop. + /// + protected virtual void UseFail() + { + PlaySound( "player_use_fail" ); + } + + /// + /// If we're using an entity, stop using it + /// + protected virtual void StopUsing() + { + Using = null; + } + + /// + /// Returns if the entity is a valid usable entity + /// + protected bool IsValidUseEntity( Entity e ) + { + if ( e == null ) return false; + if ( e is not IUse use ) return false; + if ( !use.IsUsable( this ) ) return false; + + return true; + } + + /// + /// Find a usable entity for this player to use + /// + protected virtual Entity FindUsable() + { + // First try a direct 0 width line + var tr = Trace.Ray( EyePosition, EyePosition + EyeRotation.Forward * 85 ) + .Ignore( this ) + .Run(); + + // See if any of the parent entities are usable if we ain't. + var ent = tr.Entity; + while ( ent.IsValid() && !IsValidUseEntity( ent ) ) + { + ent = ent.Parent; + } + + // Nothing found, try a wider search + if ( !IsValidUseEntity( ent ) ) + { + tr = Trace.Ray( EyePosition, EyePosition + EyeRotation.Forward * 85 ) + .Radius( 2 ) + .Ignore( this ) + .Run(); + + // See if any of the parent entities are usable if we ain't. + ent = tr.Entity; + while ( ent.IsValid() && !IsValidUseEntity( ent ) ) + { + ent = ent.Parent; + } + } + + // Still no good? Bail. + if ( !IsValidUseEntity( ent ) ) return null; + + return ent; + } +} + -- cgit v1.2.3-70-g09d2