Skip to content

Commit

Permalink
[Feature] Система передачи из рук в руки (space-syndicate#780)
Browse files Browse the repository at this point in the history
* up 1

* refactor

* Update alerts.yml

* fix popups

* Update SharedOfferItemSystem.cs

---------

Co-authored-by: Zack Backmen <backmen@bk.ru>
  • Loading branch information
AwareFoxy and Rxup committed Nov 8, 2024
1 parent 417f9dc commit b8a2c21
Show file tree
Hide file tree
Showing 23 changed files with 605 additions and 2 deletions.
72 changes: 72 additions & 0 deletions Content.Client/Backmen/OfferItem/OfferItemIndicatorsOverlay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Numerics;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.UserInterface;
using Robust.Shared.Enums;
using Robust.Shared.Utility;

namespace Content.Client.Backmen.OfferItem;

public sealed class OfferItemIndicatorsOverlay : Overlay
{
private readonly IInputManager _inputManager;
private readonly IEntityManager _entMan;
private readonly IEyeManager _eye;
private readonly OfferItemSystem _offer;

private readonly Texture _sight;

public override OverlaySpace Space => OverlaySpace.ScreenSpace;

private readonly Color _mainColor = Color.White.WithAlpha(0.3f);
private readonly Color _strokeColor = Color.Black.WithAlpha(0.5f);
private readonly float _scale = 0.6f; // 1 is a little big

public OfferItemIndicatorsOverlay(IInputManager input, IEntityManager entMan,
IEyeManager eye, OfferItemSystem offerSys)
{
_inputManager = input;
_entMan = entMan;
_eye = eye;
_offer = offerSys;

var spriteSys = _entMan.EntitySysManager.GetEntitySystem<SpriteSystem>();
_sight = spriteSys.Frame0(new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Misc/give_item.rsi"),
"give_item"));
}

protected override bool BeforeDraw(in OverlayDrawArgs args)
{
if (!_offer.IsInOfferMode())
return false;

return base.BeforeDraw(in args);
}

protected override void Draw(in OverlayDrawArgs args)
{
var mouseScreenPosition = _inputManager.MouseScreenPosition;
var mousePosMap = _eye.PixelToMap(mouseScreenPosition);
if (mousePosMap.MapId != args.MapId)
return;


var mousePos = mouseScreenPosition.Position;
var uiScale = (args.ViewportControl as Control)?.UIScale ?? 1f;
var limitedScale = uiScale > 1.25f ? 1.25f : uiScale;

DrawSight(_sight, args.ScreenHandle, mousePos, limitedScale * _scale);
}

private void DrawSight(Texture sight, DrawingHandleScreen screen, Vector2 centerPos, float scale)
{
var sightSize = sight.Size * scale;
var expandedSize = sightSize + new Vector2(7f, 7f);

screen.DrawTextureRect(sight,
UIBox2.FromDimensions(centerPos - sightSize * 0.5f, sightSize), _strokeColor);
screen.DrawTextureRect(sight,
UIBox2.FromDimensions(centerPos - expandedSize * 0.5f, expandedSize), _mainColor);
}
}
54 changes: 54 additions & 0 deletions Content.Client/Backmen/OfferItem/OfferItemSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Content.Shared.Backmen.OfferItem;
using Content.Shared._CorvaxNext.NextVars;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.Player;
using Robust.Shared.Configuration;

namespace Content.Client.Backmen.OfferItem;

public sealed class OfferItemSystem : SharedOfferItemSystem
{
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IEyeManager _eye = default!;

public override void Initialize()
{
base.Initialize();
Subs.CVar(_cfg, NextVars.OfferModeIndicatorsPointShow, OnShowOfferIndicatorsChanged, true);
}



public override void Shutdown()
{
_overlayManager.RemoveOverlay<OfferItemIndicatorsOverlay>();
base.Shutdown();
}

public bool IsInOfferMode()
{
var entity = _playerManager.LocalEntity;

if (entity == null)
return false;

return IsInOfferMode(entity.Value);
}
private void OnShowOfferIndicatorsChanged(bool isShow)
{
if (isShow)
{
_overlayManager.AddOverlay(new OfferItemIndicatorsOverlay(
_inputManager,
EntityManager,
_eye,
this));
}
else
_overlayManager.RemoveOverlay<OfferItemIndicatorsOverlay>();
}
}
1 change: 1 addition & 0 deletions Content.Client/Input/ContentContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static void SetupContexts(IInputContextContainer contexts)
human.AddFunction(ContentKeyFunctions.Arcade1);
human.AddFunction(ContentKeyFunctions.Arcade2);
human.AddFunction(ContentKeyFunctions.Arcade3);
human.AddFunction(ContentKeyFunctions.OfferItem); // Corvax-Next-Offer

