1
0

WaggingSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Content.Server.Actions;
  2. using Content.Server.Humanoid;
  3. using Content.Shared.Humanoid;
  4. using Content.Shared.Humanoid.Markings;
  5. using Content.Shared.Mobs;
  6. using Content.Shared.Toggleable;
  7. using Content.Shared.Wagging;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.Wagging;
  10. /// <summary>
  11. /// Adds an action to toggle wagging animation for tails markings that supporting this
  12. /// </summary>
  13. public sealed class WaggingSystem : EntitySystem
  14. {
  15. [Dependency] private readonly ActionsSystem _actions = default!;
  16. [Dependency] private readonly HumanoidAppearanceSystem _humanoidAppearance = default!;
  17. [Dependency] private readonly IPrototypeManager _prototype = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<WaggingComponent, MapInitEvent>(OnWaggingMapInit);
  22. SubscribeLocalEvent<WaggingComponent, ComponentShutdown>(OnWaggingShutdown);
  23. SubscribeLocalEvent<WaggingComponent, ToggleActionEvent>(OnWaggingToggle);
  24. SubscribeLocalEvent<WaggingComponent, MobStateChangedEvent>(OnMobStateChanged);
  25. }
  26. private void OnWaggingMapInit(EntityUid uid, WaggingComponent component, MapInitEvent args)
  27. {
  28. _actions.AddAction(uid, ref component.ActionEntity, component.Action, uid);
  29. }
  30. private void OnWaggingShutdown(EntityUid uid, WaggingComponent component, ComponentShutdown args)
  31. {
  32. _actions.RemoveAction(uid, component.ActionEntity);
  33. }
  34. private void OnWaggingToggle(EntityUid uid, WaggingComponent component, ref ToggleActionEvent args)
  35. {
  36. if (args.Handled)
  37. return;
  38. TryToggleWagging(uid, wagging: component);
  39. }
  40. private void OnMobStateChanged(EntityUid uid, WaggingComponent component, MobStateChangedEvent args)
  41. {
  42. if (component.Wagging)
  43. TryToggleWagging(uid, wagging: component);
  44. }
  45. public bool TryToggleWagging(EntityUid uid, WaggingComponent? wagging = null, HumanoidAppearanceComponent? humanoid = null)
  46. {
  47. if (!Resolve(uid, ref wagging, ref humanoid))
  48. return false;
  49. if (!humanoid.MarkingSet.Markings.TryGetValue(MarkingCategories.Tail, out var markings))
  50. return false;
  51. if (markings.Count == 0)
  52. return false;
  53. wagging.Wagging = !wagging.Wagging;
  54. for (var idx = 0; idx < markings.Count; idx++) // Animate all possible tails
  55. {
  56. var currentMarkingId = markings[idx].MarkingId;
  57. string newMarkingId;
  58. if (wagging.Wagging)
  59. {
  60. newMarkingId = $"{currentMarkingId}{wagging.Suffix}";
  61. }
  62. else
  63. {
  64. if (currentMarkingId.EndsWith(wagging.Suffix))
  65. {
  66. newMarkingId = currentMarkingId[..^wagging.Suffix.Length];
  67. }
  68. else
  69. {
  70. newMarkingId = currentMarkingId;
  71. Log.Warning($"Unable to revert wagging for {currentMarkingId}");
  72. }
  73. }
  74. if (!_prototype.HasIndex<MarkingPrototype>(newMarkingId))
  75. {
  76. Log.Warning($"{ToPrettyString(uid)} tried toggling wagging but {newMarkingId} marking doesn't exist");
  77. continue;
  78. }
  79. _humanoidAppearance.SetMarkingId(uid, MarkingCategories.Tail, idx, newMarkingId,
  80. humanoid: humanoid);
  81. }
  82. return true;
  83. }
  84. }