Moves Hands to shared, some prediction (#3829)

* HandsGuiState

* Gui state setting methods

* code cleanup

* Removes TryGetHands

* ClientHand

* Gui Hands

* Refactor WIP 1

* Hand index

* refactors 2

* wip 3

* wip 4

* wiip 4

* wip 5

* wip 6

* wip 7

* wip 8

* wip 9

* wip 11

* Hand ui mostly looks fine

* hands gui cleanup 1

* cleanup 2

* wip 13

* hand enabled

* stuff

* Hands gui gap fix

* onpressed test

* hand gui buttons events work

* bag activation works

* fix item use

* todo comment

* hands activate fix

* Moves Client Hands back to using strings to identify active hand

* fixes action hand highlighting

* diff fix

* serverhand

* SharedHand

* SharedHand, IReadOnlyHand

* Client Hands only stores SharedHand

* cleanup server hands

* server hand container shutdown

* misc renames, refactors of serverhand

* stuff 1

* stuff 3

* server hand refactor 1

* Undo API changes to remove massive diff

* More API name fixes

* server hands cleanup 2

* cleanup 3

* dropping cleanup

* Cleanup 4

* MoveItemFromHand

* Stuff

* region sorting

* Hand Putter methods cleanup

* stuff 2

* Merges all of serverhand and clienthand into sharedhand

* Other hands systems, hack to make inhands update (gui state set every frame, visualzier updated every frame)

* GetFinalDropCoordinates cleanup

* SwapHands cleanup

* Moves server hands code to shared hands

* Fixed hand selected and deselected

* Naming fixes

* Server hands system cleanup

* Hands privacy fixes

* Client hand updates when containers are modified

* HeldItemVisualizer

* Fixes hand gui item status panel

* method name fix

* Swap hands prediction

* Dropping prediction

* Fixes pickup entity animation

* Fixes HeldItemsVisualizer

* moves item pickup to shared

* PR cleanup

* fixes hand enabling/disabling

* build fix

* Conflict fixes

* Fixes pickup animation

* Uses component directed message subscriptions

* event unsubscriptions in hand system

* unsubscribe fix

* CanInsertEntityIntoHand checks if hand is enabled

* Moving items from one hand to another checks if the hands can pick up and drop

* Fixes stop pulling not re-enabling hand

* Fixes pickup animation for entities containers on the floor

* Fixes using held items

* Fixes multiple hands guis appearing

* test fix

* removes obsolete system sunsubscribes

* Checks IsFirstTimePredicted before playing drop animation

* fixes hand item deleted crash

* Uses Get to get other system

* Replaces AppearanceComponent with SharedAppearanceComponent

* Replaces EnsureComponent with TryGetComponent

* Improves event class names

* Moves property up to top of class

* Moves code for determining the hand visualizer rsi state into the visualizer instead of being determined on hand component

* Eventbus todo comment

* Yaml fix for changed visualizer name

* Makes HandsVisuals a byte

* Removes state from HandsVisualizer

* Fixes hand using interaction method name

* Namespace changes fixes

* Fix for changed hand interaction method

* missing }

* conflict build fix

* Moves cleint HandsSystem to correct folder

* Moved namespace fix for interaction test

* Moves Handsvisualizer ot correct folder

* Moves SharedHandsSystem to correct folder

* Fixes errors from moving namespace of hand systems

* Fixes PDA component changes

* Fixes ActionsComponent diff

* Fixes inventory component diff

* fixes null ref

* Replaces obsolete Loc.GetString usage with fluent translations

* Fluent for hands disarming

* SwapHands and Drop user input specify to the server which hand

* Pickup animation WorldPosiiton todo

* Cleans up hands gui subscription handling

* Fixes change in ActionBlockerSystem access

* Namespace references fixes

* HandsComponent PlayerAttached/Detached messages are handled through eventbus

* Fixes GasCanisterSystem drop method usage

* Fix gameticker equipping method at new location

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
collinlunn
2021-06-21 02:21:20 -07:00
committed by GitHub
parent a6a073cfdb
commit f2816e8081
33 changed files with 1829 additions and 1525 deletions

View File

@@ -226,11 +226,11 @@ namespace Content.Client.Actions
StopHighlightingItemSlots();
// figure out if it's in hand or inventory and highlight it
foreach (var hand in _handsComponent!.Hands)
foreach (var hand in _handsComponent!.Gui!.Hands)
{
if (hand.Entity != item || hand.Button == null) continue;
_highlightingItemSlots.Add(hand.Button);
hand.Button.Highlight(true);
if (hand.HeldItem != item || hand.HandButton == null) continue;
_highlightingItemSlots.Add(hand.HandButton);
hand.HandButton.Highlight(true);
return;
}

View File

