Skip to content

Commit

Permalink
[Port] Immersive (#11)
Browse files Browse the repository at this point in the history
* [Port] Immersive

* up1
  • Loading branch information
PuroSlavKing authored Nov 17, 2024
1 parent 5019f3d commit 597f773
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Content.Server/_Everwood/Immersive/ImmersiveComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Content.Server.Everwood.Immersive;

[RegisterComponent]
public sealed partial class ImmersiveComponent : Component
{

}
96 changes: 96 additions & 0 deletions Content.Server/_Everwood/Immersive/ImmersiveSystem.cs
Original file line number Diff line number Diff line change
@@ -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<ContentEyeComponent> _eyeQuery;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ImmersiveComponent, MapInitEvent>(OnPlayerSpawn);

Subs.CVar(_configurationManager, EverwoodCCVars.ImmersiveEnabled, OnValueChanged, true);

_console.RegisterCommand("setImmersive", SetImmersiveCommand);
_eyeQuery = GetEntityQuery<ContentEyeComponent>();
}

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<HumanoidAppearanceComponent, ContentEyeComponent>();

while (humans.MoveNext(out var entity, out _, out _))
{
AddTelescope(entity, TelescopeDivisor, TelescopeLerpAmount);
}
}

private void Ended()
{
var humans = EntityQueryEnumerator<HumanoidAppearanceComponent, ContentEyeComponent, TelescopeComponent>();
while (humans.MoveNext(out var entity, out _, out _, out _))
{
RemCompDeferred<TelescopeComponent>(entity);
}
}

private void AddTelescope(EntityUid human, float divisor, float lerpAmount)
{
_telescope.SetParameters((human, EnsureComp<TelescopeComponent>(human)), divisor, lerpAmount);
}

private void OnPlayerSpawn(Entity<ImmersiveComponent> ent, ref MapInitEvent ev)
{
if (!_immersiveEnabled)
return;

if (!_eyeQuery.HasComp(ent))
return;

AddTelescope(ent, TelescopeDivisor, TelescopeLerpAmount);
}
}
44 changes: 29 additions & 15 deletions Content.Shared/Telescope/SharedTelescopeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public override void Initialize()

private void OnShutdown(Entity<TelescopeComponent> 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);
Expand All @@ -40,8 +39,10 @@ private void OnHandDeselected(Entity<TelescopeComponent> ent, ref HandDeselected

private void OnUnequip(Entity<TelescopeComponent> ent, ref GotUnequippedHandEvent args)
{
if (!TryComp(args.User, out EyeComponent? eye)
|| !HasComp<ItemComponent>(ent.Owner))
if (!TryComp(args.User, out EyeComponent? eye))
return;

if (!HasComp<ItemComponent>(ent.Owner))
return;

SetOffset((args.User, eye), Vector2.Zero, ent);
Expand All @@ -51,49 +52,62 @@ private void OnUnequip(Entity<TelescopeComponent> ent, ref GotUnequippedHandEven
{
TelescopeComponent? telescope = null;

if (TryComp<HandsComponent>(ent, out var hands)
&& hands.ActiveHandEntity.HasValue
&& TryComp<TelescopeComponent>(hands.ActiveHandEntity, out var handTelescope))
if (TryComp<HandsComponent>(ent, out var hands) &&
hands.ActiveHandEntity.HasValue &&
TryComp<TelescopeComponent>(hands.ActiveHandEntity, out var handTelescope))
{
telescope = handTelescope;
}
else if (TryComp<TelescopeComponent>(ent, out var entityTelescope))
{
telescope = entityTelescope;
}

return telescope;
}

private void OnEyeOffsetChanged(EyeOffsetChangedEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not { } ent
|| !TryComp<EyeComponent>(ent, out var eye))
if (args.SenderSession.AttachedEntity is not { } ent)
return;

if (!TryComp(ent, out EyeComponent? eye))
return;

var telescope = GetRightTelescope(ent);

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<EyeComponent> ent, Vector2 offset, TelescopeComponent telescope)
private void SetOffset(Entity<EyeComponent> 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
{
_eye.SetOffset(ent, offset, ent);
}
}

public void SetParameters(Entity<TelescopeComponent> ent, float? divisor = null, float? lerpAmount = null)
public void SetParameters(Entity<TelescopeComponent?> ent, float? divisor = null, float? lerpAmount = null)
{
if(!Resolve(ent, ref ent.Comp))
return;

var telescope = ent.Comp;

telescope.Divisor = divisor ?? telescope.Divisor;
Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Telescope/TelescopeComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions Content.Shared/_Everwood/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
@@ -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<bool> ImmersiveEnabled =
CVarDef.Create("immersive.enabled", true, CVar.SERVERONLY);

}
}

0 comments on commit 597f773

Please sign in to comment.