Files
tbd-station-14/Content.Shared/Clothing/EntitySystems/MaskSystem.cs
paige404 2e7f01b99e Fix broken layer hiding on clothes with multiple equipment slots (#34080)
* Fix broken layer hiding on clothes with multiple equipment slots

* Refactor ToggleVisualLayers, HideLayerClothingComponent, and ClothingComponent to allow more
precise layer hide behavior and more CPU efficient layer toggling.

* Adjust HumanoidAppearaceSystem to track which slots are hiding a given layer (e.g. gas mask and welding mask)
Add documentation
Change gas masks to use the new HideLayerClothingComponent structure as an example of its usage

* Fix the delayed snout bug

* Misc cleanup

* Make `bool permanent` implicit from SlotFlags

any non-permanent visibility toggle with `SlotFlags.None` isn't supported with how its set up. And similarly, the slot flags argument does nothing if permanent = true. So IMO it makes more sense to infer it from a nullable arg.

* Split into separate system

Too much pasta

* Remove (hopefully unnecessary) refresh

* Fisk mask networking

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* Keep old behaviour, use clearer names?

I'm just guessing at what this was meant to do

* english

* Separate slot name & flag

* dirty = true

* fix comment

* Improved SetLayerVisibility with dirtying logic suggested by @ElectroJr

* Only set mask toggled if DisableOnFold is true

* FoldableClothingSystem fixes

* fix bandana state

* Better comment

---------

Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
2025-03-21 00:30:47 +11:00

144 lines
4.9 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Clothing.Components;
using Content.Shared.Foldable;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Popups;
using Robust.Shared.Timing;
namespace Content.Shared.Clothing.EntitySystems;
public sealed class MaskSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actionSystem = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ClothingSystem _clothing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MaskComponent, ToggleMaskEvent>(OnToggleMask);
SubscribeLocalEvent<MaskComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<MaskComponent, GotUnequippedEvent>(OnGotUnequipped);
SubscribeLocalEvent<MaskComponent, FoldedEvent>(OnFolded);
}
private void OnGetActions(EntityUid uid, MaskComponent component, GetItemActionsEvent args)
{
if (_inventorySystem.InSlotWithFlags(uid, SlotFlags.MASK))
args.AddAction(ref component.ToggleActionEntity, component.ToggleAction);
}
private void OnToggleMask(Entity<MaskComponent> ent, ref ToggleMaskEvent args)
{
var (uid, mask) = ent;
if (mask.ToggleActionEntity == null || !mask.IsToggleable)
return;
// Masks are currently only toggleable via the action while equipped.
// Its possible this might change in future?
// TODO Inventory / Clothing
// Add an easier way to check if clothing is equipped to a valid slot.
if (!TryComp(ent, out ClothingComponent? clothing)
|| clothing.InSlotFlag is not { } slotFlag
|| !clothing.Slots.HasFlag(slotFlag))
{
return;
}
SetToggled((uid, mask), !mask.IsToggled);
var dir = mask.IsToggled ? "down" : "up";
var msg = $"action-mask-pull-{dir}-popup-message";
_popupSystem.PopupClient(Loc.GetString(msg, ("mask", uid)), args.Performer, args.Performer);
}
private void OnGotUnequipped(EntityUid uid, MaskComponent mask, GotUnequippedEvent args)
{
// Masks are currently always un-toggled when unequipped.
SetToggled((uid, mask), false);
}
private void OnFolded(Entity<MaskComponent> ent, ref FoldedEvent args)
{
// See FoldableClothingComponent
if (!ent.Comp.DisableOnFolded)
return;
// While folded, we force the mask to be toggled / pulled down, so that its functionality as a mask is disabled,
// and we also prevent it from being un-toggled. We also automatically untoggle it when it gets unfolded, so it
// fully returns to its previous state when folded & unfolded.
SetToggled(ent!, args.IsFolded, force: true);
SetToggleable(ent!, !args.IsFolded);
}
public void SetToggled(Entity<MaskComponent?> mask, bool toggled, bool force = false)
{
if (_timing.ApplyingState)
return;
if (!Resolve(mask.Owner, ref mask.Comp))
return;
if (!force && !mask.Comp.IsToggleable)
return;
if (mask.Comp.IsToggled == toggled)
return;
mask.Comp.IsToggled = toggled;
if (mask.Comp.ToggleActionEntity is { } action)
_actionSystem.SetToggled(action, mask.Comp.IsToggled);
// TODO Generalize toggling & clothing prefixes. See also FoldableClothingComponent
var prefix = mask.Comp.IsToggled ? mask.Comp.EquippedPrefix : null;
_clothing.SetEquippedPrefix(mask, prefix);
// TODO Inventory / Clothing
// Add an easier way to get the entity that is wearing clothing in a valid slot.
EntityUid? wearer = null;
if (TryComp(mask, out ClothingComponent? clothing)
&& clothing.InSlotFlag is {} slotFlag
&& clothing.Slots.HasFlag(slotFlag))
{
wearer = Transform(mask).ParentUid;
}
var maskEv = new ItemMaskToggledEvent(mask!, wearer);
RaiseLocalEvent(mask, ref maskEv);
if (wearer != null)
{
var wearerEv = new WearerMaskToggledEvent(mask!);
RaiseLocalEvent(wearer.Value, ref wearerEv);
}
Dirty(mask);
}
public void SetToggleable(Entity<MaskComponent?> mask, bool toggleable)
{
if (_timing.ApplyingState)
return;
if (!Resolve(mask.Owner, ref mask.Comp))
return;
if (mask.Comp.IsToggleable == toggleable)
return;
if (mask.Comp.ToggleActionEntity is { } action)
_actionSystem.SetEnabled(action, mask.Comp.IsToggleable);
mask.Comp.IsToggleable = toggleable;
Dirty(mask);
}
}