@@ -1,16 +1,14 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Client.Animations;
using Content.Client.HUD;
using Content.Shared.Hands.Components;
using Content.Shared.Item;
using Robust.Client.GameObjects;
using Robust.Client.ResourceManagement;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Players;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Client.Hands
@@ -22,239 +20,50 @@ namespace Content.Client.Hands
{
[Dependency] private readonly IGameHud _gameHud = default!;
private HandsGui? _gui;
private readonly List<Hand> _hands = new();
[ViewVariables] public IReadOnlyList<Hand> Hands => _hands;
[ViewVariables] public string? ActiveIndex { get; private set; }
[ViewVariables] private ISpriteComponent? _sprite;
[ViewVariables] public IEntity? ActiveHand => GetEntity(ActiveIndex);
public override bool IsHolding(IEntity entity)
{
foreach (var hand in _hands)
{
if (hand.Entity == entity)
{
return true;
}
}
return false;
}
private void AddHand(Hand hand)
{
_sprite?.LayerMapReserveBlank($"hand-{hand.Name}");
_hands.Insert(hand.Index, hand);
}
public Hand? GetHand(string? name)
{
return Hands.FirstOrDefault(hand => hand.Name == name);
}
private bool TryHand(string name, [NotNullWhen(true)] out Hand? hand)
{
return (hand = GetHand(name)) != null;
}
public IEntity? GetEntity(string? handName)
{
if (handName == null)
{
return null;
}
return GetHand(handName)?.Entity;
}
[ViewVariables]
public HandsGui? Gui { get; private set; }
protected override void OnRemove()
{
ClearGui();
base.OnRemove();
_gui?.Dispose();
}
protected override void Initialize()
{
base.Initialize();
if (Owner.TryGetComponent(out _sprite))
{
foreach (var hand in _hands)
{
_sprite.LayerMapReserveBlank($"hand-{hand.Name}");
UpdateHandSprites(hand);
}
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState == null)
{
return;
}
var cast = (HandsComponentState) curState;
foreach (var sharedHand in cast.Hands)
{
if (!TryHand(sharedHand.Name, out var hand))
{
hand = new Hand(this, sharedHand, Owner.EntityManager);
AddHand(hand);
}
else
{
hand.Location = sharedHand.Location;
hand.Entity = sharedHand.EntityUid.HasValue
? Owner.EntityManager.GetEntity(sharedHand.EntityUid.Value)
: null;
}
hand.Enabled = sharedHand.Enabled;
UpdateHandSprites(hand);
}
foreach (var currentHand in _hands.ToList())
{
if (cast.Hands.All(newHand => newHand.Name != currentHand.Name))
{
_hands.Remove(currentHand);
_gui?.RemoveHand(currentHand);
HideHand(currentHand);
}
}
ActiveIndex = cast.ActiveIndex;
_gui?.UpdateHandIcons();
RefreshInHands();
}
private void HideHand(Hand hand)
{
_sprite?.LayerSetVisible($"hand-{hand.Name}", false);
}
private void UpdateHandSprites(Hand hand)
{
if (_sprite == null)
{
return;
}
var entity = hand.Entity;
var name = hand.Name;
if (entity == null)
{
if (_sprite.LayerMapTryGet($"hand-{name}", out var layer))
{
_sprite.LayerSetVisible(layer, false);
}
return;
}
if (!entity.TryGetComponent(out SharedItemComponent? item))
if (curState is not HandsComponentState state)
return;
if (item.RsiPath == null)
Hands.Clear();
foreach (var handState in state.Hands)
{
_sprite.LayerSetVisible($"hand-{name}", false);
var newHand = new Hand(handState.Name, handState.Enabled, handState.Location);
Hands.Add(newHand);
}
else
ActiveHand = state.ActiveHand;
UpdateHandContainers();
UpdateHandVisualizer();
UpdateHandsGuiState();
}
public void SettupGui()
{
if (Gui == null)
{
var rsi = IoCManager.Resolve<IResourceCache>().GetResource<RSIResource>(SharedSpriteComponent.TextureRoot / item.RsiPath).RSI;
var handName = hand.Location.ToString().ToLowerInvariant();
var prefix = item.EquippedPrefix;
var state = prefix != null ? $"{prefix}-inhand-{handName}" : $"inhand-{handName}";
if (!rsi.TryGetState(state, out _))
return;
var color = item.Color;
_sprite.LayerSetColor($"hand-{name}", color);
_sprite.LayerSetVisible($"hand-{name}", true);
_sprite.LayerSetState($"hand-{name}", state, rsi);
Gui = new HandsGui();
_gameHud.HandsContainer.AddChild(Gui);
Gui.HandClick += args => OnHandClick(args.HandClicked);
Gui.HandActivate += args => OnActivateInHand(args.HandUsed);
UpdateHandsGuiState();
}
}
public void RefreshInHands()
public void ClearGui()
{
if (!Initialized) return;
foreach (var hand in _hands)
{
UpdateHandSprites(hand);
}
}
protected override void Startup()
{
base.Startup();
ActiveIndex = _hands.LastOrDefault()?.Name;
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case HandEnabledMsg msg:
{
var hand = GetHand(msg.Name);
if (hand?.Button == null)
{
break;
}
hand.Button.Blocked.Visible = false;
break;
}
case HandDisabledMsg msg:
{
var hand = GetHand(msg.Name);
if (hand?.Button == null)
{
break;
}
hand.Button.Blocked.Visible = true;
break;
}
}
}
public void PlayerDetached() { _gui?.Parent?.RemoveChild(_gui); }
public void PlayerAttached()
{
if (_gui == null)
{
_gui = new HandsGui();
}
else
{
_gui.Parent?.RemoveChild(_gui);
}
_gameHud.HandsContainer.AddChild(_gui);
_gui.UpdateHandIcons();
Gui?.Dispose();
Gui = null;
}
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null)
@@ -263,94 +72,126 @@ namespace Content.Client.Hands
switch (message)
{
case AnimatePickupEntityMessage msg:
{
if (Owner.EntityManager.TryGetEntity(msg.EntityId, out var entity))
{
ReusableAnimations.AnimateEntityPickup(entity, msg.EntityPosition, Owner.Transform.WorldPosition);
}
case PickupAnimationMessage msg:
RunPickupAnimation(msg);
break;
}
}
}
public void SendChangeHand(string index)
public override void HandsModified()
{
SendNetworkMessage(new ClientChangedHandMsg(index));
base.HandsModified();
UpdateHandContainers();
UpdateHandVisualizer();
UpdateHandsGuiState();
}
public void AttackByInHand(string index)
private void OnHandClick(string handClicked)
{
SendNetworkMessage(new ClientAttackByInHandMsg(index));
}
if (!TryGetHand(handClicked, out var pressedHand))
return;
public void UseActiveHand()
{
if (GetEntity(ActiveIndex) != null)
{
SendNetworkMessage(new UseInHandMsg());
}
}
public void ActivateItemInHand(string handIndex)
{
if (GetEntity(handIndex) == null)
if (!TryGetActiveHand(out var activeHand))
return;
var pressedEntity = pressedHand.HeldEntity;
var activeEntity = activeHand.HeldEntity;
if (pressedHand == activeHand && activeEntity != null)
{
SendNetworkMessage(new UseInHandMsg()); //use item in hand
return;
}
SendNetworkMessage(new ActivateInHandMsg(handIndex));
}
}
public class Hand
{
private bool _enabled = true;
public Hand(HandsComponent parent, SharedHand hand, IEntityManager manager, HandButton? button = null)
{
Parent = parent;
Index = hand.Index;
Name = hand.Name;
Location = hand.Location;
Button = button;
if (!hand.EntityUid.HasValue)
if (pressedHand != activeHand && pressedEntity == null)
{
SendNetworkMessage(new ClientChangedHandMsg(pressedHand.Name)); //swap hand
return;
}
manager.TryGetEntity(hand.EntityUid.Value, out var entity);
Entity = entity;
if (pressedHand != activeHand && pressedEntity != null && activeEntity != null)
{
SendNetworkMessage(new ClientAttackByInHandMsg(pressedHand.Name)); //use active item on held item
return;
}
if (pressedHand != activeHand && pressedEntity != null && activeEntity == null)
{
SendNetworkMessage(new MoveItemFromHandMsg(pressedHand.Name)); //move item in hand to active hand
return;
}
}
private HandsComponent Parent { get; }
public int Index { get; }
public string Name { get; }
public HandLocation Location { get; set; }
public IEntity? Entity { get; set; }
public HandButton? Button { get; set; }
public bool Enabled
private void OnActivateInHand(string handActivated)
{
get => _enabled;
set
SendNetworkMessage(new ActivateInHandMsg(handActivated));
}
public void UpdateHandContainers()
{
if (!Owner.TryGetComponent<ContainerManagerComponent>(out var containerMan))
return;
foreach (var hand in Hands)
{
if (_enabled == value)
if (hand.Container == null)
{
return;
containerMan.TryGetContainer(hand.Name, out var container);
hand.Container = container;
}
_enabled = value;
Parent.Dirty();
var message = value
? (ComponentMessage) new HandEnabledMsg(Name)
: new HandDisabledMsg(Name);
Parent.HandleMessage(message, Parent);
Parent.Owner.SendMessage(Parent, message);
}
}
public void UpdateHandVisualizer()
{
if (Owner.TryGetComponent(out SharedAppearanceComponent? appearance))
appearance.SetData(HandsVisuals.VisualState, GetHandsVisualState());
}
public void UpdateHandsGuiState()
{
Gui?.SetState(GetHandsGuiState());
}
private HandsGuiState GetHandsGuiState()
{
var handStates = new List<GuiHand>();
foreach (var hand in ReadOnlyHands)
{
var handState = new GuiHand(hand.Name, hand.Location, hand.HeldEntity, hand.Enabled);
handStates.Add(handState);
}
return new HandsGuiState(handStates, ActiveHand);
}
private HandsVisualState GetHandsVisualState()
{
var hands = new List<HandVisualState>();
foreach (var hand in ReadOnlyHands)
{
if (hand.HeldEntity == null)
continue;
if (!hand.HeldEntity.TryGetComponent(out SharedItemComponent? item) || item.RsiPath == null)
continue;
var handState = new HandVisualState(item.RsiPath, item.EquippedPrefix, hand.Location, item.Color);
hands.Add(handState);
}
return new(hands);
}
private void RunPickupAnimation(PickupAnimationMessage msg)
{
if (!Owner.EntityManager.TryGetEntity(msg.EntityUid, out var entity))
return;
if (!IoCManager.Resolve<IGameTiming>().IsFirstTimePredicted)
return;
ReusableAnimations.AnimateEntityPickup(entity, msg.InitialPosition, msg.PickupDirection);
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Client.HUD;
@@ -15,246 +16,242 @@ using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Client.Hands
{
public class HandsGui : Control
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly IItemSlotManager _itemSlotManager = default!;
[Dependency] private readonly IGameHud _gameHud = default!;
[Dependency] private readonly INetConfigurationManager _configManager = default!;
private Texture _leftHandTexture;
private Texture _middleHandTexture;
private Texture _rightHandTexture;
private Texture StorageTexture => _gameHud.GetHudTexture("back.png");
private Texture BlockedTexture => _resourceCache.GetTexture("/Textures/Interface/Inventory/blocked.png");
private readonly ItemStatusPanel _topPanel;
private ItemStatusPanel StatusPanel { get; }
private readonly HBoxContainer _guiContainer;
private readonly VBoxContainer _handsColumn;
private readonly HBoxContainer _handsContainer;
private HBoxContainer HandsContainer { get; }
[ViewVariables]
public IReadOnlyList<GuiHand> Hands => _hands;
private List<GuiHand> _hands = new();
private string? ActiveHand { get; set; }
public Action<HandClickEventArgs>? HandClick; //TODO: Move to Eventbus
public Action<HandActivateEventArgs>? HandActivate; //TODO: Move to Eventbus
public HandsGui()
{
IoCManager.InjectDependencies(this);
_configManager.OnValueChanged(CCVars.HudTheme, UpdateHudTheme);
_configManager.OnValueChanged(CCVars.HudTheme, UpdateHudTheme, invokeImmediately: true);
AddChild(_guiContainer = new HBoxContainer
AddChild(new HBoxContainer
{
SeparationOverride = 0,
HorizontalAlignment = HAlignment.Center,
Children =
{
(_handsColumn = new VBoxContainer
new VBoxContainer
{
Children =
{
(_topPanel = ItemStatusPanel.FromSide(HandLocation.Middle)),
(_handsContainer = new HBoxContainer{HorizontalAlignment = HAlignment.Center})
(StatusPanel = ItemStatusPanel.FromSide(HandLocation.Middle)),
(HandsContainer = new HBoxContainer() { HorizontalAlignment = HAlignment.Center } ),
}
}),
},
}
});
_leftHandTexture = _gameHud.GetHudTexture("hand_l.png");
_middleHandTexture = _gameHud.GetHudTexture("hand_l.png");
_rightHandTexture = _gameHud.GetHudTexture("hand_r.png");
}
private void UpdateHudTheme(int idx)
public void SetState(HandsGuiState state)
{
if (!_gameHud.ValidateHudTheme(idx))
ActiveHand = state.ActiveHand;
_hands = state.GuiHands;
UpdateGui();
}
private void UpdateGui()
{
HandsContainer.DisposeAllChildren();
foreach (var hand in _hands)
{
return;
var newButton = MakeHandButton(hand.HandLocation);
HandsContainer.AddChild(newButton);
hand.HandButton = newButton;
var handName = hand.Name;
newButton.OnPressed += args => OnHandPressed(args, handName);
newButton.OnStoragePressed += args => OnStoragePressed(handName);
newButton.Blocked.Visible = !hand.Enabled;
_itemSlotManager.SetItemSlot(newButton, hand.HeldItem);
}
_leftHandTexture = _gameHud.GetHudTexture("hand_l.png");
_middleHandTexture = _gameHud.GetHudTexture("hand_l.png");
_rightHandTexture = _gameHud.GetHudTexture("hand_r.png");
UpdateHandIcons();
}
private Texture HandTexture(HandLocation location)
{
switch (location)
if (TryGetActiveHand(out var activeHand))
{
case HandLocation.Left:
return _leftHandTexture;
case HandLocation.Middle:
return _middleHandTexture;
case HandLocation.Right:
return _rightHandTexture;
default:
throw new ArgumentOutOfRangeException(nameof(location), location, null);
activeHand.HandButton.SetActiveHand(true);
StatusPanel.Update(activeHand.HeldItem);
}
}
/// <summary>
/// Adds a new hand to this control
/// </summary>
/// <param name="hand">The hand to add to this control</param>
/// <param name="buttonLocation">
/// The actual location of the button. The right hand is drawn
/// on the LEFT of the screen.
/// </param>
private void AddHand(Hand hand, HandLocation buttonLocation)
private void OnHandPressed(GUIBoundKeyEventArgs args, string handName)
{
var textureName = "hand_l.png";
if(buttonLocation == HandLocation.Right)
{
textureName = "hand_r.png";
}
var buttonTexture = HandTexture(buttonLocation);
var storageTexture = _gameHud.GetHudTexture("back.png");
var blockedTexture = _resourceCache.GetTexture("/Textures/Interface/Inventory/blocked.png");
var button = new HandButton(buttonTexture, storageTexture, textureName, blockedTexture, buttonLocation);
var slot = hand.Name;
button.OnPressed += args => HandKeyBindDown(args, slot);
button.OnStoragePressed += args => _OnStoragePressed(args, slot);
_handsContainer.AddChild(button);
hand.Button = button;
}
public void RemoveHand(Hand hand)
{
var button = hand.Button;
if (button != null)
{
_handsContainer.RemoveChild(button);
}
}
/// <summary>
/// Gets the hands component controlling this gui
/// </summary>
/// <param name="hands"></param>
/// <returns>true if successful and false if failure</returns>
private bool TryGetHands([NotNullWhen(true)] out HandsComponent? hands)
{
hands = default;
var entity = _playerManager?.LocalPlayer?.ControlledEntity;
return entity != null && entity.TryGetComponent(out hands);
}
public void UpdateHandIcons()
{
if (Parent == null)
{
return;
}
UpdateDraw();
if (!TryGetHands(out var component))
{
return;
}
// TODO: Remove button on remove hand
var hands = component.Hands.OrderByDescending(x => x.Location).ToArray();
for (var i = 0; i < hands.Length; i++)
{
var hand = hands[i];
if (hand.Button == null)
{
AddHand(hand, hand.Location);
}
hand.Button!.Button.Texture = HandTexture(hand.Location);
hand.Button!.SetPositionInParent(i);
_itemSlotManager.SetItemSlot(hand.Button, hand.Entity);
hand.Button!.SetActiveHand(component.ActiveIndex == hand.Name);
}
}
private void HandKeyBindDown(GUIBoundKeyEventArgs args, string slotName)
{
if (!TryGetHands(out var hands))
{
return;
}
if (args.Function == ContentKeyFunctions.MouseMiddle)
{
hands.SendChangeHand(slotName);
args.Handle();
return;
}
var entity = hands.GetEntity(slotName);
if (entity == null)
{
if (args.Function == EngineKeyFunctions.UIClick && hands.ActiveIndex != slotName)
{
hands.SendChangeHand(slotName);
args.Handle();
}
return;
}
if (_itemSlotManager.OnButtonPressed(args, entity))
{
args.Handle();
return;
}
if (args.Function == EngineKeyFunctions.UIClick)
{
if (hands.ActiveIndex == slotName)
{
hands.UseActiveHand();
}
else
{
hands.AttackByInHand(slotName);
}
args.Handle();
HandClick?.Invoke(new HandClickEventArgs(handName));
}
else if (TryGetHand(handName, out var hand))
{
_itemSlotManager.OnButtonPressed(args, hand.HeldItem);
}
}
private void _OnStoragePressed(GUIBoundKeyEventArgs args, string handIndex)
private void OnStoragePressed(string handName)
{
if (args.Function != EngineKeyFunctions.UIClick || !TryGetHands(out var hands))
{
return;
}
hands.ActivateItemInHand(handIndex);
HandActivate?.Invoke(new HandActivateEventArgs(handName));
}
private void UpdatePanels()
private bool TryGetActiveHand([NotNullWhen(true)] out GuiHand? activeHand)
{
if (!TryGetHands(out var component))
{
return;
}
TryGetHand(ActiveHand, out activeHand);
return activeHand != null;
}
foreach (var hand in component.Hands)
{
_itemSlotManager.UpdateCooldown(hand.Button, hand.Entity);
}
private bool TryGetHand(string? handName, [NotNullWhen(true)] out GuiHand? foundHand)
{
foundHand = null;
_topPanel.Update(component.GetEntity(component.ActiveIndex));
if (handName == null)
return false;
foreach (var hand in _hands)
{
if (hand.Name == handName)
foundHand = hand;
}
return foundHand != null;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
UpdatePanels();
foreach (var hand in _hands)
_itemSlotManager.UpdateCooldown(hand.HandButton, hand.HeldItem);
}
private HandButton MakeHandButton(HandLocation buttonLocation)
{
var buttonTextureName = buttonLocation switch
{
HandLocation.Right => "hand_r.png",
_ => "hand_l.png"
};
var buttonTexture = _gameHud.GetHudTexture(buttonTextureName);
return new HandButton(buttonTexture, StorageTexture, buttonTextureName, BlockedTexture, buttonLocation);
}
private void UpdateHudTheme(int idx)
{
UpdateGui();
}
public class HandClickEventArgs
{
public string HandClicked { get; }
public HandClickEventArgs(string handClicked)
{
HandClicked = handClicked;
}
}
public class HandActivateEventArgs
{
public string HandUsed { get; }
public HandActivateEventArgs(string handUsed)
{
HandUsed = handUsed;
}
}
}
/// <summary>
/// Info on a set of hands to be displayed.
/// </summary>
public class HandsGuiState
{
/// <summary>
/// The set of hands to be displayed.
/// </summary>
[ViewVariables]
public List<GuiHand> GuiHands { get; } = new();
/// <summary>
/// The name of the currently active hand.
/// </summary>
[ViewVariables]
public string? ActiveHand { get; }
public HandsGuiState(List<GuiHand> guiHands, string? activeHand = null)
{
GuiHands = guiHands;
ActiveHand = activeHand;
}
}
/// <summary>
/// Info on an individual hand to be displayed.
/// </summary>
public class GuiHand
{
/// <summary>
/// The name of this hand.
/// </summary>
[ViewVariables]
public string Name { get; }
/// <summary>
/// Where this hand is located.
/// </summary>
[ViewVariables]
public HandLocation HandLocation { get; }
/// <summary>
/// The item being held in this hand.
/// </summary>
[ViewVariables]
public IEntity? HeldItem { get; }
/// <summary>
/// The button in the gui associated with this hand. Assumed to be set by gui shortly after being received from the client HandsComponent.
/// </summary>
[ViewVariables]
public HandButton HandButton { get; set; } = default!;
/// <summary>
/// If this hand can be used by the player.
/// </summary>
[ViewVariables]
public bool Enabled { get; }
public GuiHand(string name, HandLocation handLocation, IEntity? heldItem, bool enabled)
{
Name = name;
HandLocation = handLocation;
HeldItem = heldItem;
Enabled = enabled;
}
}
}

View File

@@ -1,16 +1,80 @@
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Input;
using Robust.Client.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Players;
namespace Content.Client.Hands
{
internal class HandsSystem : EntitySystem
internal sealed class HandsSystem : SharedHandsSystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandsComponent, PlayerAttachedEvent>((_, component, _) => component.PlayerAttached());
SubscribeLocalEvent<HandsComponent, PlayerDetachedEvent>((_, component, _) => component.PlayerDetached());
SubscribeLocalEvent<HandsComponent, PlayerAttachedEvent>((_, component, _) => component.SettupGui());
SubscribeLocalEvent<HandsComponent, PlayerDetachedEvent>((_, component, _) => component.ClearGui());
CommandBinds.Builder
.Bind(ContentKeyFunctions.SwapHands, InputCmdHandler.FromDelegate(SwapHandsPressed))
.Bind(ContentKeyFunctions.Drop, new PointerInputCmdHandler(DropPressed))
.Register<HandsSystem>();
}
public override void Shutdown()
{
CommandBinds.Unregister<HandsSystem>();
base.Shutdown();
}
private void SwapHandsPressed(ICommonSession? session)
{
if (session == null)
return;
var player = session.AttachedEntity;
if (player == null)
return;
if (!player.TryGetComponent(out SharedHandsComponent? hands))
return;
if (!hands.TryGetSwapHandsResult(out var nextHand))
return;
EntityManager.RaisePredictiveEvent(new RequestSetHandEvent(nextHand));
}
private bool DropPressed(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
if (session == null)
return false;
var player = session.AttachedEntity;
if (player == null)
return false;
if (!player.TryGetComponent(out SharedHandsComponent? hands))
return false;
var activeHand = hands.ActiveHand;
if (activeHand == null)
return false;
EntityManager.RaisePredictiveEvent(new RequestDropHeldEntityEvent(activeHand, coords));
return true;
}
protected override void HandleContainerModified(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args)
{
component.HandsModified();
}
}
}

