From 0eb64fbca040f40f816048bbfc74ca531295b8a5 Mon Sep 17 00:00:00 2001 From: Spatison <137375981+Spatison@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:21:43 +0300 Subject: [PATCH 1/3] add: TelescopeSystem --- Content.Client/Input/ContentContexts.cs | 1 + .../Options/UI/Tabs/KeyRebindTab.xaml.cs | 9 ++ .../_White/Telescope/TelescopeSystem.cs | 126 ++++++++++++++++++ .../_White/Telescope/TelescopeSystem.cs | 5 + Content.Shared/Input/ContentKeyFunctions.cs | 1 + Content.Shared/_White/CVars.cs | 14 ++ .../_White/Telescope/SharedTelescopeSystem.cs | 111 +++++++++++++++ .../_White/Telescope/TelescopeComponent.cs | 16 +++ .../en-US/_white/escape-menu/options-menu.ftl | 2 + .../Objects/Weapons/Guns/Snipers/snipers.yml | 1 + Resources/keybinds.yml | 4 + 11 files changed, 290 insertions(+) create mode 100644 Content.Client/_White/Telescope/TelescopeSystem.cs create mode 100644 Content.Server/_White/Telescope/TelescopeSystem.cs create mode 100644 Content.Shared/_White/CVars.cs create mode 100644 Content.Shared/_White/Telescope/SharedTelescopeSystem.cs create mode 100644 Content.Shared/_White/Telescope/TelescopeComponent.cs create mode 100644 Resources/Locale/en-US/_white/escape-menu/options-menu.ftl diff --git a/Content.Client/Input/ContentContexts.cs b/Content.Client/Input/ContentContexts.cs index 503a9ac953..ae5763e280 100644 --- a/Content.Client/Input/ContentContexts.cs +++ b/Content.Client/Input/ContentContexts.cs @@ -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); diff --git a/Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs b/Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs index 13e456985a..9484606951 100644 --- a/Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs +++ b/Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs @@ -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; @@ -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); @@ -186,6 +193,8 @@ void AddCheckBox(string checkBoxName, bool currentState, Action + { + 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(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 + }); + } +} diff --git a/Content.Server/_White/Telescope/TelescopeSystem.cs b/Content.Server/_White/Telescope/TelescopeSystem.cs new file mode 100644 index 0000000000..223654fd98 --- /dev/null +++ b/Content.Server/_White/Telescope/TelescopeSystem.cs @@ -0,0 +1,5 @@ +using Content.Shared._White.Telescope; + +namespace Content.Server._White.Telescope; + +public sealed class TelescopeSystem : SharedTelescopeSystem; diff --git a/Content.Shared/Input/ContentKeyFunctions.cs b/Content.Shared/Input/ContentKeyFunctions.cs index dac780783c..f25646d24a 100644 --- a/Content.Shared/Input/ContentKeyFunctions.cs +++ b/Content.Shared/Input/ContentKeyFunctions.cs @@ -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"; diff --git a/Content.Shared/_White/CVars.cs b/Content.Shared/_White/CVars.cs new file mode 100644 index 0000000000..065956993e --- /dev/null +++ b/Content.Shared/_White/CVars.cs @@ -0,0 +1,14 @@ +using Robust.Shared.Configuration; + +namespace Content.Shared._White; + +[CVarDefs] +public sealed class WhiteCVars +{ + #region Keybind + + public static readonly CVarDef HoldLookUp = + CVarDef.Create("white.hold_look_up", false, CVar.CLIENT | CVar.ARCHIVE); + + #endregion +} diff --git a/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs b/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs new file mode 100644 index 0000000000..6a8ea65827 --- /dev/null +++ b/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs @@ -0,0 +1,111 @@ +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(OnEyeOffsetChanged); + SubscribeLocalEvent(OnUnequip); + SubscribeLocalEvent(OnHandDeselected); + SubscribeLocalEvent(OnShutdown); + } + + private void OnShutdown(Entity 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 ent, ref HandDeselectedEvent args) + { + if (!TryComp(args.User, out EyeComponent? eye)) + return; + + SetOffset((args.User, eye), Vector2.Zero, ent); + } + + private void OnUnequip(Entity ent, ref GotUnequippedHandEvent args) + { + if (!TryComp(args.User, out EyeComponent? eye)) + return; + + if (!HasComp(ent.Owner)) + return; + + SetOffset((args.User, eye), Vector2.Zero, ent); + } + + public TelescopeComponent? GetRightTelescope(EntityUid? ent) + { + TelescopeComponent? telescope = null; + + 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) + 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 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 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; +} diff --git a/Content.Shared/_White/Telescope/TelescopeComponent.cs b/Content.Shared/_White/Telescope/TelescopeComponent.cs new file mode 100644 index 0000000000..599770e886 --- /dev/null +++ b/Content.Shared/_White/Telescope/TelescopeComponent.cs @@ -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; +} diff --git a/Resources/Locale/en-US/_white/escape-menu/options-menu.ftl b/Resources/Locale/en-US/_white/escape-menu/options-menu.ftl new file mode 100644 index 0000000000..b94df35ba6 --- /dev/null +++ b/Resources/Locale/en-US/_white/escape-menu/options-menu.ftl @@ -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 \ No newline at end of file diff --git a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Snipers/snipers.yml b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Snipers/snipers.yml index adb8e323f4..bd6dfdf10f 100644 --- a/Resources/Prototypes/Entities/Objects/Weapons/Guns/Snipers/snipers.yml +++ b/Resources/Prototypes/Entities/Objects/Weapons/Guns/Snipers/snipers.yml @@ -71,6 +71,7 @@ - CartridgeAntiMateriel capacity: 5 proto: CartridgeAntiMateriel + - type: Telescope # WD EDIT - type: entity name: musket diff --git a/Resources/keybinds.yml b/Resources/keybinds.yml index 2cca749317..7f4000edff 100644 --- a/Resources/keybinds.yml +++ b/Resources/keybinds.yml @@ -543,3 +543,7 @@ binds: - function: Hotbar9 type: State key: Num9 + #WD EDIT +- function: LookUp + type: State + key: Space From e6791b1c46abbc5bad6daf48ceb1f54a55dcb668 Mon Sep 17 00:00:00 2001 From: Spatison <137375981+Spatison@users.noreply.github.com> Date: Thu, 22 Aug 2024 12:55:18 +0300 Subject: [PATCH 2/3] fix --- Content.Shared/_White/Telescope/SharedTelescopeSystem.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs b/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs index 6a8ea65827..78bcf757ea 100644 --- a/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs +++ b/Content.Shared/_White/Telescope/SharedTelescopeSystem.cs @@ -55,9 +55,13 @@ private void OnUnequip(Entity ent, ref GotUnequippedHandEven 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; } @@ -90,7 +94,9 @@ private void SetOffset(Entity ent, Vector2 offset, TelescopeCompon _eye.SetOffset(ent, offset + recoil.CurrentKick, ent); } else + { _eye.SetOffset(ent, offset, ent); + } } public void SetParameters(Entity ent, float? divisor = null, float? lerpAmount = null) From 605b78e570272328f09df1039d887c86d5737661 Mon Sep 17 00:00:00 2001 From: Spatison <137375981+Spatison@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:44:47 +0300 Subject: [PATCH 3/3] add: RU loc --- Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl diff --git a/Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl b/Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl new file mode 100644 index 0000000000..b8667e14c0 --- /dev/null +++ b/Resources/Locale/ru-RU/_white/escape-menu/options-menu.ftl @@ -0,0 +1,2 @@ +ui-options-function-look-up = Присмотреться/Прицелиться +ui-options-function-hold-look-up = Удерживать клавишу для прицеливания \ No newline at end of file