Skip to content

Commit

Permalink
[Port] Telescope System / Система Прицеливания (#8)
Browse files Browse the repository at this point in the history
* add: TelescopeSystem

* fix

* add: RU loc
  • Loading branch information
Spatison authored Aug 23, 2024
1 parent 02d3cd1 commit 5fc774e
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 0 deletions.
1 change: 1 addition & 0 deletions Content.Client/Input/ContentContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static void SetupContexts(IInputContextContainer contexts)
human.AddFunction(ContentKeyFunctions.Arcade1);
human.AddFunction(ContentKeyFunctions.Arcade2);
human.AddFunction(ContentKeyFunctions.Arcade3);
human.AddFunction(ContentKeyFunctions.LookUp); // WD EDIT

// actions should be common (for ghosts, mobs, etc)
common.AddFunction(ContentKeyFunctions.OpenActionsMenu);
Expand Down
9 changes: 9 additions & 0 deletions Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Numerics;
using Content.Client.Stylesheets;
using Content.Shared._White;
using Content.Shared.CCVar;
using Content.Shared.Input;
using Robust.Client.AutoGenerated;
Expand Down Expand Up @@ -97,6 +98,12 @@ private void HandleToggleWalk(BaseButton.ButtonToggledEventArgs args)
_deferCommands.Add(_inputManager.SaveToUserData);
}

private void HandleHoldLookUp(BaseButton.ButtonToggledEventArgs args) // WD EDIT
{
_cfg.SetCVar(WhiteCVars.HoldLookUp, args.Pressed);
_cfg.SaveToFile();
}

private void HandleStaticStorageUI(BaseButton.ButtonToggledEventArgs args)
{
_cfg.SetCVar(CCVars.StaticStorageUI, args.Pressed);
Expand Down Expand Up @@ -186,6 +193,8 @@ void AddCheckBox(string checkBoxName, bool currentState, Action<BaseButton.Butto
AddButton(ContentKeyFunctions.OfferItem);
AddButton(ContentKeyFunctions.SaveItemLocation);
AddButton(ContentKeyFunctions.ToggleStanding);
AddButton(ContentKeyFunctions.LookUp); // WD EDIT
AddCheckBox("ui-options-function-hold-look-up", _cfg.GetCVar(WhiteCVars.HoldLookUp), HandleHoldLookUp); // WD EDIT

AddHeader("ui-options-header-interaction-adv");
AddButton(ContentKeyFunctions.SmartEquipBackpack);
Expand Down
126 changes: 126 additions & 0 deletions Content.Client/_White/Telescope/TelescopeSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.Numerics;
using Content.Client.Viewport;
using Content.Shared._White;
using Content.Shared._White.Telescope;
using Content.Shared.Input;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Shared.Configuration;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Timing;

namespace Content.Client._White.Telescope;

public sealed class TelescopeSystem : SharedTelescopeSystem
{
[Dependency] private readonly InputSystem _inputSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IInputManager _input = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;

private ScalingViewport? _viewport;
private bool _holdLookUp;
private bool _toggled;

public override void Initialize()
{
base.Initialize();

_cfg.OnValueChanged(WhiteCVars.HoldLookUp,
val =>
{
var input = val ? null : InputCmdHandler.FromDelegate(_ => _toggled = !_toggled);
_input.SetInputCommand(ContentKeyFunctions.LookUp, input);
_holdLookUp = val;
_toggled = false;
},
true);
}

public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);

if (_timing.ApplyingState || !_timing.IsFirstTimePredicted || !_input.MouseScreenPosition.IsValid)
return;

var player = _player.LocalEntity;

var telescope = GetRightTelescope(player);

if (telescope == null)
{
_toggled = false;
return;
}

if (!TryComp<EyeComponent>(player, out var eye))
return;

var offset = Vector2.Zero;

if (_holdLookUp)
{
if (_inputSystem.CmdStates.GetState(ContentKeyFunctions.LookUp) != BoundKeyState.Down)
{
RaiseEvent(offset);
return;
}
}
else if (!_toggled)
{
RaiseEvent(offset);
return;
}

var mousePos = _input.MouseScreenPosition;

if (_uiManager.MouseGetControl(mousePos) as ScalingViewport is { } viewport)
_viewport = viewport;

if (_viewport == null)
return;

var centerPos = _eyeManager.WorldToScreen(eye.Eye.Position.Position + eye.Offset);

var diff = mousePos.Position - centerPos;
var len = diff.Length();

var size = _viewport.PixelSize;

var maxLength = Math.Min(size.X, size.Y) * 0.4f;
var minLength = maxLength * 0.2f;

if (len > maxLength)
{
diff *= maxLength / len;
len = maxLength;
}

var divisor = maxLength * telescope.Divisor;

if (len > minLength)
{
diff -= diff * minLength / len;
offset = new Vector2(diff.X / divisor, -diff.Y / divisor);
offset = new Angle(-eye.Rotation.Theta).RotateVec(offset);
}

RaiseEvent(offset);
}

private void RaiseEvent(Vector2 offset)
{
RaisePredictiveEvent(new EyeOffsetChangedEvent
{
Offset = offset
});
}
}
5 changes: 5 additions & 0 deletions Content.Server/_White/Telescope/TelescopeSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Content.Shared._White.Telescope;