View File

@@ -0,0 +1,96 @@
using Content.Shared.Hands.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.ResourceManagement;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using System;
using System.Collections.Generic;
namespace Content.Client.Hands
{
[UsedImplicitly]
public class HandsVisualizer : AppearanceVisualizer
{
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
if (!component.Owner.TryGetComponent<ISpriteComponent>(out var sprite)) return;
if (!component.TryGetData(HandsVisuals.VisualState, out HandsVisualState visualState)) return;
foreach (HandLocation location in Enum.GetValues(typeof(HandLocation)))
{
var layerKey = LocationToLayerKey(location);
if (sprite.LayerMapTryGet(layerKey, out var layer))
{
sprite.RemoveLayer(layer);
sprite.LayerMapRemove(layer);
}
}
var resourceCache = IoCManager.Resolve<IResourceCache>();
var hands = visualState.Hands;
foreach (var hand in hands)
{
var rsi = resourceCache.GetResource<RSIResource>(SharedSpriteComponent.TextureRoot / hand.RsiPath).RSI;
var layerKey = LocationToLayerKey(hand.Location);
sprite.LayerMapReserveBlank(layerKey);
var layer = sprite.LayerMapGet(layerKey);
sprite.LayerSetVisible(layer, true);
sprite.LayerSetRSI(layer, rsi);
sprite.LayerSetColor(layer, hand.Color);
var state = $"inhand-{hand.Location.ToString().ToLowerInvariant()}";
if (hand.EquippedPrefix != null)
state = $"{hand.EquippedPrefix}-" + state;
sprite.LayerSetState(layer, state);
}
}
private string LocationToLayerKey(HandLocation location)
{
return location.ToString();
}
}
public enum HandsVisuals : byte
{
VisualState
}
public class HandsVisualState
{
public List<HandVisualState> Hands { get; } = new();
public HandsVisualState(List<HandVisualState> hands)
{
Hands = hands;
}
}
public class HandVisualState
{
public string RsiPath { get; }
public string? EquippedPrefix { get; }
public HandLocation Location { get; }
public Color Color { get; }
public HandVisualState(string rsiPath, string? equippedPrefix, HandLocation location, Color color)
{
RsiPath = rsiPath;
EquippedPrefix = equippedPrefix;
Location = location;
Color = color;
}
}
}

