diff --git a/Content.Server/_Everwood/Immersive/ImmersiveComponent.cs b/Content.Server/_Everwood/Immersive/ImmersiveComponent.cs new file mode 100644 index 0000000000..7f4808297b --- /dev/null +++ b/Content.Server/_Everwood/Immersive/ImmersiveComponent.cs @@ -0,0 +1,7 @@ +namespace Content.Server.Everwood.Immersive; + +[RegisterComponent] +public sealed partial class ImmersiveComponent : Component +{ + +} diff --git a/Content.Server/_Everwood/Immersive/ImmersiveSystem.cs b/Content.Server/_Everwood/Immersive/ImmersiveSystem.cs new file mode 100644 index 0000000000..7b429fee6c --- /dev/null +++ b/Content.Server/_Everwood/Immersive/ImmersiveSystem.cs @@ -0,0 +1,96 @@ +using Content.Server.Administration; +using Content.Server.Administration.Logs; +using Content.Shared.Telescope; +using Content.Shared.Administration; +using Content.Shared.Everwood.CCVar; +using Content.Shared.Database; +using Content.Shared.Humanoid; +using Content.Shared.Movement.Components; +using Robust.Shared.Configuration; +using Robust.Shared.Console; +using Robust.Shared.GameObjects; + +namespace Content.Server.Everwood.Immersive; + +public sealed class ImmersiveSystem : EntitySystem +{ + [Dependency] private readonly SharedTelescopeSystem _telescope = default!; + [Dependency] private readonly IConfigurationManager _configurationManager = default!; + [Dependency] private readonly IConsoleHost _console = default!; + [Dependency] private readonly IAdminLogManager _adminLog = default!; + + private bool _immersiveEnabled; + private const float TelescopeDivisor = 0.8f; // 2 tiles further than normal + private const float TelescopeLerpAmount = 0.1f; // Looks nice. + + private EntityQuery _eyeQuery; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnPlayerSpawn); + + Subs.CVar(_configurationManager, EverwoodCCVars.ImmersiveEnabled, OnValueChanged, true); + + _console.RegisterCommand("setImmersive", SetImmersiveCommand); + _eyeQuery = GetEntityQuery(); + } + + private void OnValueChanged(bool value) + { + _immersiveEnabled = value; + if (value) + { + OnStarted(); + } + else + { + Ended(); + } + } + + [AdminCommand(AdminFlags.Fun)] + private void SetImmersiveCommand(IConsoleShell shell, string str, string[] args) + { + _configurationManager.SetCVar(EverwoodCCVars.ImmersiveEnabled, !_immersiveEnabled); + shell.WriteLine($"Immersive set in {_immersiveEnabled}"); + _adminLog.Add(LogType.AdminMessage, + LogImpact.Extreme, + $"Admin {(shell.Player != null ? shell.Player.Name : "An administrator")} immersive set in {_immersiveEnabled}"); + } + + private void OnStarted() + { + var humans = EntityQueryEnumerator(); + + while (humans.MoveNext(out var entity, out _, out _)) + { + AddTelescope(entity, TelescopeDivisor, TelescopeLerpAmount); + } + } + + private void Ended() + { + var humans = EntityQueryEnumerator(); + while (humans.MoveNext(out var entity, out _, out _, out _)) + { + RemCompDeferred(entity); + } + } + + private void AddTelescope(EntityUid human, float divisor, float lerpAmount) + { + _telescope.SetParameters((human, EnsureComp(human)), divisor, lerpAmount); + } + + private void OnPlayerSpawn(Entity ent, ref MapInitEvent ev) + { + if (!_immersiveEnabled) + return; + + if (!_eyeQuery.HasComp(ent)) + return; + + AddTelescope(ent, TelescopeDivisor, TelescopeLerpAmount); + } +} diff --git a/Content.Shared/Telescope/SharedTelescopeSystem.cs b/Content.Shared/Telescope/SharedTelescopeSystem.cs index 5f9896cc35..8d73b9b2fc 100644 --- a/Content.Shared/Telescope/SharedTelescopeSystem.cs +++ b/Content.Shared/Telescope/SharedTelescopeSystem.cs @@ -23,8 +23,7 @@ public override void Initialize() private void OnShutdown(Entity ent, ref ComponentShutdown args) { - if (!TryComp(ent.Comp.LastEntity, out EyeComponent? eye) - || ent.Comp.LastEntity == ent && TerminatingOrDeleted(ent)) + if (!TryComp(ent.Comp.LastEntity, out EyeComponent? eye) || ent.Comp.LastEntity == ent && TerminatingOrDeleted(ent)) return; SetOffset((ent.Comp.LastEntity.Value, eye), Vector2.Zero, ent); @@ -40,8 +39,10 @@ private void OnHandDeselected(Entity ent, ref HandDeselected private void OnUnequip(Entity ent, ref GotUnequippedHandEvent args) { - if (!TryComp(args.User, out EyeComponent? eye) - || !HasComp(ent.Owner)) + if (!TryComp(args.User, out EyeComponent? eye)) + return; + + if (!HasComp(ent.Owner)) return; SetOffset((args.User, eye), Vector2.Zero, ent); @@ -51,20 +52,26 @@ private void OnUnequip(Entity ent, ref GotUnequippedHandEven { TelescopeComponent? telescope = null; - if (TryComp(ent, out var hands) - && hands.ActiveHandEntity.HasValue - && TryComp(hands.ActiveHandEntity, out var handTelescope)) + if (TryComp(ent, out var hands) && + hands.ActiveHandEntity.HasValue && + TryComp(hands.ActiveHandEntity, out var handTelescope)) + { telescope = handTelescope; + } else if (TryComp(ent, out var entityTelescope)) + { telescope = entityTelescope; + } return telescope; } private void OnEyeOffsetChanged(EyeOffsetChangedEvent msg, EntitySessionEventArgs args) { - if (args.SenderSession.AttachedEntity is not { } ent - || !TryComp(ent, out var eye)) + if (args.SenderSession.AttachedEntity is not { } ent) + return; + + if (!TryComp(ent, out EyeComponent? eye)) return; var telescope = GetRightTelescope(ent); @@ -72,19 +79,23 @@ private void OnEyeOffsetChanged(EyeOffsetChangedEvent msg, EntitySessionEventArg if (telescope == null) return; - var offset = Vector2.Lerp(eye.Offset, msg.Offset, telescope.LerpAmount); - - SetOffset((ent, eye), offset, telescope); + SetOffset((ent, eye), msg.Offset, telescope); } - private void SetOffset(Entity ent, Vector2 offset, TelescopeComponent telescope) + private void SetOffset(Entity ent, Vector2 msgOffset, TelescopeComponent telescope) { telescope.LastEntity = ent; + var offset = Vector2.Lerp(ent.Comp.Offset, msgOffset, telescope.LerpAmount); if (TryComp(ent, out CameraRecoilComponent? recoil)) { recoil.BaseOffset = offset; - _eye.SetOffset(ent, offset + recoil.CurrentKick, ent); + if (recoil.CurrentKick != Vector2.Zero) + { + _eye.SetOffset(ent, msgOffset + recoil.CurrentKick, ent); + return; + } + _eye.SetOffset(ent, offset, ent); } else { @@ -92,8 +103,11 @@ private void SetOffset(Entity ent, Vector2 offset, TelescopeCompon } } - public void SetParameters(Entity ent, float? divisor = null, float? lerpAmount = null) + public void SetParameters(Entity ent, float? divisor = null, float? lerpAmount = null) { + if(!Resolve(ent, ref ent.Comp)) + return; + var telescope = ent.Comp; telescope.Divisor = divisor ?? telescope.Divisor; diff --git a/Content.Shared/Telescope/TelescopeComponent.cs b/Content.Shared/Telescope/TelescopeComponent.cs index 529cc58324..7c720cd0bf 100644 --- a/Content.Shared/Telescope/TelescopeComponent.cs +++ b/Content.Shared/Telescope/TelescopeComponent.cs @@ -9,7 +9,7 @@ public sealed partial class TelescopeComponent : Component public float Divisor = 0.1f; [DataField, AutoNetworkedField] - public float LerpAmount = 0.1f; + public float LerpAmount = 0.05f; [ViewVariables] public EntityUid? LastEntity; diff --git a/Content.Shared/_Everwood/CCVar/CCVars.cs b/Content.Shared/_Everwood/CCVar/CCVars.cs new file mode 100644 index 0000000000..dd2984c534 --- /dev/null +++ b/Content.Shared/_Everwood/CCVar/CCVars.cs @@ -0,0 +1,18 @@ +using Robust.Shared; +using Robust.Shared.Configuration; + +namespace Content.Shared.Everwood.CCVar +{ + [CVarDefs] + public sealed class EverwoodCCVars : CVars + { + + /* + * Immersive + */ + + public static readonly CVarDef ImmersiveEnabled = + CVarDef.Create("immersive.enabled", true, CVar.SERVERONLY); + + } +}