diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-07-28 13:37:17 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-07-28 13:37:17 +0100 |
| commit | f137095304f456b06229e4d17ee8249e974fceaf (patch) | |
| tree | 23d4aa9d3c8b5ddfc432bfa68b5e4e761641f6e0 /code/pawn/PlayerSpectator.cs | |
| parent | 8b69e65e51507118b014a0426ae6f957b78a0707 (diff) | |
Add spectator mode
Diffstat (limited to 'code/pawn/PlayerSpectator.cs')
| -rw-r--r-- | code/pawn/PlayerSpectator.cs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/code/pawn/PlayerSpectator.cs b/code/pawn/PlayerSpectator.cs new file mode 100644 index 0000000..c468de0 --- /dev/null +++ b/code/pawn/PlayerSpectator.cs @@ -0,0 +1,48 @@ +using Sandbox;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace MurderGame;
+
+public class PlayerSpectator : EntityComponent<Player>
+{
+ public Player Target { get; set; }
+
+ public void Simulate()
+ {
+ 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 void FrameSimulate( Player player )
+ {
+ if ( Target == null || !Target.IsValid() || Target.LifeState == LifeState.Dead ) return;
+
+ // SimulateRotation(player);
+ Camera.Rotation = Target.EyeRotation;
+ Camera.FieldOfView = Screen.CreateVerticalFieldOfView( Game.Preferences.FieldOfView );
+
+ Camera.FirstPersonViewer = Target;
+ Camera.Position = Target.EyePosition;
+ }
+
+ protected void SimulateRotation(Player player)
+ {
+ player.EyeRotation = Target.ViewAngles.ToRotation();
+ player.Rotation = Target.ViewAngles.WithPitch( 0f ).ToRotation();
+ }
+
+ public List<IClient> GetTargets()
+ {
+ return Game.Clients.Where(c => c.Pawn is Player player && player.CurrentTeam != Team.Spectator && player.LifeState == LifeState.Alive).ToList();
+ }
+}
|