View File

@@ -10,18 +10,13 @@ namespace Content.Client.Items.Components
[ComponentReference(typeof(SharedItemComponent))]
public class ItemComponent : SharedItemComponent
{
public override bool TryPutInHand(IEntity user)
{
return false;
}
protected override void OnEquippedPrefixChange()
{
if (!Owner.TryGetContainer(out var container))
return;
if (container.Owner.TryGetComponent(out HandsComponent? hands))
hands.RefreshInHands();
hands.UpdateHandVisualizer();
}
}
}

View File

@@ -1,4 +1,4 @@
using Content.Client.Examine;
using Content.Client.Examine;
using Content.Client.Items.UI;
using Content.Client.Storage;
using Content.Client.Verbs;
@@ -104,6 +104,7 @@ namespace Content.Client.Items.Managers
}
if (entity == null ||
entity.Deleted ||
!entity.TryGetComponent(out ItemCooldownComponent? cooldown) ||
!cooldown.CooldownStart.HasValue ||
!cooldown.CooldownEnd.HasValue)

View File

@@ -1,6 +1,7 @@
using System;
using System;
using Content.Client.CombatMode;
using Content.Client.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Weapons.Ranged.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
@@ -56,13 +57,12 @@ namespace Content.Client.Weapons.Ranged
}
var entity = _playerManager.LocalPlayer?.ControlledEntity;
if (entity == null || !entity.TryGetComponent(out HandsComponent? hands))
if (entity == null || !entity.TryGetComponent(out SharedHandsComponent? hands))
{
return;
}
var held = hands.ActiveHand;
if (held == null || !held.TryGetComponent(out ClientRangedWeaponComponent? weapon))
if (!hands.TryGetActiveHeldEntity(out var held) || !held.TryGetComponent(out ClientRangedWeaponComponent? weapon))
{
_blocked = true;
return;

View File

@@ -266,7 +266,7 @@ namespace Content.IntegrationTests.Tests.Buckle
Assert.True(buckle.Buckled);
// With items in all hands
foreach (var slot in hands.Hands)
foreach (var slot in hands.HandNames)
{
Assert.NotNull(hands.GetItem(slot));
}
@@ -288,7 +288,7 @@ namespace Content.IntegrationTests.Tests.Buckle
Assert.True(buckle.Buckled);
// Now with no item in any hand
foreach (var slot in hands.Hands)
foreach (var slot in hands.HandNames)
{
Assert.Null(hands.GetItem(slot));
}

View File

@@ -80,8 +80,9 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.ActionBlocking
// Test to ensure a player with 4 hands will still only have 2 hands cuffed
AddHand(cuffed.Owner);
AddHand(cuffed.Owner);
Assert.That(cuffed.CuffedHandCount, Is.EqualTo(2));
Assert.That(hands.Hands.Count(), Is.EqualTo(4));
Assert.That(hands.HandNames.Count(), Is.EqualTo(4));
// Test to give a player with 4 hands 2 sets of cuffs
cuffed.TryAddNewCuffs(human, secondCuffs);

View File

@@ -330,7 +330,7 @@ namespace Content.IntegrationTests.Tests.GameObjects.Components.Mobs
{
// drop the item, and the item actions should go away
serverPlayerEnt.GetComponent<HandsComponent>()
.Drop(serverFlashlight, serverPlayerEnt.Transform.Coordinates, false);
.TryDropEntity(serverFlashlight, serverPlayerEnt.Transform.Coordinates, false);
Assert.That(serverActionsComponent.ItemActionStates().ContainsKey(serverFlashlight.Uid), Is.False);
});

