hands ECS (#7081)
This commit is contained in:
@@ -1,19 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.Act;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Pulling;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Body.Part;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Pulling.Components;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Player;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
|
||||
namespace Content.Server.Hands.Components
|
||||
{
|
||||
@@ -24,9 +11,6 @@ namespace Content.Server.Hands.Components
|
||||
#pragma warning restore 618
|
||||
{
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entities = default!;
|
||||
|
||||
#region Pull/Disarm
|
||||
|
||||
void IBodyPartAdded.BodyPartAdded(BodyPartAddedEventArgs args)
|
||||
{
|
||||
@@ -43,7 +27,7 @@ namespace Content.Server.Hands.Components
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
AddHand(args.Slot, location);
|
||||
_entitySystemManager.GetEntitySystem<SharedHandsSystem>().AddHand(Owner, args.Slot, location);
|
||||
}
|
||||
|
||||
void IBodyPartRemoved.BodyPartRemoved(BodyPartRemovedEventArgs args)
|
||||
@@ -51,94 +35,8 @@ namespace Content.Server.Hands.Components
|
||||
if (args.Part.PartType != BodyPartType.Hand)
|
||||
return;
|
||||
|
||||
RemoveHand(args.Slot);
|
||||
_entitySystemManager.GetEntitySystem<SharedHandsSystem>().RemoveHand(Owner, args.Slot);
|
||||
}
|
||||
|
||||
public bool BreakPulls()
|
||||
{
|
||||
// What is this API??
|
||||
// I just wanted to do actions not deal with this shit...
|
||||
if (!_entities.TryGetComponent(Owner, out SharedPullerComponent? puller)
|
||||
|| puller.Pulling is not {Valid: true} pulling || !_entities.TryGetComponent(puller.Pulling.Value, out SharedPullableComponent? pullable))
|
||||
return false;
|
||||
|
||||
return _entitySystemManager.GetEntitySystem<PullingSystem>().TryStopPull(pullable);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Old public methods
|
||||
|
||||
public IEnumerable<string> HandNames => Hands.Select(h => h.Name);
|
||||
|
||||
public int Count => Hands.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of all hand names, with the active hand being first.
|
||||
/// </summary>
|
||||
public IEnumerable<string> ActivePriorityEnumerable()
|
||||
{
|
||||
if (ActiveHand != null)
|
||||
yield return ActiveHand;
|
||||
|
||||
foreach (var hand in Hands)
|
||||
{
|
||||
if (hand.Name == ActiveHand)
|
||||
continue;
|
||||
|
||||
yield return hand.Name;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the ItemComponent on the entity held by a hand.
|
||||
/// </summary>
|
||||
public SharedItemComponent? GetItem(string handName)
|
||||
{
|
||||
if (!TryGetHeldEntity(handName, out var heldEntity))
|
||||
return null;
|
||||
|
||||
_entities.TryGetComponent(heldEntity, out SharedItemComponent? item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the ItemComponent on the entity held by a hand.
|
||||
/// </summary>
|
||||
public bool TryGetItem(string handName, [NotNullWhen(true)] out SharedItemComponent? item)
|
||||
{
|
||||
item = null;
|
||||
|
||||
if (!TryGetHeldEntity(handName, out var heldEntity))
|
||||
return false;
|
||||
|
||||
return _entities.TryGetComponent(heldEntity, out item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the ItemComponent off the entity in the active hand.
|
||||
/// </summary>
|
||||
public SharedItemComponent? GetActiveHandItem
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!TryGetActiveHeldEntity(out var heldEntity))
|
||||
return null;
|
||||
|
||||
_entities.TryGetComponent(heldEntity, out SharedItemComponent? item);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<SharedItemComponent> GetAllHeldItems()
|
||||
{
|
||||
foreach (var entity in GetAllHeldEntities())
|
||||
{
|
||||
if (_entities.TryGetComponent(entity, out SharedItemComponent? item))
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.Hands.Systems
|
||||
{
|
||||
@@ -14,24 +13,15 @@ namespace Content.Server.Hands.Systems
|
||||
|
||||
public bool TrySpawnVirtualItemInHand(EntityUid blockingEnt, EntityUid user)
|
||||
{
|
||||
if (EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
|
||||
{
|
||||
foreach (var handName in hands.ActivePriorityEnumerable())
|
||||
{
|
||||
var hand = hands.GetHand(handName);
|
||||
if (hand.HeldEntity != null)
|
||||
continue;
|
||||
if (!_handsSystem.TryGetEmptyHand(user, out var hand))
|
||||
return false;
|
||||
|
||||
var pos = EntityManager.GetComponent<TransformComponent>(hands.Owner).Coordinates;
|
||||
var virtualItem = EntityManager.SpawnEntity("HandVirtualItem", pos);
|
||||
var virtualItemComp = EntityManager.GetComponent<HandVirtualItemComponent>(virtualItem);
|
||||
virtualItemComp.BlockingEntity = blockingEnt;
|
||||
_handsSystem.PutEntityIntoHand(user, hand, virtualItem, hands);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
var pos = EntityManager.GetComponent<TransformComponent>(user).Coordinates;
|
||||
var virtualItem = EntityManager.SpawnEntity("HandVirtualItem", pos);
|
||||
var virtualItemComp = EntityManager.GetComponent<HandVirtualItemComponent>(virtualItem);
|
||||
virtualItemComp.BlockingEntity = blockingEnt;
|
||||
_handsSystem.DoPickup(user, hand, virtualItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,20 +30,12 @@ namespace Content.Server.Hands.Systems
|
||||
/// </summary>
|
||||
public void DeleteInHandsMatching(EntityUid user, EntityUid matching)
|
||||
{
|
||||
if (!EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
|
||||
return;
|
||||
|
||||
foreach (var handName in hands.ActivePriorityEnumerable())
|
||||
foreach (var hand in _handsSystem.EnumerateHands(user))
|
||||
{
|
||||
var hand = hands.GetHand(handName);
|
||||
|
||||
if (!(hand.HeldEntity is { } heldEntity))
|
||||
continue;
|
||||
|
||||
if (EntityManager.TryGetComponent<HandVirtualItemComponent>(heldEntity, out var virt)
|
||||
&& virt.BlockingEntity == matching)
|
||||
if (TryComp(hand.HeldEntity, out HandVirtualItemComponent? virt) && virt.BlockingEntity == matching)
|
||||
{
|
||||
Delete(virt, user);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ using Content.Shared.Popups;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Input.Binding;
|
||||
@@ -29,6 +28,9 @@ using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Utility;
|
||||
using Content.Shared.Pulling.Components;
|
||||
using Content.Server.Pulling;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
|
||||
namespace Content.Server.Hands.Systems
|
||||
{
|
||||
@@ -43,27 +45,24 @@ namespace Content.Server.Hands.Systems
|
||||
[Dependency] private readonly StrippableSystem _strippableSystem = default!;
|
||||
[Dependency] private readonly SharedHandVirtualItemSystem _virtualSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
|
||||
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
||||
[Dependency] private readonly PullingSystem _pullingSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<HandsComponent, ExaminedEvent>(HandleExamined);
|
||||
SubscribeNetworkEvent<ActivateInHandMsg>(HandleActivateInHand);
|
||||
SubscribeNetworkEvent<ClientInteractUsingInHandMsg>(HandleInteractUsingInHand);
|
||||
SubscribeNetworkEvent<UseInHandMsg>(HandleUseInHand);
|
||||
SubscribeNetworkEvent<MoveItemFromHandMsg>(HandleMoveItemFromHand);
|
||||
SubscribeLocalEvent<HandsComponent, DisarmedEvent>(OnDisarmed, before: new[] { typeof(StunSystem) });
|
||||
|
||||
SubscribeLocalEvent<HandsComponent, PullAttemptMessage>(HandlePullAttempt);
|
||||
SubscribeLocalEvent<HandsComponent, PullStartedMessage>(HandlePullStarted);
|
||||
SubscribeLocalEvent<HandsComponent, PullStoppedMessage>(HandlePullStopped);
|
||||
|
||||
SubscribeLocalEvent<HandsComponent, EntRemovedFromContainerMessage>(HandleEntityRemoved);
|
||||
|
||||
SubscribeLocalEvent<HandsComponent, ComponentGetState>(GetComponentState);
|
||||
|
||||
CommandBinds.Builder
|
||||
.Bind(ContentKeyFunctions.ActivateItemInHand, InputCmdHandler.FromDelegate(s => HandleActivateItem(s)))
|
||||
.Bind(ContentKeyFunctions.AltActivateItemInHand, InputCmdHandler.FromDelegate(s => HandleActivateItem(s, true)))
|
||||
.Bind(ContentKeyFunctions.ThrowItemInHand, new PointerInputCmdHandler(HandleThrowItem))
|
||||
.Bind(ContentKeyFunctions.SmartEquipBackpack, InputCmdHandler.FromDelegate(HandleSmartEquipBackpack))
|
||||
.Bind(ContentKeyFunctions.SmartEquipBelt, InputCmdHandler.FromDelegate(HandleSmartEquipBelt))
|
||||
@@ -79,15 +78,19 @@ namespace Content.Server.Hands.Systems
|
||||
|
||||
private void GetComponentState(EntityUid uid, HandsComponent hands, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new HandsComponentState(hands.Hands, hands.ActiveHand);
|
||||
args.State = new HandsComponentState(hands);
|
||||
}
|
||||
|
||||
private void OnDisarmed(EntityUid uid, HandsComponent component, DisarmedEvent args)
|
||||
{
|
||||
if (args.Handled || component.BreakPulls())
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (component.ActiveHand == null || !component.Drop(component.ActiveHand, false))
|
||||
// Break any pulls
|
||||
if (TryComp(uid, out SharedPullerComponent? puller) && puller.Pulling is EntityUid pulled && TryComp(pulled, out SharedPullableComponent? pullable))
|
||||
_pullingSystem.TryStopPull(pullable);
|
||||
|
||||
if (_handsSystem.TryDrop(uid, component.ActiveHand!, null, checkActionBlocker: false))
|
||||
return;
|
||||
|
||||
var targetName = Name(args.Target);
|
||||
@@ -103,9 +106,9 @@ namespace Content.Server.Hands.Systems
|
||||
}
|
||||
|
||||
#region EntityInsertRemove
|
||||
public override void RemoveHeldEntityFromHand(EntityUid uid, Hand hand, SharedHandsComponent? hands = null)
|
||||
public override void DoDrop(EntityUid uid, Hand hand, bool doDropInteraction = true, SharedHandsComponent? hands = null)
|
||||
{
|
||||
base.RemoveHeldEntityFromHand(uid, hand, hands);
|
||||
base.DoDrop(uid, hand,doDropInteraction, hands);
|
||||
|
||||
// update gui of anyone stripping this entity.
|
||||
_strippableSystem.SendUpdate(uid);
|
||||
@@ -114,9 +117,9 @@ namespace Content.Server.Hands.Systems
|
||||
sprite.RenderOrder = EntityManager.CurrentTick.Value;
|
||||
}
|
||||
|
||||
public override void PutEntityIntoHand(EntityUid uid, Hand hand, EntityUid entity, SharedHandsComponent? hands = null)
|
||||
public override void DoPickup(EntityUid uid, Hand hand, EntityUid entity, SharedHandsComponent? hands = null)
|
||||
{
|
||||
base.PutEntityIntoHand(uid, hand, entity, hands);
|
||||
base.DoPickup(uid, hand, entity, hands);
|
||||
|
||||
// update gui of anyone stripping this entity.
|
||||
_strippableSystem.SendUpdate(uid);
|
||||
@@ -124,6 +127,7 @@ namespace Content.Server.Hands.Systems
|
||||
_logSystem.Add(LogType.Pickup, LogImpact.Low, $"{uid} picked up {entity}");
|
||||
}
|
||||
|
||||
|
||||
public override void PickupAnimation(EntityUid item, EntityCoordinates initialPosition, Vector2 finalPosition,
|
||||
EntityUid? exclude)
|
||||
{
|
||||
@@ -138,12 +142,10 @@ namespace Content.Server.Hands.Systems
|
||||
RaiseNetworkEvent(new PickupAnimationEvent(item, initialPosition, finalPosition), filter);
|
||||
}
|
||||
|
||||
protected override void HandleContainerRemoved(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args)
|
||||
private void HandleEntityRemoved(EntityUid uid, SharedHandsComponent component, EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (!Deleted(args.Entity) && TryComp(args.Entity, out HandVirtualItemComponent? @virtual))
|
||||
_virtualSystem.Delete(@virtual, uid);
|
||||
|
||||
base.HandleContainerRemoved(uid, component, args);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -154,11 +156,10 @@ namespace Content.Server.Hands.Systems
|
||||
return;
|
||||
|
||||
// Cancel pull if all hands full.
|
||||
if (component.Hands.All(hand => !hand.IsEmpty))
|
||||
if (!component.IsAnyHandFree())
|
||||
args.Cancelled = true;
|
||||
}
|
||||
|
||||
|
||||
private void HandlePullStarted(EntityUid uid, HandsComponent component, PullStartedMessage args)
|
||||
{
|
||||
if (args.Puller.Owner != uid)
|
||||
@@ -177,7 +178,7 @@ namespace Content.Server.Hands.Systems
|
||||
|
||||
// Try find hand that is doing this pull.
|
||||
// and clear it.
|
||||
foreach (var hand in component.Hands)
|
||||
foreach (var hand in component.Hands.Values)
|
||||
{
|
||||
if (hand.HeldEntity == null
|
||||
|| !TryComp(hand.HeldEntity, out HandVirtualItemComponent? virtualItem)
|
||||
@@ -191,34 +192,6 @@ namespace Content.Server.Hands.Systems
|
||||
#endregion
|
||||
|
||||
#region interactions
|
||||
private void HandleMoveItemFromHand(MoveItemFromHandMsg msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp(args.SenderSession.AttachedEntity, out SharedHandsComponent? hands))
|
||||
hands.TryMoveHeldEntityToActiveHand(msg.HandName);
|
||||
}
|
||||
private void HandleUseInHand(UseInHandMsg msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp(args.SenderSession.AttachedEntity, out SharedHandsComponent? hands))
|
||||
hands.ActivateItem();
|
||||
}
|
||||
private void HandleInteractUsingInHand(ClientInteractUsingInHandMsg msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp(args.SenderSession.AttachedEntity, out SharedHandsComponent? hands))
|
||||
hands.InteractHandWithActiveHand(msg.HandName);
|
||||
}
|
||||
|
||||
private void HandleActivateInHand(ActivateInHandMsg msg, EntitySessionEventArgs args)
|
||||
{
|
||||
if (TryComp(args.SenderSession.AttachedEntity, out SharedHandsComponent? hands))
|
||||
hands.ActivateHeldEntity(msg.HandName);
|
||||
}
|
||||
|
||||
private void HandleActivateItem(ICommonSession? session, bool altInteract = false)
|
||||
{
|
||||
if (TryComp(session?.AttachedEntity, out SharedHandsComponent? hands))
|
||||
hands.ActivateItem(altInteract);
|
||||
}
|
||||
|
||||
private bool HandleThrowItem(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
||||
{
|
||||
if (session is not IPlayerSession playerSession)
|
||||
@@ -228,20 +201,20 @@ namespace Content.Server.Hands.Systems
|
||||
!Exists(player) ||
|
||||
player.IsInContainer() ||
|
||||
!TryComp(player, out SharedHandsComponent? hands) ||
|
||||
!hands.TryGetActiveHeldEntity(out var throwEnt) ||
|
||||
hands.ActiveHandEntity is not EntityUid throwEnt ||
|
||||
!_actionBlockerSystem.CanThrow(player))
|
||||
return false;
|
||||
|
||||
if (EntityManager.TryGetComponent(throwEnt.Value, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
|
||||
if (EntityManager.TryGetComponent(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
|
||||
{
|
||||
var splitStack = _stackSystem.Split(throwEnt.Value, 1, EntityManager.GetComponent<TransformComponent>(player).Coordinates, stack);
|
||||
var splitStack = _stackSystem.Split(throwEnt, 1, EntityManager.GetComponent<TransformComponent>(player).Coordinates, stack);
|
||||
|
||||
if (splitStack is not {Valid: true})
|
||||
return false;
|
||||
|
||||
throwEnt = splitStack.Value;
|
||||
}
|
||||
else if (!hands.Drop(throwEnt.Value))
|
||||
else if (!TryDrop(player, throwEnt, handsComp: hands))
|
||||
return false;
|
||||
|
||||
var direction = coords.ToMapPos(EntityManager) - Transform(player).WorldPosition;
|
||||
@@ -251,7 +224,7 @@ namespace Content.Server.Hands.Systems
|
||||
direction = direction.Normalized * Math.Min(direction.Length, hands.ThrowRange);
|
||||
|
||||
var throwStrength = hands.ThrowForceMultiplier;
|
||||
throwEnt.Value.TryThrow(direction, throwStrength, player);
|
||||
throwEnt.TryThrow(direction, throwStrength, player);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -287,7 +260,7 @@ namespace Content.Server.Hands.Systems
|
||||
return;
|
||||
}
|
||||
|
||||
if (hands.ActiveHandIsHoldingEntity())
|
||||
if (hands.ActiveHand?.HeldEntity != null)
|
||||
{
|
||||
storageComponent.PlayerInsertHeldEntity(plyEnt);
|
||||
}
|
||||
@@ -302,24 +275,11 @@ namespace Content.Server.Hands.Systems
|
||||
var lastStoredEntity = Enumerable.Last(storageComponent.StoredEntities);
|
||||
if (storageComponent.Remove(lastStoredEntity))
|
||||
{
|
||||
if (!hands.TryPickupEntityToActiveHand(lastStoredEntity, animateUser: true))
|
||||
Transform(lastStoredEntity).Coordinates = Transform(plyEnt).Coordinates;
|
||||
PickupOrDrop(plyEnt, lastStoredEntity, animateUser: true, handsComp: hands);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//TODO: Actually shows all items/clothing/etc.
|
||||
private void HandleExamined(EntityUid uid, HandsComponent component, ExaminedEvent args)
|
||||
{
|
||||
foreach (var inhand in component.GetAllHeldItems())
|
||||
{
|
||||
if (HasComp<HandVirtualItemComponent>(inhand.Owner))
|
||||
continue;
|
||||
|
||||
args.PushText(Loc.GetString("comp-hands-examine", ("user", component.Owner), ("item", inhand.Owner)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user