Predicted multihanded component (#36712)

* Predicted multihanded component

* Refview

* reh
This commit is contained in:
metalgearsloth
2025-04-19 18:05:20 +10:00
committed by GitHub
parent 64cd180556
commit 9bdd7307fd
5 changed files with 57 additions and 87 deletions

View File

@@ -1,15 +0,0 @@
using Content.Shared.Hands;
using Content.Shared.Item;
namespace Content.Client.Items.Systems;
public sealed class MultiHandedItemSystem : SharedMultiHandedItemSystem
{
protected override void OnEquipped(EntityUid uid, MultiHandedItemComponent component, GotEquippedHandEvent args)
{
}
protected override void OnUnequipped(EntityUid uid, MultiHandedItemComponent component, GotUnequippedHandEvent args)
{
}
}

View File

@@ -1,24 +0,0 @@
using Content.Server.Hands.Systems;
using Content.Server.Inventory;
using Content.Shared.Hands;
using Content.Shared.Item;
namespace Content.Server.Item;
public sealed class MultiHandedItemSystem : SharedMultiHandedItemSystem
{
[Dependency] private readonly VirtualItemSystem _virtualItem = default!;
protected override void OnEquipped(EntityUid uid, MultiHandedItemComponent component, GotEquippedHandEvent args)
{
for (var i = 0; i < component.HandsNeeded - 1; i++)
{
_virtualItem.TrySpawnVirtualItemInHand(uid, args.User);
}
}
protected override void OnUnequipped(EntityUid uid, MultiHandedItemComponent component, GotUnequippedHandEvent args)
{
_virtualItem.DeleteInHandsMatching(args.User, uid);
}
}

View File

@@ -9,6 +9,6 @@ namespace Content.Shared.Item;
[RegisterComponent, NetworkedComponent]
public sealed partial class MultiHandedItemComponent : Component
{
[DataField("handsNeeded"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public int HandsNeeded = 2;
}

View File

@@ -0,0 +1,56 @@
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Inventory.VirtualItem;
using Content.Shared.Popups;
using Robust.Shared.Timing;
namespace Content.Shared.Item;
public sealed class MultiHandedItemSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<MultiHandedItemComponent, GettingPickedUpAttemptEvent>(OnAttemptPickup);
SubscribeLocalEvent<MultiHandedItemComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
SubscribeLocalEvent<MultiHandedItemComponent, GotEquippedHandEvent>(OnEquipped);
SubscribeLocalEvent<MultiHandedItemComponent, GotUnequippedHandEvent>(OnUnequipped);
}
private void OnEquipped(Entity<MultiHandedItemComponent> ent, ref GotEquippedHandEvent args)
{
for (var i = 0; i < ent.Comp.HandsNeeded - 1; i++)
{
_virtualItem.TrySpawnVirtualItemInHand(ent.Owner, args.User);
}
}
private void OnUnequipped(Entity<MultiHandedItemComponent> ent, ref GotUnequippedHandEvent args)
{
_virtualItem.DeleteInHandsMatching(args.User, ent.Owner);
}
private void OnAttemptPickup(Entity<MultiHandedItemComponent> ent, ref GettingPickedUpAttemptEvent args)
{
if (TryComp<HandsComponent>(args.User, out var hands) && hands.CountFreeHands() >= ent.Comp.HandsNeeded)
return;
args.Cancel();
_popup.PopupPredictedCursor(Loc.GetString("multi-handed-item-pick-up-fail",
("number", ent.Comp.HandsNeeded - 1), ("item", ent.Owner)), args.User);
}
private void OnVirtualItemDeleted(Entity<MultiHandedItemComponent> ent, ref VirtualItemDeletedEvent args)
{
if (args.BlockingEntity != ent.Owner || _timing.ApplyingState)
return;
_hands.TryDrop(args.User, ent.Owner);
}
}

View File

@@ -1,47 +0,0 @@
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Popups;
using Robust.Shared.Timing;
namespace Content.Shared.Item;
public abstract class SharedMultiHandedItemSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<MultiHandedItemComponent, GettingPickedUpAttemptEvent>(OnAttemptPickup);
SubscribeLocalEvent<MultiHandedItemComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
SubscribeLocalEvent<MultiHandedItemComponent, GotEquippedHandEvent>(OnEquipped);
SubscribeLocalEvent<MultiHandedItemComponent, GotUnequippedHandEvent>(OnUnequipped);
}
protected abstract void OnEquipped(EntityUid uid, MultiHandedItemComponent component, GotEquippedHandEvent args);
protected abstract void OnUnequipped(EntityUid uid, MultiHandedItemComponent component, GotUnequippedHandEvent args);
private void OnAttemptPickup(EntityUid uid, MultiHandedItemComponent component, GettingPickedUpAttemptEvent args)
{
if (TryComp<HandsComponent>(args.User, out var hands) && hands.CountFreeHands() >= component.HandsNeeded)
return;
args.Cancel();
if (_timing.IsFirstTimePredicted)
{
_popup.PopupCursor(Loc.GetString("multi-handed-item-pick-up-fail",
("number", component.HandsNeeded - 1), ("item", uid)), args.User);
}
}
private void OnVirtualItemDeleted(EntityUid uid, MultiHandedItemComponent component, VirtualItemDeletedEvent args)
{
if (args.BlockingEntity != uid)
return;
_hands.TryDrop(args.User, uid);
}
}