View File

@@ -11,6 +11,7 @@ using Content.Server.Interaction;
using Content.Server.Items;
using Content.Shared.Interaction;
using Content.Shared.Weapons.Melee;
using Content.Shared.Hands.Components;
namespace Content.IntegrationTests.Tests.Interaction.Click
{
@@ -66,7 +67,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand");
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, coords);
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
@@ -137,7 +138,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand");
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((1.9f, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
@@ -207,7 +208,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand");
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((InteractionSystem.InteractionRange - 0.1f, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
@@ -277,7 +278,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand");
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, new MapCoordinates((InteractionSystem.InteractionRange, 0), mapId));
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();
@@ -349,7 +350,7 @@ namespace Content.IntegrationTests.Tests.Interaction.Click
server.Assert(() =>
{
user = entityManager.SpawnEntity(null, coords);
user.EnsureComponent<HandsComponent>().AddHand("hand");
user.EnsureComponent<HandsComponent>().AddHand("hand", HandLocation.Left);
target = entityManager.SpawnEntity(null, coords);
item = entityManager.SpawnEntity(null, coords);
item.EnsureComponent<ItemComponent>();

View File

@@ -20,8 +20,7 @@ namespace Content.Server.AI.Operators.Inventory
/// <returns></returns>
public override Outcome Execute(float frameTime)
{
if (!_owner.TryGetComponent(out HandsComponent? handsComponent) ||
!handsComponent.TryHand(_entity, out _))
if (!_owner.TryGetComponent(out HandsComponent? handsComponent))
{
return Outcome.Failed;
}

View File

@@ -159,7 +159,7 @@ namespace Content.Server.Access.Components
return;
}
if (!hands.Drop(hands.ActiveHand, container))
if (!hands.TryPutHandIntoContainer(hands.ActiveHand, container))
{
Owner.PopupMessage(user, Loc.GetString("access-id-card-console-component-cannot-let-go-error"));
return;
@@ -216,11 +216,11 @@ namespace Content.Server.Access.Components
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if(!eventArgs.User.TryGetComponent(out ActorComponent? actor))
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
{
return;
}
if(!Powered) return;
if (!Powered) return;
UserInterface?.Open(actor.PlayerSession);
}

View File

@@ -193,10 +193,7 @@ namespace Content.Server.Atmos.Piping.Binary.EntitySystems
if (!args.User.InRangeUnobstructed(canister, SharedInteractionSystem.InteractionRange, popup: true))
return;
if (!hands.Drop(args.Used, canister.Transform.Coordinates))
return;
if (!container.Insert(args.Used))
if (!hands.TryPutEntityIntoContainer(args.Used, container))
return;
args.Handled = true;

View File

@@ -113,7 +113,7 @@ namespace Content.Server.Cuffs.Components
}
Container.Insert(handcuff);
CanStillInteract = Owner.TryGetComponent(out HandsComponent? ownerHands) && ownerHands.Hands.Count() > CuffedHandCount;
CanStillInteract = Owner.TryGetComponent(out HandsComponent? ownerHands) && ownerHands.HandNames.Count() > CuffedHandCount;
OnCuffedStateChanged?.Invoke();
UpdateAlert();
@@ -136,7 +136,7 @@ namespace Content.Server.Cuffs.Components
if (!Owner.TryGetComponent(out HandsComponent? handsComponent)) return;
var itemCount = handsComponent.GetAllHeldItems().Count();
var freeHandCount = handsComponent.Hands.Count() - CuffedHandCount;
var freeHandCount = handsComponent.HandNames.Count() - CuffedHandCount;
if (freeHandCount < itemCount)
{
@@ -279,7 +279,7 @@ namespace Content.Server.Cuffs.Components
}
}
CanStillInteract = Owner.TryGetComponent(out HandsComponent? handsComponent) && handsComponent.Hands.Count() > CuffedHandCount;
CanStillInteract = Owner.TryGetComponent(out HandsComponent? handsComponent) && handsComponent.HandNames.Count() > CuffedHandCount;
OnCuffedStateChanged?.Invoke();
UpdateAlert();
Dirty();

View File

@@ -1,6 +1,7 @@
#nullable enable
#nullable enable
using Content.Server.Cuffs.Components;
using Content.Server.Hands.Components;
using Content.Shared.Hands.Components;
using Content.Shared.Cuffs;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;

View File

@@ -201,7 +201,7 @@ namespace Content.Server.GameTicking
foreach (var (hand, prototype) in inhand)
{
var inhandEntity = _entityManager.SpawnEntity(prototype, entity.Transform.Coordinates);
handsComponent.PutInHand(inhandEntity.GetComponent<ItemComponent>(), hand);
handsComponent.TryPickupEntity(hand, inhandEntity, checkActionBlocker: false);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -21,7 +21,7 @@ namespace Content.Server.Hands.Components
/// <summary>
/// The hands in this component.
/// </summary>
IEnumerable<string> Hands { get; }
IEnumerable<string> HandNames { get; }
/// <summary>
/// The hand name of the currently active hand.
@@ -61,18 +61,6 @@ namespace Content.Server.Hands.Components
/// <returns>True if the item was inserted, false otherwise.</returns>
bool PutInHand(ItemComponent item, bool mobCheck = true);
/// <summary>
/// Puts an item into a specific hand.
/// </summary>
/// <param name="item">The item to put in the hand.</param>
/// <param name="index">The name of the hand to put the item into.</param>
/// <param name="fallback">
/// If true and the provided hand is full, the method will fall back to <see cref="PutInHand(ItemComponent)" />
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// </param>
/// <returns>True if the item was inserted into a hand, false otherwise.</returns>
bool PutInHand(ItemComponent item, string index, bool fallback=true, bool mobCheck = true);
/// <summary>
/// Checks to see if an item can be put in any hand.
/// </summary>
@@ -81,43 +69,19 @@ namespace Content.Server.Hands.Components
/// <returns>True if the item can be inserted, false otherwise.</returns>
bool CanPutInHand(ItemComponent item, bool mobCheck = true);
/// <summary>
/// Checks to see if an item can be put in the specified hand.
/// </summary>
/// <param name="item">The item to check for.</param>
/// <param name="index">The name for the hand to check for.</param>
/// <param name="mobCheck">Whether to perform an ActionBlocker check to the entity.</param>
/// <returns>True if the item can be inserted, false otherwise.</returns>
bool CanPutInHand(ItemComponent item, string index, bool mobCheck = true);
/// <summary>
/// Finds the hand slot holding the specified entity, if any.
/// </summary>
/// <param name="entity">The entity to look for in our hands.</param>
/// <param name="handName">
/// The name of the hand slot if the entity is indeed held,
/// <see langword="null" /> otherwise.
/// </param>
/// <returns>
/// true if the entity is held, false otherwise
/// </returns>
bool TryHand(IEntity entity, [NotNullWhen(true)] out string? handName);
/// <summary>
/// Drops the item contained in the slot to the same position as our entity.
/// </summary>
/// <param name="slot">The slot of which to drop to drop the item.</param>
/// <param name="mobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <param name="mobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param
/// <returns>True on success, false if something blocked the drop.</returns>
bool Drop(string slot, bool mobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool Drop(string slot, bool mobChecks = true, bool intentional = true);
/// <summary>
/// Drops an item held by one of our hand slots to the same position as our owning entity.
/// </summary>
/// <param name="entity">The item to drop.</param>
/// <param name="mobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <returns>True on success, false if something blocked the drop.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown if <see cref="entity"/> is null.
@@ -125,7 +89,7 @@ namespace Content.Server.Hands.Components
/// <exception cref="ArgumentException">
/// Thrown if <see cref="entity"/> is not actually held in any hand.
/// </exception>
bool Drop(IEntity entity, bool mobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool Drop(IEntity entity, bool mobChecks = true, bool intentional = true);
/// <summary>
/// Drops the item in a slot.
@@ -133,9 +97,8 @@ namespace Content.Server.Hands.Components
/// <param name="slot">The slot to drop the item from.</param>
/// <param name="coords"></param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <returns>True if an item was dropped, false otherwise.</returns>
bool Drop(string slot, EntityCoordinates coords, bool doMobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool TryDropHand(string slot, EntityCoordinates coords, bool doMobChecks = true, bool intentional = true);
/// <summary>
/// Drop the specified entity in our hands to a certain position.
@@ -147,7 +110,6 @@ namespace Content.Server.Hands.Components
/// <param name="entity">The entity to drop, must be held in one of the hands.</param>
/// <param name="coords">The coordinates to drop the entity at.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <returns>
/// True if the drop succeeded,
/// false if it failed (due to failing to eject from our hand slot, etc...)
@@ -158,22 +120,21 @@ namespace Content.Server.Hands.Components
/// <exception cref="ArgumentException">
/// Thrown if <see cref="entity"/> is not actually held in any hand.
/// </exception>
bool Drop(IEntity entity, EntityCoordinates coords, bool doMobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool TryDropEntity(IEntity entity, EntityCoordinates coords, bool doMobChecks = true, bool intentional = true);
/// <summary>
/// Drop the item contained in a slot into another container.
/// </summary>
/// <param name="slot">The slot of which to drop the entity.</param>
/// <param name="targetContainer">The container to drop into.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop(IEntity)"/> for the mob or not.</param>
/// <returns>True on success, false if something was blocked (insertion or removal).</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if dry-run checks reported OK to remove and insert,
/// but practical remove or insert returned false anyways.
/// This is an edge-case that is currently unhandled.
/// </exception>
bool Drop(string slot, BaseContainer targetContainer, bool doMobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool TryPutHandIntoContainer(string slot, BaseContainer targetContainer, bool doMobChecks = true);
/// <summary>
/// Drops an item in one of the hands into a container.
@@ -181,7 +142,6 @@ namespace Content.Server.Hands.Components
/// <param name="entity">The item to drop.</param>
/// <param name="targetContainer">The container to drop into.</param>
/// <param name="doMobChecks">Whether to check the <see cref="ActionBlockerSystem.CanDrop()"/> for the mob or not.</param>
/// <param name="doDropInteraction">Whether to perform Dropped interactions.</param>
/// <returns>True on success, false if something was blocked (insertion or removal).</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if dry-run checks reported OK to remove and insert,
@@ -194,7 +154,7 @@ namespace Content.Server.Hands.Components
/// <exception cref="ArgumentException">
/// Thrown if <see cref="entity"/> is not actually held in any hand.
/// </exception>
bool Drop(IEntity entity, BaseContainer targetContainer, bool doMobChecks = true, bool doDropInteraction = true, bool intentional = true);
bool Drop(IEntity entity, BaseContainer targetContainer, bool doMobChecks = true);
/// <summary>
/// Checks whether the item in the specified hand can be dropped.
@@ -213,7 +173,7 @@ namespace Content.Server.Hands.Components
/// <exception cref="InvalidOperationException">
/// Thrown if a hand with specified name already exists.
/// </exception>
void AddHand(string name);
void AddHand(string name, HandLocation handLocation);
/// <summary>
/// Removes a hand from this hands component.
@@ -230,7 +190,5 @@ namespace Content.Server.Hands.Components
/// <param name="name">The hand name to check.</param>
/// <returns>True if the hand exists, false otherwise.</returns>
bool HasHand(string name);
void HandleSlotModifiedMaybe(ContainerModifiedMessage message);
}
}

View File

@@ -9,10 +9,9 @@ using Content.Server.Stack;
using Content.Server.Storage.Components;
using Content.Server.Throwing;
using Content.Shared.Examine;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Input;
using Content.Shared.Interaction;
using Content.Shared.Movement.Components;
using Content.Shared.Notification;
using Content.Shared.Notification.Managers;
using JetBrains.Annotations;
using Robust.Server.Player;
@@ -28,22 +27,15 @@ using static Content.Shared.Inventory.EquipmentSlotDefines;
namespace Content.Server.Hands
{
[UsedImplicitly]
internal sealed class HandsSystem : EntitySystem
internal sealed class HandsSystem : SharedHandsSystem
{
private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntRemovedFromContainerMessage>(HandleContainerModified);
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleContainerModified);
SubscribeLocalEvent<HandsComponent, ExaminedEvent>(HandleExamined);
CommandBinds.Builder
.Bind(ContentKeyFunctions.SwapHands, InputCmdHandler.FromDelegate(HandleSwapHands))
.Bind(ContentKeyFunctions.Drop, new PointerInputCmdHandler(HandleDrop))
.Bind(ContentKeyFunctions.ActivateItemInHand, InputCmdHandler.FromDelegate(HandleActivateItem))
.Bind(ContentKeyFunctions.ThrowItemInHand, new PointerInputCmdHandler(HandleThrowItem))
.Bind(ContentKeyFunctions.SmartEquipBackpack, InputCmdHandler.FromDelegate(HandleSmartEquipBackpack))
@@ -51,7 +43,6 @@ namespace Content.Server.Hands
.Register<HandsSystem>();
}
/// <inheritdoc />
public override void Shutdown()
{
base.Shutdown();
@@ -59,14 +50,6 @@ namespace Content.Server.Hands
CommandBinds.Unregister<HandsSystem>();
}
private static void HandleContainerModified(ContainerModifiedMessage args)
{
if (args.Container.Owner.TryGetComponent(out IHandsComponent? handsComponent))
{
handsComponent.HandleSlotModifiedMaybe(args);
}
}
//TODO: Actually shows all items/clothing/etc.
private void HandleExamined(EntityUid uid, HandsComponent component, ExaminedEvent args)
{
@@ -76,142 +59,69 @@ namespace Content.Server.Hands
}
}
private static bool TryGetAttachedComponent<T>(IPlayerSession? session, [NotNullWhen(true)] out T? component)
where T : Component
protected override void HandleContainerModified(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args)
{
component = default;
var ent = session?.AttachedEntity;
if (ent == null || !ent.IsValid() || !ent.TryGetComponent(out T? comp))
{
return false;
}
component = comp;
return true;
component.Dirty();
}
private static void HandleSwapHands(ICommonSession? session)
private bool TryGetHandsComp(ICommonSession? session, [NotNullWhen(true)] out SharedHandsComponent? hands)
{
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent? handsComp))
{
return;
}
hands = default;
var interactionSystem = Get<InteractionSystem>();
var oldItem = handsComp.GetActiveHand;
handsComp.SwapHands();
var newItem = handsComp.GetActiveHand;
if (oldItem != null)
{
interactionSystem.HandDeselectedInteraction(handsComp.Owner, oldItem.Owner);
}
if (newItem != null)
{
interactionSystem.HandSelectedInteraction(handsComp.Owner, newItem.Owner);
}
}
private bool HandleDrop(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
var ent = ((IPlayerSession?) session)?.AttachedEntity;
if (ent == null || !ent.IsValid())
if (session is not IPlayerSession playerSession)
return false;
if (!ent.TryGetComponent(out HandsComponent? handsComp))
return false;
if (handsComp.ActiveHand == null || handsComp.GetActiveHand == null)
return false;
// It's important to note that the calculations are done in map coordinates (they're absolute).
// They're translated back to EntityCoordinates at the end.
var entMap = ent.Transform.MapPosition;
var targetPos = coords.ToMapPos(EntityManager);
var dropVector = targetPos - entMap.Position;
var targetVector = Vector2.Zero;
if (dropVector != Vector2.Zero)
{
var targetLength = MathF.Min(dropVector.Length, SharedInteractionSystem.InteractionRange - 0.001f); // InteractionRange is reduced due to InRange not dealing with floating point error
var newCoords = new MapCoordinates(dropVector.Normalized * targetLength + entMap.Position, entMap.MapId);
var rayLength = Get<SharedInteractionSystem>().UnobstructedDistance(entMap, newCoords, ignoredEnt: ent);
targetVector = dropVector.Normalized * rayLength;
}
var resultMapCoordinates = new MapCoordinates(entMap.Position + targetVector, entMap.MapId);
var resultEntCoordinates = EntityCoordinates.FromMap(coords.GetParent(EntityManager), resultMapCoordinates);
handsComp.Drop(handsComp.ActiveHand, resultEntCoordinates);
return true;
}
private static void HandleActivateItem(ICommonSession? session)
{
if (!TryGetAttachedComponent(session as IPlayerSession, out HandsComponent? handsComp))
return;
handsComp.ActivateItem();
}
private bool HandleThrowItem(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
var playerEnt = ((IPlayerSession?) session)?.AttachedEntity;
var playerEnt = playerSession?.AttachedEntity;
if (playerEnt == null || !playerEnt.IsValid())
return false;
if (!playerEnt.TryGetComponent(out HandsComponent? handsComp))
playerEnt.TryGetComponent(out hands);
return hands != null;
}
private void HandleActivateItem(ICommonSession? session)
{
if (!TryGetHandsComp(session, out var hands))
return;
hands.UseActiveHeldEntity();
}
private bool HandleThrowItem(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
if (session is not IPlayerSession playerSession)
return false;
if (handsComp.ActiveHand == null || !handsComp.CanDrop(handsComp.ActiveHand))
var playerEnt = playerSession.AttachedEntity;
if (playerEnt == null || !playerEnt.IsValid() || !playerEnt.TryGetComponent(out SharedHandsComponent? hands))
return false;
var throwEnt = handsComp.GetItem(handsComp.ActiveHand)?.Owner;
if (throwEnt == null)
if (!hands.TryGetActiveHeldEntity(out var throwEnt))
return false;
if (!handsComp.ThrowItem())
if (!Get<InteractionSystem>().TryThrowInteraction(hands.Owner, throwEnt))
return false;
// throw the item, split off from a stack if it's meant to be thrown individually
if (!throwEnt.TryGetComponent(out StackComponent? stackComp) || stackComp.Count < 2 || !stackComp.ThrowIndividually)
if (throwEnt.TryGetComponent(out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
{
handsComp.Drop(handsComp.ActiveHand);
}
else
{
var splitStack = Get<StackSystem>().Split(throwEnt.Uid, stackComp, 1, playerEnt.Transform.Coordinates);
var splitStack = Get<StackSystem>().Split(throwEnt.Uid, stack, 1, playerEnt.Transform.Coordinates);
if (splitStack == null)
return false;
throwEnt = splitStack;
}
else if (!hands.TryDropEntityToFloor(throwEnt))
return false;
var direction = coords.ToMapPos(EntityManager) - playerEnt.Transform.WorldPosition;
if (direction == Vector2.Zero) return true;
if (direction == Vector2.Zero)
return true;
direction = direction.Normalized * MathF.Min(direction.Length, 8.0f);
var yeet = direction * ThrowForce * 15;
// Softer yeet in weightlessness
if (playerEnt.IsWeightless())
{
throwEnt.TryThrow(yeet / 4, playerEnt, 10.0f);
}
else
{
throwEnt.TryThrow(yeet, playerEnt);
}
var throwVec = direction.Normalized * MathF.Min(direction.Length, hands.ThrowRange) * hands.ThrowForceMultiplier;
throwEnt.TryThrow(throwVec, playerEnt);
return true;
}
@@ -228,26 +138,26 @@ namespace Content.Server.Hands
private void HandleSmartEquip(ICommonSession? session, Slots equipmentSlot)
{
var plyEnt = ((IPlayerSession?) session)?.AttachedEntity;
if (session is not IPlayerSession playerSession)
return;
var plyEnt = playerSession.AttachedEntity;
if (plyEnt == null || !plyEnt.IsValid())
return;
if (!plyEnt.TryGetComponent(out HandsComponent? handsComp) ||
!plyEnt.TryGetComponent(out InventoryComponent? inventoryComp))
if (!plyEnt.TryGetComponent(out SharedHandsComponent? hands) ||
!plyEnt.TryGetComponent(out InventoryComponent? inventory))
return;
if (!inventoryComp.TryGetSlotItem(equipmentSlot, out ItemComponent? equipmentItem)
|| !equipmentItem.Owner.TryGetComponent<ServerStorageComponent>(out var storageComponent))
if (!inventory.TryGetSlotItem(equipmentSlot, out ItemComponent? equipmentItem) ||
!equipmentItem.Owner.TryGetComponent(out ServerStorageComponent? storageComponent))
{
plyEnt.PopupMessage(Loc.GetString("hands-system-missing-equipment-slot",
("slotName", SlotNames[equipmentSlot].ToLower())));
plyEnt.PopupMessage(Loc.GetString("hands-system-missing-equipment-slot", ("equipment", SlotNames[equipmentSlot].ToLower())));
return;
}
var heldItem = handsComp.GetItem(handsComp.ActiveHand)?.Owner;
if (heldItem != null)
if (hands.ActiveHandIsHoldingEntity())
{
storageComponent.PlayerInsertHeldEntity(plyEnt);
}
@@ -255,14 +165,16 @@ namespace Content.Server.Hands
{
if (storageComponent.StoredEntities.Count == 0)
{
plyEnt.PopupMessage(Loc.GetString("hands-system-empty-equipment-slot",
("slotName", SlotNames[equipmentSlot].ToLower())));
plyEnt.PopupMessage(Loc.GetString("hands-system-empty-equipment-slot", ("equipment", SlotNames[equipmentSlot].ToLower())));
}
else
{
var lastStoredEntity = Enumerable.Last(storageComponent.StoredEntities);
if (storageComponent.Remove(lastStoredEntity))
handsComp.PutInHandOrDrop(lastStoredEntity.GetComponent<ItemComponent>());
{
if (!hands.TryPickupEntityToActiveHand(lastStoredEntity))
lastStoredEntity.Transform.Coordinates = plyEnt.Transform.Coordinates;
}
}
}
}

View File

@@ -575,7 +575,7 @@ namespace Content.Server.Interaction
/// Calls EquippedHand on all components that implement the IEquippedHand interface
/// on an item.
/// </summary>
public void EquippedHandInteraction(IEntity user, IEntity item, SharedHand hand)
public void EquippedHandInteraction(IEntity user, IEntity item, HandState hand)
{
var equippedHandMessage = new EquippedHandEvent(user, item, hand);
RaiseLocalEvent(item.Uid, equippedHandMessage);
@@ -594,7 +594,7 @@ namespace Content.Server.Interaction
/// Calls UnequippedHand on all components that implement the IUnequippedHand interface
/// on an item.
/// </summary>
public void UnequippedHandInteraction(IEntity user, IEntity item, SharedHand hand)
public void UnequippedHandInteraction(IEntity user, IEntity item, HandState hand)
{
var unequippedHandMessage = new UnequippedHandEvent(user, item, hand);
RaiseLocalEvent(item.Uid, unequippedHandMessage);
@@ -761,7 +761,7 @@ namespace Content.Server.Interaction
var ev = new WideAttackEvent(item, user, coordinates);
RaiseLocalEvent(item.Uid, ev, false);
if(ev.Handled)
if (ev.Handled)
return;
}
else
@@ -769,7 +769,7 @@ namespace Content.Server.Interaction
var ev = new ClickAttackEvent(item, user, coordinates, targetUid);
RaiseLocalEvent(item.Uid, ev, false);
if(ev.Handled)
if (ev.Handled)
return;
}
}
@@ -785,7 +785,7 @@ namespace Content.Server.Interaction
// TODO: Make this saner?
// Attempt to do unarmed combat. We don't check for handled just because at this point it doesn't matter.
if(wideAttack)
if (wideAttack)
RaiseLocalEvent(user.Uid, new WideAttackEvent(user, user, coordinates), false);
else
RaiseLocalEvent(user.Uid, new ClickAttackEvent(user, user, coordinates, targetUid), false);

View File

@@ -522,7 +522,7 @@ namespace Content.Server.Inventory.Components
var activeItem = hands.GetActiveHand;
if (activeHand != null && activeItem != null && activeItem.Owner.TryGetComponent(out ItemComponent? clothing))
{
hands.Drop(activeHand, doDropInteraction: false);
hands.TryDropNoInteraction();
if (!Equip(msg.Inventoryslot, clothing, true, out var reason))
{
hands.PutInHand(clothing);

View File

@@ -31,23 +31,6 @@ namespace Content.Server.Items
}
}
public override bool TryPutInHand(IEntity user)
{
if (!CanPickup(user))
return false;
if (!user.TryGetComponent(out IHandsComponent? hands))
return false;
var activeHand = hands.ActiveHand;
if (activeHand == null)
return false;
hands.PutInHand(this, activeHand, false);
return true;
}
[Verb]
public sealed class PickUpVerb : Verb<ItemComponent>
{
@@ -74,3 +57,4 @@ namespace Content.Server.Items
}
}
}

View File

@@ -10,6 +10,7 @@ using Content.Server.Items;
using Content.Server.PDA.Managers;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Notification.Managers;
@@ -123,19 +124,21 @@ namespace Content.Server.PDA
case PDAUplinkBuyListingMessage buyMsg:
{
if (message.Session.AttachedEntity == null)
var player = message.Session.AttachedEntity;
if (player == null)
break;
if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ItemId,
message.Session.AttachedEntity.Transform.Coordinates, out var entity))
player.Transform.Coordinates, out var entity))
{
SendNetworkMessage(new PDAUplinkInsufficientFundsMessage(), message.Session.ConnectedClient);
break;
}
HandsComponent.PutInHandOrDropStatic(
message.Session.AttachedEntity,
entity.GetComponent<ItemComponent>());
if (!player.TryGetComponent(out HandsComponent? hands) || !entity.TryGetComponent(out ItemComponent? item))
break;
hands.PutInHandOrDrop(item);
SendNetworkMessage(new PDAUplinkBuySuccessMessage(), message.Session.ConnectedClient);
break;

