1
0

SharedRatKingSystem.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Content.Shared.Actions;
  2. using Content.Shared.DoAfter;
  3. using Content.Shared.Random;
  4. using Content.Shared.Random.Helpers;
  5. using Content.Shared.Verbs;
  6. using Robust.Shared.Audio;
  7. using Robust.Shared.Audio.Systems;
  8. using Robust.Shared.Network;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Random;
  11. using Robust.Shared.Serialization;
  12. namespace Content.Shared.RatKing;
  13. public abstract class SharedRatKingSystem : EntitySystem
  14. {
  15. [Dependency] private readonly INetManager _net = default!;
  16. [Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
  17. [Dependency] protected readonly IRobustRandom Random = default!;
  18. [Dependency] private readonly SharedActionsSystem _action = default!;
  19. [Dependency] private readonly SharedAudioSystem _audio = default!;
  20. [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
  21. /// <inheritdoc/>
  22. public override void Initialize()
  23. {
  24. SubscribeLocalEvent<RatKingComponent, ComponentStartup>(OnStartup);
  25. SubscribeLocalEvent<RatKingComponent, ComponentShutdown>(OnShutdown);
  26. SubscribeLocalEvent<RatKingComponent, RatKingOrderActionEvent>(OnOrderAction);
  27. SubscribeLocalEvent<RatKingServantComponent, ComponentShutdown>(OnServantShutdown);
  28. SubscribeLocalEvent<RatKingRummageableComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerb);
  29. SubscribeLocalEvent<RatKingRummageableComponent, RatKingRummageDoAfterEvent>(OnDoAfterComplete);
  30. }
  31. private void OnStartup(EntityUid uid, RatKingComponent component, ComponentStartup args)
  32. {
  33. if (!TryComp(uid, out ActionsComponent? comp))
  34. return;
  35. _action.AddAction(uid, ref component.ActionRaiseArmyEntity, component.ActionRaiseArmy, component: comp);
  36. _action.AddAction(uid, ref component.ActionDomainEntity, component.ActionDomain, component: comp);
  37. _action.AddAction(uid, ref component.ActionOrderStayEntity, component.ActionOrderStay, component: comp);
  38. _action.AddAction(uid, ref component.ActionOrderFollowEntity, component.ActionOrderFollow, component: comp);
  39. _action.AddAction(uid, ref component.ActionOrderCheeseEmEntity, component.ActionOrderCheeseEm, component: comp);
  40. _action.AddAction(uid, ref component.ActionOrderLooseEntity, component.ActionOrderLoose, component: comp);
  41. UpdateActions(uid, component);
  42. }
  43. private void OnShutdown(EntityUid uid, RatKingComponent component, ComponentShutdown args)
  44. {
  45. foreach (var servant in component.Servants)
  46. {
  47. if (TryComp(servant, out RatKingServantComponent? servantComp))
  48. servantComp.King = null;
  49. }
  50. if (!TryComp(uid, out ActionsComponent? comp))
  51. return;
  52. _action.RemoveAction(uid, component.ActionRaiseArmyEntity, comp);
  53. _action.RemoveAction(uid, component.ActionDomainEntity, comp);
  54. _action.RemoveAction(uid, component.ActionOrderStayEntity, comp);
  55. _action.RemoveAction(uid, component.ActionOrderFollowEntity, comp);
  56. _action.RemoveAction(uid, component.ActionOrderCheeseEmEntity, comp);
  57. _action.RemoveAction(uid, component.ActionOrderLooseEntity, comp);
  58. }
  59. private void OnOrderAction(EntityUid uid, RatKingComponent component, RatKingOrderActionEvent args)
  60. {
  61. if (component.CurrentOrder == args.Type)
  62. return;
  63. args.Handled = true;
  64. component.CurrentOrder = args.Type;
  65. Dirty(uid, component);
  66. DoCommandCallout(uid, component);
  67. UpdateActions(uid, component);
  68. UpdateAllServants(uid, component);
  69. }
  70. private void OnServantShutdown(EntityUid uid, RatKingServantComponent component, ComponentShutdown args)
  71. {
  72. if (TryComp(component.King, out RatKingComponent? ratKingComponent))
  73. ratKingComponent.Servants.Remove(uid);
  74. }
  75. private void UpdateActions(EntityUid uid, RatKingComponent? component = null)
  76. {
  77. if (!Resolve(uid, ref component))
  78. return;
  79. _action.SetToggled(component.ActionOrderStayEntity, component.CurrentOrder == RatKingOrderType.Stay);
  80. _action.SetToggled(component.ActionOrderFollowEntity, component.CurrentOrder == RatKingOrderType.Follow);
  81. _action.SetToggled(component.ActionOrderCheeseEmEntity, component.CurrentOrder == RatKingOrderType.CheeseEm);
  82. _action.SetToggled(component.ActionOrderLooseEntity, component.CurrentOrder == RatKingOrderType.Loose);
  83. _action.StartUseDelay(component.ActionOrderStayEntity);
  84. _action.StartUseDelay(component.ActionOrderFollowEntity);
  85. _action.StartUseDelay(component.ActionOrderCheeseEmEntity);
  86. _action.StartUseDelay(component.ActionOrderLooseEntity);
  87. }
  88. private void OnGetVerb(EntityUid uid, RatKingRummageableComponent component, GetVerbsEvent<AlternativeVerb> args)
  89. {
  90. if (!HasComp<RatKingComponent>(args.User) || component.Looted)
  91. return;
  92. args.Verbs.Add(new AlternativeVerb
  93. {
  94. Text = Loc.GetString("rat-king-rummage-text"),
  95. Priority = 0,
  96. Act = () =>
  97. {
  98. _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, component.RummageDuration,
  99. new RatKingRummageDoAfterEvent(), uid, uid)
  100. {
  101. BlockDuplicate = true,
  102. BreakOnDamage = true,
  103. BreakOnMove = true,
  104. DistanceThreshold = 2f
  105. });
  106. }
  107. });
  108. }
  109. private void OnDoAfterComplete(EntityUid uid, RatKingRummageableComponent component, RatKingRummageDoAfterEvent args)
  110. {
  111. if (args.Cancelled || component.Looted)
  112. return;
  113. component.Looted = true;
  114. Dirty(uid, component);
  115. _audio.PlayPredicted(component.Sound, uid, args.User);
  116. var spawn = PrototypeManager.Index<WeightedRandomEntityPrototype>(component.RummageLoot).Pick(Random);
  117. if (_net.IsServer)
  118. Spawn(spawn, Transform(uid).Coordinates);
  119. }
  120. public void UpdateAllServants(EntityUid uid, RatKingComponent component)
  121. {
  122. foreach (var servant in component.Servants)
  123. {
  124. UpdateServantNpc(servant, component.CurrentOrder);
  125. }
  126. }
  127. public virtual void UpdateServantNpc(EntityUid uid, RatKingOrderType orderType)
  128. {
  129. }
  130. public virtual void DoCommandCallout(EntityUid uid, RatKingComponent component)
  131. {
  132. }
  133. }
  134. [Serializable, NetSerializable]
  135. public sealed partial class RatKingRummageDoAfterEvent : SimpleDoAfterEvent
  136. {
  137. }