// actions should be common (for ghosts, mobs, etc)
common.AddFunction(ContentKeyFunctions.OpenActionsMenu);
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Options/UI/Tabs/KeyRebindTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void AddCheckBox(string checkBoxName, bool currentState, Action<BaseButton.Butto
AddButton(ContentKeyFunctions.MoveStoredItem);
AddButton(ContentKeyFunctions.RotateStoredItem);
AddButton(ContentKeyFunctions.SaveItemLocation);

AddButton(ContentKeyFunctions.OfferItem); // Corvax-Next-Offer
AddHeader("ui-options-header-interaction-adv");
AddButton(ContentKeyFunctions.SmartEquipBackpack);
AddButton(ContentKeyFunctions.SmartEquipBelt);
Expand Down
1 change: 1 addition & 0 deletions Content.Client/Options/UI/Tabs/MiscTab.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
StyleClasses="LabelKeyText"/>
<CheckBox Name="ShowHeldItemCheckBox" Text="{Loc 'ui-options-show-held-item'}" />
<CheckBox Name="ShowCombatModeIndicatorsCheckBox" Text="{Loc 'ui-options-show-combat-mode-indicators'}" />
<CheckBox Name="ShowOfferModeIndicatorsCheckBox" Text="{Loc 'ui-options-show-offer-mode-indicators'}" />
<Label Text="{Loc 'ui-options-general-storage'}"
StyleClasses="LabelKeyText"/>
<CheckBox Name="OpaqueStorageWindowCheckBox" Text="{Loc 'ui-options-opaque-storage-window'}" />
Expand Down
3 changes: 2 additions & 1 deletion Content.Client/Options/UI/Tabs/MiscTab.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using Content.Client.UserInterface.Screens;
using Content.Shared._CorvaxNext.NextVars;
using Content.Shared.CCVar;
using Content.Shared.HUD;
using Robust.Client.AutoGenerated;
Expand Down Expand Up @@ -52,7 +53,7 @@ public MiscTab()
Control.AddOptionCheckBox(CCVars.ChatEnableFancyBubbles, FancySpeechBubblesCheckBox);
Control.AddOptionCheckBox(CCVars.ChatFancyNameBackground, FancyNameBackgroundsCheckBox);
Control.AddOptionCheckBox(CCVars.StaticStorageUI, StaticStorageUI);

Control.AddOptionCheckBox(NextVars.OfferModeIndicatorsPointShow, ShowOfferModeIndicatorsCheckBox); // Corvax-Next-Offer
Control.Initialize();
}
}
58 changes: 58 additions & 0 deletions Content.Server/Backmen/OfferItem/OfferItemSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Content.Shared.Alert;
using Content.Shared.Backmen.OfferItem;
using Content.Shared.Hands.Components;

namespace Content.Server.Backmen.OfferItem;

public sealed class OfferItemSystem : SharedOfferItemSystem
{
[Dependency] private readonly AlertsSystem _alertsSystem = default!;

private float _offerAcc = 0;
private const float OfferAccMax = 3f;

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

_offerAcc += frameTime;
if (_offerAcc >= OfferAccMax)
{
_offerAcc -= OfferAccMax;
}
else
{
return;
}

var query = EntityQueryEnumerator<OfferItemComponent,HandsComponent>();
while (query.MoveNext(out var uid, out var offerItem, out var hands))
{
if (hands.ActiveHand == null)
continue;

if (offerItem.Hand != null &&
hands.Hands[offerItem.Hand].HeldEntity == null)
{
if (offerItem.Target != null)
{
UnReceive(offerItem.Target.Value, offerItem: offerItem);
offerItem.IsInOfferMode = false;
Dirty(uid, offerItem);
}
else
UnOffer(uid, offerItem);
}

if (!offerItem.IsInReceiveMode)
{
_alertsSystem.ClearAlert(uid, OfferAlert);
continue;
}

_alertsSystem.ShowAlert(uid, OfferAlert);
}
}


}
8 changes: 8 additions & 0 deletions Content.Shared/Backmen/Alert/Click/AcceptingOffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Content.Shared.Alert;

namespace Content.Shared.Backmen.Alert.Click;

/// <summary>
/// Accepting the offer and receive item
/// </summary>
public sealed partial class AcceptOfferAlertEvent : BaseAlertEvent;
26 changes: 26 additions & 0 deletions Content.Shared/Backmen/OfferItem/OfferItemComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Robust.Shared.GameStates;

namespace Content.Shared.Backmen.OfferItem;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
[Access(typeof(SharedOfferItemSystem))]
public sealed partial class OfferItemComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
public bool IsInOfferMode;

[DataField, AutoNetworkedField]
public bool IsInReceiveMode;

[DataField, AutoNetworkedField]
public string? Hand;

[DataField, AutoNetworkedField]
public EntityUid? Item;

[DataField, AutoNetworkedField]
public EntityUid? Target;

[DataField]
public float MaxOfferDistance = 2f;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components;
using Content.Shared.Input;
using Content.Shared.Popups;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;

namespace Content.Shared.Backmen.OfferItem;

public abstract partial class SharedOfferItemSystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;

private void InitializeInteractions()
{
CommandBinds.Builder
.Bind(ContentKeyFunctions.OfferItem, InputCmdHandler.FromDelegate(SetInOfferMode, handle: false, outsidePrediction: false))
.Register<SharedOfferItemSystem>();
}

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

CommandBinds.Unregister<SharedOfferItemSystem>();
}

private void SetInOfferMode(ICommonSession? session)
{
if (!_timing.IsFirstTimePredicted)
return;

if (session is not { } playerSession)
return;

if ((playerSession.AttachedEntity is not { Valid: true } uid || !Exists(uid)) ||
!_actionBlocker.CanInteract(uid, null))
return;

if (!TryComp<OfferItemComponent>(uid, out var offerItem))
return;

if (!TryComp<HandsComponent>(uid, out var hands) || hands.ActiveHand == null)
return;

offerItem.Item = hands.ActiveHand.HeldEntity;

if (offerItem.IsInOfferMode == false)
{
if (offerItem.Item == null)
{
_popup.PopupEntity(Loc.GetString("offer-item-empty-hand"), uid, uid);
return;
}

if (offerItem.Hand == null || offerItem.Target == null)
{
offerItem.IsInOfferMode = true;
offerItem.Hand = hands.ActiveHand.Name;

Dirty(uid, offerItem);
return;
}
}

if (offerItem.Target != null)
{
UnReceive(offerItem.Target.Value, offerItem: offerItem);
offerItem.IsInOfferMode = false;
Dirty(uid, offerItem);
return;
}

UnOffer(uid, offerItem);
}
}
Loading

0 comments on commit b8a2c21

Please sign in to comment.