View File

@@ -128,7 +128,7 @@ namespace Content.Server.Strip
return dictionary;
}
foreach (var hand in hands.Hands)
foreach (var hand in hands.HandNames)
{
dictionary[hand] = hands.GetItem(hand)?.Owner.Name ?? "None";
}
@@ -243,7 +243,7 @@ namespace Content.Server.Strip
return false;
}
if (!hands.CanPutInHand(item, hand, false))
if (!hands.CanPickupEntity(hand, item.Owner, checkActionBlocker: false))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-put-message",("owner", Owner)));
return false;
@@ -267,8 +267,8 @@ namespace Content.Server.Strip
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result != DoAfterStatus.Finished) return;
userHands.Drop(hand, false);
hands.PutInHand(item!, hand, false, false);
userHands.Drop(hand);
hands.TryPickupEntity(hand, item!.Owner, checkActionBlocker: false);
UpdateSubscribed();
}

View File

@@ -42,7 +42,7 @@ namespace Content.Shared.Actions.Components
/// <summary>
/// hand it's currently in, null if not in a hand.
/// </summary>
public SharedHand? InHand { get; private set; }
public HandState? InHand { get; private set; }
/// <summary>
/// Entity currently holding this in hand or equip slot. Null if not held.

File diff suppressed because it is too large Load Diff

