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>
This commit is contained in:
@@ -3,7 +3,6 @@ using Content.Shared.Clothing.Components;
|
||||
using Content.Shared.Foldable;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
@@ -15,6 +14,7 @@ public sealed class MaskSystem : EntitySystem
|
||||
[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()
|
||||
{
|
||||
@@ -35,53 +35,109 @@ public sealed class MaskSystem : EntitySystem
|
||||
private void OnToggleMask(Entity<MaskComponent> ent, ref ToggleMaskEvent args)
|
||||
{
|
||||
var (uid, mask) = ent;
|
||||
if (mask.ToggleActionEntity == null || !_timing.IsFirstTimePredicted || !mask.IsEnabled)
|
||||
if (mask.ToggleActionEntity == null || !mask.IsToggleable)
|
||||
return;
|
||||
|
||||
if (!_inventorySystem.TryGetSlotEntity(args.Performer, "mask", out var existing) || !uid.Equals(existing))
|
||||
return;
|
||||
// Masks are currently only toggleable via the action while equipped.
|
||||
// Its possible this might change in future?
|
||||
|
||||
mask.IsToggled ^= true;
|
||||
// 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);
|
||||
|
||||
ToggleMaskComponents(uid, mask, args.Performer, mask.EquippedPrefix);
|
||||
}
|
||||
|
||||
// set to untoggled when unequipped, so it isn't left in a 'pulled down' state
|
||||
private void OnGotUnequipped(EntityUid uid, MaskComponent mask, GotUnequippedEvent args)
|
||||
{
|
||||
if (!mask.IsToggled || !mask.IsEnabled)
|
||||
return;
|
||||
|
||||
mask.IsToggled = false;
|
||||
ToggleMaskComponents(uid, mask, args.Equipee, mask.EquippedPrefix, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after setting IsToggled, raises events and dirties.
|
||||
/// <summary>
|
||||
private void ToggleMaskComponents(EntityUid uid, MaskComponent mask, EntityUid wearer, string? equippedPrefix = null, bool isEquip = false)
|
||||
{
|
||||
Dirty(uid, mask);
|
||||
if (mask.ToggleActionEntity is {} action)
|
||||
_actionSystem.SetToggled(action, mask.IsToggled);
|
||||
|
||||
var maskEv = new ItemMaskToggledEvent(wearer, equippedPrefix, mask.IsToggled, isEquip);
|
||||
RaiseLocalEvent(uid, ref maskEv);
|
||||
|
||||
var wearerEv = new WearerMaskToggledEvent(mask.IsToggled);
|
||||
RaiseLocalEvent(wearer, ref wearerEv);
|
||||
// Masks are currently always un-toggled when unequipped.
|
||||
SetToggled((uid, mask), false);
|
||||
}
|
||||
|
||||
private void OnFolded(Entity<MaskComponent> ent, ref FoldedEvent args)
|
||||
{
|
||||
if (ent.Comp.DisableOnFolded)
|
||||
ent.Comp.IsEnabled = !args.IsFolded;
|
||||
ent.Comp.IsToggled = args.IsFolded;
|
||||
// See FoldableClothingComponent
|
||||
|
||||
ToggleMaskComponents(ent.Owner, ent.Comp, ent.Owner);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user