From 914512435d37d9f1e1ea4c045afd4ec5612b7534 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Sun, 30 Jul 2023 02:45:36 +0100 Subject: Replace movement controllers --- code/pawn/component/camera/BaseCameraComponent.cs | 20 +++++++++ .../pawn/component/camera/PlayerCameraComponent.cs | 48 ++++++++++++++++++++++ .../component/camera/SpectatorCameraComponent.cs | 41 ++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 code/pawn/component/camera/BaseCameraComponent.cs create mode 100644 code/pawn/component/camera/PlayerCameraComponent.cs create mode 100644 code/pawn/component/camera/SpectatorCameraComponent.cs (limited to 'code/pawn/component/camera') diff --git a/code/pawn/component/camera/BaseCameraComponent.cs b/code/pawn/component/camera/BaseCameraComponent.cs new file mode 100644 index 0000000..be310c8 --- /dev/null +++ b/code/pawn/component/camera/BaseCameraComponent.cs @@ -0,0 +1,20 @@ +using Sandbox; + +namespace MurderGame; + +public class BaseCameraComponent : EntityComponent, ISingletonComponent +{ + + public virtual void Simulate( IClient cl ) + { + + } + public virtual void FrameSimulate( IClient cl ) + { + + } + public virtual void BuildInput() + { + + } +} diff --git a/code/pawn/component/camera/PlayerCameraComponent.cs b/code/pawn/component/camera/PlayerCameraComponent.cs new file mode 100644 index 0000000..5011c67 --- /dev/null +++ b/code/pawn/component/camera/PlayerCameraComponent.cs @@ -0,0 +1,48 @@ +using Sandbox; + +namespace MurderGame; + +public class PlayerCameraComponent : BaseCameraComponent +{ + protected override void OnActivate() + { + base.OnActivate(); + // Set field of view to whatever the user chose in options + Camera.FieldOfView = Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView ); + } + public override void FrameSimulate( IClient cl ) + { + + var pl = Entity as Player; + // Update rotation every frame, to keep things smooth + + if (pl.PlayerRagdoll != null && pl.LifeState == LifeState.Dead) + { + Camera.Position = pl.PlayerRagdoll.Position; + Camera.FirstPersonViewer = pl.PlayerRagdoll; + return; + } + + pl.EyeRotation = pl.ViewAngles.ToRotation(); + + Camera.Position = pl.EyePosition; + Camera.Rotation = pl.ViewAngles.ToRotation(); + + Camera.Main.SetViewModelCamera( Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView ) ); + + // Set the first person viewer to this, so it won't render our model + Camera.FirstPersonViewer = Entity; + + Camera.ZNear = 8 * pl.Scale; + } + public override void BuildInput() + { + if ( Game.LocalClient.Components.TryGet( out var _ ) ) + return; + + var pl = Entity as Player; + var viewAngles = (pl.ViewAngles + Input.AnalogLook).Normal; + pl.ViewAngles = viewAngles.WithPitch( viewAngles.pitch.Clamp( -89f, 89f ) ); + return; + } +} diff --git a/code/pawn/component/camera/SpectatorCameraComponent.cs b/code/pawn/component/camera/SpectatorCameraComponent.cs new file mode 100644 index 0000000..f81e186 --- /dev/null +++ b/code/pawn/component/camera/SpectatorCameraComponent.cs @@ -0,0 +1,41 @@ +using Sandbox; +using System.Collections.Generic; +using System.Linq; + +namespace MurderGame; + +public class SpectatorCameraComponent : BaseCameraComponent +{ + public Player Target { get; set; } + + public override void Simulate( IClient cl ) + { + if (Target == null || !Target.IsValid() || Target.LifeState == LifeState.Dead) + { + var targets = GetTargets(); + if ( targets.Count == 0 ) + { + Target = null; + return; + } + var nextTarget = targets.First(); + Target = (Player)nextTarget.Pawn; + } + } + + public override void FrameSimulate( IClient cl ) + { + if ( Target == null || !Target.IsValid() || Target.LifeState == LifeState.Dead ) return; + + Camera.Rotation = Target.EyeRotation; + Camera.FieldOfView = Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView ); + + Camera.FirstPersonViewer = Target; + Camera.Position = Target.EyePosition; + } + + private List GetTargets() + { + return Game.Clients.Where(c => c.Pawn is Player player && player.CurrentTeam != Team.Spectator && player.LifeState == LifeState.Alive).ToList(); + } +} -- cgit v1.2.3-70-g09d2