diff options
Diffstat (limited to 'code/pawn/component/camera')
| -rw-r--r-- | code/pawn/component/camera/BaseCameraComponent.cs | 20 | ||||
| -rw-r--r-- | code/pawn/component/camera/PlayerCameraComponent.cs | 48 | ||||
| -rw-r--r-- | code/pawn/component/camera/SpectatorCameraComponent.cs | 41 |
3 files changed, 109 insertions, 0 deletions
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<Player>, 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<DevCamera>( 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<IClient> GetTargets()
+ {
+ return Game.Clients.Where(c => c.Pawn is Player player && player.CurrentTeam != Team.Spectator && player.LifeState == LifeState.Alive).ToList();
+ }
+}
|
