Files
OldThink/Content.Server/Hands/Systems/HandVirtualItemSystem.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2021-12-30 18:27:15 -08:00
using Content.Server.Hands.Components;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Hands.Systems
{
[UsedImplicitly]
public sealed class HandVirtualItemSystem : SharedHandVirtualItemSystem
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
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);
2021-12-30 18:27:15 -08:00
if (hand.HeldEntity != null)
continue;
2021-12-05 18:09:01 +01:00
var pos = EntityManager.GetComponent<TransformComponent>(hands.Owner).Coordinates;
var virtualItem = EntityManager.SpawnEntity("HandVirtualItem", pos);
2021-12-05 18:09:01 +01:00
var virtualItemComp = EntityManager.GetComponent<HandVirtualItemComponent>(virtualItem);
virtualItemComp.BlockingEntity = blockingEnt;
_handsSystem.PutEntityIntoHand(user, hand, virtualItem, hands);
return true;
}
}
return false;
}
/// <summary>
/// Deletes all virtual items in a user's hands with
/// the specified blocked entity.
/// </summary>
public void DeleteInHandsMatching(EntityUid user, EntityUid matching)
{
2021-12-05 18:09:01 +01:00
if (!EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
return;
foreach (var handName in hands.ActivePriorityEnumerable())
{
2021-12-05 18:09:01 +01:00
var hand = hands.GetHand(handName);
2021-12-30 18:27:15 -08:00
if (!(hand.HeldEntity is { } heldEntity))
2021-12-05 18:09:01 +01:00
continue;
2021-12-30 18:27:15 -08:00
if (EntityManager.TryGetComponent<HandVirtualItemComponent>(heldEntity, out var virt)
2021-12-05 18:09:01 +01:00
&& virt.BlockingEntity == matching)
{
Delete(virt, user);
}
}
}
}
}