ToggleClothingSystem.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Content.Shared.Actions;
  2. using Content.Shared.Clothing;
  3. using Content.Shared.Clothing.Components;
  4. using Content.Shared.Inventory;
  5. using Content.Shared.Item.ItemToggle;
  6. using Content.Shared.Toggleable;
  7. namespace Content.Shared.Clothing.EntitySystems;
  8. /// <summary>
  9. /// Handles adding and using a toggle action for <see cref="ToggleClothingComponent"/>.
  10. /// </summary>
  11. public sealed class ToggleClothingSystem : EntitySystem
  12. {
  13. [Dependency] private readonly SharedActionsSystem _actions = default!;
  14. [Dependency] private readonly ItemToggleSystem _toggle = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<ToggleClothingComponent, MapInitEvent>(OnMapInit);
  19. SubscribeLocalEvent<ToggleClothingComponent, GetItemActionsEvent>(OnGetActions);
  20. SubscribeLocalEvent<ToggleClothingComponent, ToggleActionEvent>(OnToggleAction);
  21. SubscribeLocalEvent<ToggleClothingComponent, ClothingGotUnequippedEvent>(OnUnequipped);
  22. }
  23. private void OnMapInit(Entity<ToggleClothingComponent> ent, ref MapInitEvent args)
  24. {
  25. var (uid, comp) = ent;
  26. // test funny
  27. if (string.IsNullOrEmpty(comp.Action))
  28. return;
  29. _actions.AddAction(uid, ref comp.ActionEntity, comp.Action);
  30. _actions.SetToggled(comp.ActionEntity, _toggle.IsActivated(ent.Owner));
  31. Dirty(uid, comp);
  32. }
  33. private void OnGetActions(Entity<ToggleClothingComponent> ent, ref GetItemActionsEvent args)
  34. {
  35. if (args.InHands && ent.Comp.MustEquip)
  36. return;
  37. var ev = new ToggleClothingCheckEvent(args.User);
  38. RaiseLocalEvent(ent, ref ev);
  39. if (!ev.Cancelled)
  40. args.AddAction(ent.Comp.ActionEntity);
  41. }
  42. private void OnToggleAction(Entity<ToggleClothingComponent> ent, ref ToggleActionEvent args)
  43. {
  44. args.Handled = _toggle.Toggle(ent.Owner, args.Performer);
  45. }
  46. private void OnUnequipped(Entity<ToggleClothingComponent> ent, ref ClothingGotUnequippedEvent args)
  47. {
  48. if (ent.Comp.DisableOnUnequip)
  49. _toggle.TryDeactivate(ent.Owner, args.Wearer);
  50. }
  51. }