Skip to content

Commit

Permalink
wielding refactor/fixes (space-wizards#32188)
Browse files Browse the repository at this point in the history
* refactor wieldable events

* fix inconsitency with wielding and use updated events

* wieldable cosmetic refactoring

* Update Content.Shared/Wieldable/Events.cs

Co-authored-by: Centronias <[email protected]>

* real

Co-authored-by: ScarKy0 <[email protected]>

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: Centronias <[email protected]>
Co-authored-by: ScarKy0 <[email protected]>
  • Loading branch information
3 people authored Jan 5, 2025
1 parent 0639a49 commit 0aebc76
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 111 deletions.
5 changes: 0 additions & 5 deletions Content.Shared/Wieldable/BeforeUnwieldEvent.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Content.Shared/Wieldable/BeforeWieldEvent.cs

This file was deleted.

43 changes: 43 additions & 0 deletions Content.Shared/Wieldable/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Content.Shared.Wieldable;

/// <summary>
/// Raised directed on an item when it is wielded.
/// </summary>
[ByRefEvent]
public readonly record struct ItemWieldedEvent(EntityUid User);

/// <summary>
/// Raised directed on an item that has been unwielded.
/// Force is whether the item is being forced to be unwielded, or if the player chose to unwield it themselves.
/// </summary>
[ByRefEvent]
public readonly record struct ItemUnwieldedEvent(EntityUid User, bool Force);

/// <summary>
/// Raised directed on an item before a user tries to wield it.
/// If this event is cancelled wielding will not happen.
/// </summary>
[ByRefEvent]
public record struct WieldAttemptEvent(EntityUid User, bool Cancelled = false)
{
public void Cancel()
{
Cancelled = true;
}
}

/// <summary>
/// Raised directed on an item before a user tries to stop wielding it willingly.
/// If this event is cancelled unwielding will not happen.
/// </summary>
/// <remarks>
/// This event is not raised if the user is forced to unwield the item.
/// </remarks>
[ByRefEvent]
public record struct UnwieldAttemptEvent(EntityUid User, bool Cancelled = false)
{
public void Cancel()
{
Cancelled = true;
}
}
23 changes: 0 additions & 23 deletions Content.Shared/Wieldable/ItemUnwieldedEvent.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Content.Shared/Wieldable/ItemWieldedEvent.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Content.Shared/Wieldable/WieldableDoAfterEvent.cs

This file was deleted.

128 changes: 66 additions & 62 deletions Content.Shared/Wieldable/WieldableSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ namespace Content.Shared.Wieldable;

public sealed class WieldableSystem : EntitySystem
{
[Dependency] private readonly SharedVirtualItemSystem _virtualItemSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedItemSystem _itemSystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly UseDelaySystem _delay = default!;
[Dependency] private readonly SharedGunSystem _gun = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedGunSystem _gun = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedItemSystem _item = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedVirtualItemSystem _virtualItem = default!;
[Dependency] private readonly UseDelaySystem _delay = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -82,7 +82,7 @@ private void OnShootAttempt(EntityUid uid, GunRequiresWieldComponent component,
{
component.LastPopup = time;
var message = Loc.GetString("wieldable-component-requires", ("item", uid));
_popupSystem.PopupClient(message, args.Used, args.User);
_popup.PopupClient(message, args.Used, args.User);
}
}
}
Expand All @@ -99,8 +99,7 @@ private void OnGunWielded(EntityUid uid, GunWieldBonusComponent component, ref I

private void OnDeselectWieldable(EntityUid uid, WieldableComponent component, HandDeselectedEvent args)
{
if (!component.Wielded ||
_handsSystem.EnumerateHands(args.User).Count() > 2)
if (_hands.EnumerateHands(args.User).Count() > 2)
return;

TryUnwield(uid, component, args.User);
Expand Down Expand Up @@ -138,7 +137,7 @@ private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, Get
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;

if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
if (!_hands.IsHolding(args.User, uid, out _, args.Hands))
return;

// TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
Expand Down Expand Up @@ -170,28 +169,28 @@ private void OnUseInHand(EntityUid uid, WieldableComponent component, UseInHandE
public bool CanWield(EntityUid uid, WieldableComponent component, EntityUid user, bool quiet = false)
{
// Do they have enough hands free?
if (!EntityManager.TryGetComponent<HandsComponent>(user, out var hands))
if (!TryComp<HandsComponent>(user, out var hands))
{
if (!quiet)
_popupSystem.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
_popup.PopupClient(Loc.GetString("wieldable-component-no-hands"), user, user);
return false;
}

// Is it.. actually in one of their hands?
if (!_handsSystem.IsHolding(user, uid, out _, hands))
if (!_hands.IsHolding(user, uid, out _, hands))
{
if (!quiet)
_popupSystem.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
_popup.PopupClient(Loc.GetString("wieldable-component-not-in-hands", ("item", uid)), user, user);
return false;
}

if (_handsSystem.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
if (_hands.CountFreeableHands((user, hands)) < component.FreeHandsRequired)
{
if (!quiet)
{
var message = Loc.GetString("wieldable-component-not-enough-free-hands",
("number", component.FreeHandsRequired), ("item", uid));
_popupSystem.PopupClient(message, user, user);
_popup.PopupClient(message, user, user);
}
return false;
}
Expand All @@ -209,22 +208,26 @@ public bool TryWield(EntityUid used, WieldableComponent component, EntityUid use
if (!CanWield(used, component, user))
return false;

var ev = new BeforeWieldEvent();
RaiseLocalEvent(used, ev);
if (TryComp(used, out UseDelayComponent? useDelay)
&& !_delay.TryResetDelay((used, useDelay), true))
return false;

var attemptEv = new WieldAttemptEvent(user);
RaiseLocalEvent(used, ref attemptEv);

if (ev.Cancelled)
if (attemptEv.Cancelled)
return false;

if (TryComp<ItemComponent>(used, out var item))
{
component.OldInhandPrefix = item.HeldPrefix;
_itemSystem.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
_item.SetHeldPrefix(used, component.WieldedInhandPrefix, component: item);
}

component.Wielded = true;
SetWielded((used, component), true);

if (component.WieldSound != null)
_audioSystem.PlayPredicted(component.WieldSound, used, user);
_audio.PlayPredicted(component.WieldSound, used, user);

//This section handles spawning the virtual item(s) to occupy the required additional hand(s).
//Since the client can't currently predict entity spawning, only do this if this is running serverside.
Expand All @@ -234,7 +237,7 @@ public bool TryWield(EntityUid used, WieldableComponent component, EntityUid use
var virtuals = new List<EntityUid>();
for (var i = 0; i < component.FreeHandsRequired; i++)
{
if (_virtualItemSystem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
if (_virtualItem.TrySpawnVirtualItemInHand(used, user, out var virtualItem, true))
{
virtuals.Add(virtualItem.Value);
continue;
Expand All @@ -249,78 +252,79 @@ public bool TryWield(EntityUid used, WieldableComponent component, EntityUid use
}
}

if (TryComp(used, out UseDelayComponent? useDelay)
&& !_delay.TryResetDelay((used, useDelay), true))
return false;

var selfMessage = Loc.GetString("wieldable-component-successful-wield", ("item", used));
var othersMessage = Loc.GetString("wieldable-component-successful-wield-other", ("user", Identity.Entity(user, EntityManager)), ("item", used));
_popupSystem.PopupPredicted(selfMessage, othersMessage, user, user);
_popup.PopupPredicted(selfMessage, othersMessage, user, user);

var targEv = new ItemWieldedEvent();
RaiseLocalEvent(used, ref targEv);
var ev = new ItemWieldedEvent(user);
RaiseLocalEvent(used, ref ev);

Dirty(used, component);
return true;
}

/// <summary>
/// Attempts to unwield an item, with no DoAfter.
/// Attempts to unwield an item, with no use delay.
/// </summary>
/// <returns>True if the attempt wasn't blocked.</returns>
public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user)
public bool TryUnwield(EntityUid used, WieldableComponent component, EntityUid user, bool force = false)
{
var ev = new BeforeUnwieldEvent();
RaiseLocalEvent(used, ev);
if (!component.Wielded)
return false; // already unwielded

if (ev.Cancelled)
return false;
if (!force)
{
var attemptEv = new UnwieldAttemptEvent(user);
RaiseLocalEvent(used, ref attemptEv);

if (attemptEv.Cancelled)
return false;
}

component.Wielded = false;
var targEv = new ItemUnwieldedEvent(user);
SetWielded((used, component), false);

RaiseLocalEvent(used, targEv);
var ev = new ItemUnwieldedEvent(user, force);
RaiseLocalEvent(used, ref ev);
return true;
}

/// <summary>
/// Sets wielded without doing any checks.
/// </summary>
private void SetWielded(Entity<WieldableComponent> ent, bool wielded)
{
ent.Comp.Wielded = wielded;
Dirty(ent);
_appearance.SetData(ent, WieldableVisuals.Wielded, wielded);
}

private void OnItemUnwielded(EntityUid uid, WieldableComponent component, ItemUnwieldedEvent args)
{
if (args.User == null)
return;
_item.SetHeldPrefix(uid, component.OldInhandPrefix);

if (TryComp<ItemComponent>(uid, out var item))
{
_itemSystem.SetHeldPrefix(uid, component.OldInhandPrefix, component: item);
}
var user = args.User;
_virtualItem.DeleteInHandsMatching(user, uid);

if (!args.Force) // don't play sound/popup if this was a forced unwield
{
if (component.UnwieldSound != null)
_audioSystem.PlayPredicted(component.UnwieldSound, uid, args.User);
_audio.PlayPredicted(component.UnwieldSound, uid, user);

var selfMessage = Loc.GetString("wieldable-component-failed-wield", ("item", uid));
var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User.Value, EntityManager)), ("item", uid));
_popupSystem.PopupPredicted(selfMessage, othersMessage, args.User.Value, args.User.Value);
var othersMessage = Loc.GetString("wieldable-component-failed-wield-other", ("user", Identity.Entity(args.User, EntityManager)), ("item", uid));
_popup.PopupPredicted(selfMessage, othersMessage, user, user);
}

_appearance.SetData(uid, WieldableVisuals.Wielded, false);

Dirty(uid, component);
_virtualItemSystem.DeleteInHandsMatching(args.User.Value, uid);
}

private void OnItemLeaveHand(EntityUid uid, WieldableComponent component, GotUnequippedHandEvent args)
{
if (!component.Wielded || uid != args.Unequipped)
return;

RaiseLocalEvent(uid, new ItemUnwieldedEvent(args.User, force: true), true);
if (uid == args.Unequipped)
TryUnwield(uid, component, args.User, force: true);
}

private void OnVirtualItemDeleted(EntityUid uid, WieldableComponent component, VirtualItemDeletedEvent args)
{
if (args.BlockingEntity == uid && component.Wielded)
TryUnwield(args.BlockingEntity, component, args.User);
if (args.BlockingEntity == uid)
TryUnwield(uid, component, args.User, force: true);
}

private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)
Expand Down

0 comments on commit 0aebc76

Please sign in to comment.