GibActionSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Content.Shared.Species.Components;
  2. using Content.Shared.Actions;
  3. using Content.Shared.Body.Systems;
  4. using Content.Shared.Mobs;
  5. using Content.Shared.Mobs.Components;
  6. using Content.Shared.Popups;
  7. using Robust.Shared.Prototypes;
  8. namespace Content.Shared.Species;
  9. public sealed partial class GibActionSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  12. [Dependency] private readonly SharedBodySystem _bodySystem = default!;
  13. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  14. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<GibActionComponent, MobStateChangedEvent>(OnMobStateChanged);
  19. SubscribeLocalEvent<GibActionComponent, GibActionEvent>(OnGibAction);
  20. }
  21. private void OnMobStateChanged(EntityUid uid, GibActionComponent comp, MobStateChangedEvent args)
  22. {
  23. // When the mob changes state, check if they're dead and give them the action if so.
  24. if (!TryComp<MobStateComponent>(uid, out var mobState))
  25. return;
  26. if (!_protoManager.TryIndex<EntityPrototype>(comp.ActionPrototype, out var actionProto))
  27. return;
  28. foreach (var allowedState in comp.AllowedStates)
  29. {
  30. if(allowedState == mobState.CurrentState)
  31. {
  32. // The mob should never have more than 1 state so I don't see this being an issue
  33. _actionsSystem.AddAction(uid, ref comp.ActionEntity, comp.ActionPrototype);
  34. return;
  35. }
  36. }
  37. // If they aren't given the action, remove it.
  38. _actionsSystem.RemoveAction(uid, comp.ActionEntity);
  39. }
  40. private void OnGibAction(EntityUid uid, GibActionComponent comp, GibActionEvent args)
  41. {
  42. // When they use the action, gib them.
  43. _popupSystem.PopupClient(Loc.GetString(comp.PopupText, ("name", uid)), uid, uid);
  44. _bodySystem.GibBody(uid, true);
  45. }
  46. public sealed partial class GibActionEvent : InstantActionEvent { }
  47. }