namespace Content.Server._White.Telescope;

public sealed class TelescopeSystem : SharedTelescopeSystem;
1 change: 1 addition & 0 deletions Content.Shared/Input/ContentKeyFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class ContentKeyFunctions
public static readonly BoundKeyFunction ResetZoom = "ResetZoom";
public static readonly BoundKeyFunction OfferItem = "OfferItem";
public static readonly BoundKeyFunction ToggleStanding = "ToggleStanding";
public static readonly BoundKeyFunction LookUp = "LookUp"; // WD EDIT

public static readonly BoundKeyFunction ArcadeUp = "ArcadeUp";
public static readonly BoundKeyFunction ArcadeDown = "ArcadeDown";
Expand Down
14 changes: 14 additions & 0 deletions Content.Shared/_White/CVars.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Robust.Shared.Configuration;

namespace Content.Shared._White;

[CVarDefs]
public sealed class WhiteCVars
{
#region Keybind

public static readonly CVarDef<bool> HoldLookUp =
CVarDef.Create("white.hold_look_up", false, CVar.CLIENT | CVar.ARCHIVE);

#endregion
}
117 changes: 117 additions & 0 deletions Content.Shared/_White/Telescope/SharedTelescopeSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System.Numerics;
using Content.Shared.Camera;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Item;
using Robust.Shared.Serialization;

namespace Content.Shared._White.Telescope;

public abstract class SharedTelescopeSystem : EntitySystem
{
[Dependency] private readonly SharedEyeSystem _eye = default!;

public override void Initialize()
{
base.Initialize();

SubscribeAllEvent<EyeOffsetChangedEvent>(OnEyeOffsetChanged);
SubscribeLocalEvent<TelescopeComponent, GotUnequippedHandEvent>(OnUnequip);
SubscribeLocalEvent<TelescopeComponent, HandDeselectedEvent>(OnHandDeselected);
SubscribeLocalEvent<TelescopeComponent, ComponentShutdown>(OnShutdown);
}

private void OnShutdown(Entity<TelescopeComponent> ent, ref ComponentShutdown args)
{
if (!TryComp(ent.Comp.LastEntity, out EyeComponent? eye) || ent.Comp.LastEntity == ent && TerminatingOrDeleted(ent))
return;

SetOffset((ent.Comp.LastEntity.Value, eye), Vector2.Zero, ent);
}

private void OnHandDeselected(Entity<TelescopeComponent> ent, ref HandDeselectedEvent args)
{
if (!TryComp(args.User, out EyeComponent? eye))
return;

SetOffset((args.User, eye), Vector2.Zero, ent);
}

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

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

SetOffset((args.User, eye), Vector2.Zero, ent);
}

public TelescopeComponent? GetRightTelescope(EntityUid? ent)
{
TelescopeComponent? telescope = null;

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)
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);
}

private void SetOffset(Entity<EyeComponent> ent, Vector2 offset, TelescopeComponent telescope)
{
telescope.LastEntity = ent;

if (TryComp(ent, out CameraRecoilComponent? recoil))
{
recoil.BaseOffset = offset;
_eye.SetOffset(ent, offset + recoil.CurrentKick, ent);
}
else
{
_eye.SetOffset(ent, offset, ent);
}
}

public void SetParameters(Entity<TelescopeComponent> ent, float? divisor = null, float? lerpAmount = null)
{
var telescope = ent.Comp;

telescope.Divisor = divisor ?? telescope.Divisor;
telescope.LerpAmount = lerpAmount ?? telescope.LerpAmount;

Dirty(ent.Owner, telescope);
}
}

[Serializable, NetSerializable]
public sealed class EyeOffsetChangedEvent : EntityEventArgs
{
public Vector2 Offset;
}
16 changes: 16 additions & 0 deletions Content.Shared/_White/Telescope/TelescopeComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Robust.Shared.GameStates;

namespace Content.Shared._White.Telescope;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class TelescopeComponent : Component
{
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
public float Divisor = 0.1f;

[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
public float LerpAmount = 0.1f;

[ViewVariables]
public EntityUid? LastEntity;
}
2 changes: 2 additions & 0 deletions Resources/Locale/en-US/_white/escape-menu/options-menu.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ui-options-function-look-up = Look up/Take aim
ui-options-function-hold-look-up = Hold down the key to aim
2 changes: 2 additions & 0 deletions Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ui-options-function-look-up = Присмотреться/Прицелиться
ui-options-function-hold-look-up = Удерживать клавишу для прицеливания
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
- CartridgeAntiMateriel
capacity: 5
proto: CartridgeAntiMateriel
- type: Telescope # WD EDIT

- type: entity
name: musket
Expand Down
4 changes: 4 additions & 0 deletions Resources/keybinds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,7 @@ binds:
- function: Hotbar9
type: State
key: Num9
#WD EDIT
- function: LookUp
type: State
key: Space

0 comments on commit 5fc774e

Please sign in to comment.