Move MaskComponent to shared and add toggle events (#22395)

* Move MaskComponent to shared and add toggle events

* Datafield and network IsToggled

* Add missing dirty
This commit is contained in:
DrSmugleaf
2023-12-12 19:02:35 -07:00
committed by GitHub
parent 16bd6802df
commit ad6dc94c60
12 changed files with 174 additions and 148 deletions

View File

@@ -59,3 +59,15 @@ public sealed class EquipmentVisualsUpdatedEvent : EntityEventArgs
}
public sealed partial class ToggleMaskEvent : InstantActionEvent { }
/// <summary>
/// Event raised on the mask entity when it is toggled.
/// </summary>
[ByRefEvent]
public readonly record struct ItemMaskToggledEvent(EntityUid Wearer, bool IsToggled, bool IsEquip);
/// <summary>
/// Event raised on the entity wearing the mask when it is toggled.
/// </summary>
[ByRefEvent]
public readonly record struct WearerMaskToggledEvent(bool Enabled);

View File

@@ -0,0 +1,22 @@
using Content.Shared.Clothing.EntitySystems;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Clothing.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(MaskSystem))]
public sealed partial class MaskComponent : Component
{
[DataField, AutoNetworkedField]
public EntProtoId ToggleAction = "ActionToggleMask";
/// <summary>
/// This mask can be toggled (pulled up/down)
/// </summary>
[DataField, AutoNetworkedField]
public EntityUid? ToggleActionEntity;
[DataField, AutoNetworkedField]
public bool IsToggled;
}

View File

@@ -25,6 +25,7 @@ public abstract class ClothingSystem : EntitySystem
SubscribeLocalEvent<ClothingComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<ClothingComponent, GotEquippedEvent>(OnGotEquipped);
SubscribeLocalEvent<ClothingComponent, GotUnequippedEvent>(OnGotUnequipped);
SubscribeLocalEvent<ClothingComponent, ItemMaskToggledEvent>(OnMaskToggled);
}
protected virtual void OnGotEquipped(EntityUid uid, ClothingComponent component, GotEquippedEvent args)
@@ -52,6 +53,12 @@ public abstract class ClothingSystem : EntitySystem
SetEquippedPrefix(uid, state.EquippedPrefix, component);
}
private void OnMaskToggled(Entity<ClothingComponent> ent, ref ItemMaskToggledEvent args)
{
//TODO: sprites for 'pulled down' state. defaults to invisible due to no sprite with this prefix
SetEquippedPrefix(ent, args.IsToggled ? "toggled" : null, ent);
}
#region Public API
public void SetEquippedPrefix(EntityUid uid, string? prefix, ClothingComponent? clothing = null)

View File

@@ -0,0 +1,71 @@
using Content.Shared.Actions;
using Content.Shared.Clothing.Components;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Popups;
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!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MaskComponent, ToggleMaskEvent>(OnToggleMask);
SubscribeLocalEvent<MaskComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<MaskComponent, GotUnequippedEvent>(OnGotUnequipped);
}
private void OnGetActions(EntityUid uid, MaskComponent component, GetItemActionsEvent args)
{
if (!args.InHands)
args.AddAction(ref component.ToggleActionEntity, component.ToggleAction);
}
private void OnToggleMask(Entity<MaskComponent> ent, ref ToggleMaskEvent args)
{
var (uid, mask) = ent;
if (mask.ToggleActionEntity == null)
return;
if (!_inventorySystem.TryGetSlotEntity(args.Performer, "mask", out var existing) || !uid.Equals(existing))
return;
mask.IsToggled ^= true;
_actionSystem.SetToggled(mask.ToggleActionEntity, mask.IsToggled);
if (mask.IsToggled)
_popupSystem.PopupEntity(Loc.GetString("action-mask-pull-down-popup-message", ("mask", uid)), args.Performer, args.Performer);
else
_popupSystem.PopupEntity(Loc.GetString("action-mask-pull-up-popup-message", ("mask", uid)), args.Performer, args.Performer);
ToggleMaskComponents(uid, mask, args.Performer);
}
// 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.ToggleActionEntity == null)
return;
mask.IsToggled = false;
Dirty(uid, mask);
_actionSystem.SetToggled(mask.ToggleActionEntity, mask.IsToggled);
ToggleMaskComponents(uid, mask, args.Equipee, true);
}
private void ToggleMaskComponents(EntityUid uid, MaskComponent mask, EntityUid wearer, bool isEquip = false)
{
var maskEv = new ItemMaskToggledEvent(wearer, mask.IsToggled, isEquip);
RaiseLocalEvent(uid, ref maskEv);
var wearerEv = new WearerMaskToggledEvent(mask.IsToggled);
RaiseLocalEvent(uid, ref wearerEv);
}
}