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

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