View File

@@ -23,12 +23,12 @@ namespace Content.Shared.Hands
public class EquippedHandEventArgs : UserEventArgs
{
public EquippedHandEventArgs(IEntity user, SharedHand hand) : base(user)
public EquippedHandEventArgs(IEntity user, HandState hand) : base(user)
{
Hand = hand;
}
public SharedHand Hand { get; }
public HandState Hand { get; }
}
/// <summary>
@@ -50,9 +50,9 @@ namespace Content.Shared.Hands
/// <summary>
/// Hand that the item was placed into.
/// </summary>
public SharedHand Hand { get; }
public HandState Hand { get; }
public EquippedHandEvent(IEntity user, IEntity equipped, SharedHand hand)
public EquippedHandEvent(IEntity user, IEntity equipped, HandState hand)
{
User = user;
Equipped = equipped;

View File

@@ -22,12 +22,12 @@ namespace Content.Shared.Hands
public class UnequippedHandEventArgs : UserEventArgs
{
public UnequippedHandEventArgs(IEntity user, SharedHand hand) : base(user)
public UnequippedHandEventArgs(IEntity user, HandState hand) : base(user)
{
Hand = hand;
}
public SharedHand Hand { get; }
public HandState Hand { get; }
}
/// <summary>
@@ -49,9 +49,9 @@ namespace Content.Shared.Hands
/// <summary>
/// Hand that the item is removed from.
/// </summary>
public SharedHand Hand { get; }
public HandState Hand { get; }
public UnequippedHandEvent(IEntity user, IEntity unequipped, SharedHand hand)
public UnequippedHandEvent(IEntity user, IEntity unequipped, HandState hand)
{
User = user;
Unequipped = unequipped;

View File

@@ -0,0 +1,79 @@
using Content.Shared.Hands.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
using System;
namespace Content.Shared.Hands
{
public abstract class SharedHandsSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedHandsComponent, EntRemovedFromContainerMessage>(HandleContainerModified);
SubscribeLocalEvent<SharedHandsComponent, EntInsertedIntoContainerMessage>(HandleContainerModified);
SubscribeLocalEvent<RequestSetHandEvent>(HandleSetHand);
SubscribeNetworkEvent<RequestSetHandEvent>(HandleSetHand);
SubscribeLocalEvent<RequestDropHeldEntityEvent>(HandleDrop);
SubscribeNetworkEvent<RequestDropHeldEntityEvent>(HandleDrop);
}
private void HandleSetHand(RequestSetHandEvent msg, EntitySessionEventArgs eventArgs)
{
var entity = eventArgs.SenderSession?.AttachedEntity;
if (entity == null || !entity.TryGetComponent(out SharedHandsComponent? hands))
return;
hands.ActiveHand = msg.HandName;
}
private void HandleDrop(RequestDropHeldEntityEvent msg, EntitySessionEventArgs eventArgs)
{
var entity = eventArgs.SenderSession?.AttachedEntity;
if (entity == null || !entity.TryGetComponent(out SharedHandsComponent? hands))
return;
hands.TryDropHand(msg.HandName, msg.DropTarget);
}
protected abstract void HandleContainerModified(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args);
}
[Serializable, NetSerializable]
public class RequestSetHandEvent : EntityEventArgs
{
/// <summary>
/// The hand to be swapped to.
/// </summary>
public string HandName { get; }
public RequestSetHandEvent(string handName)
{
HandName = handName;
}
}
[Serializable, NetSerializable]
public class RequestDropHeldEntityEvent : EntityEventArgs
{
/// <summary>
/// The hand to drop from.
/// </summary>
public string HandName { get; }
public EntityCoordinates DropTarget { get; }
public RequestDropHeldEntityEvent(string handName, EntityCoordinates dropTarget)
{
HandName = handName;
DropTarget = dropTarget;
}
}
}

View File

@@ -1,6 +1,7 @@
#nullable enable
using System;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Inventory;
@@ -136,14 +137,22 @@ namespace Content.Shared.Item
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
return TryPutInHand(eventArgs.User);
}
var user = eventArgs.User;
/// <summary>
/// Tries to put this item in a player's hands.
/// TODO: Move server implementation here once hands are in shared.
/// </summary>
public abstract bool TryPutInHand(IEntity user);
if (!CanPickup(user))
return false;
if (!user.TryGetComponent(out SharedHandsComponent? hands))
return false;
var activeHand = hands.ActiveHand;
if (activeHand == null)
return false;
hands.TryPickupEntityToActiveHand(Owner);
return true;
}
protected virtual void OnEquippedPrefixChange() { }

View File

@@ -207,6 +207,7 @@
fireStackAlternateState: 3
- type: CreamPiedVisualizer
state: creampie_human
- type: HandsVisualizer
- type: CombatMode
- type: Climbing
- type: Cuffable