SpiderChargeSystem.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Content.Server.Explosion.EntitySystems;
  2. using Content.Server.Mind;
  3. using Content.Server.Objectives.Components;
  4. using Content.Server.Popups;
  5. using Content.Server.Roles;
  6. using Content.Shared.Ninja.Components;
  7. using Content.Shared.Ninja.Systems;
  8. using Content.Shared.Roles;
  9. using Content.Shared.Sticky;
  10. namespace Content.Server.Ninja.Systems;
  11. /// <summary>
  12. /// Prevents planting a spider charge outside of its location and handles greentext.
  13. /// </summary>
  14. public sealed class SpiderChargeSystem : SharedSpiderChargeSystem
  15. {
  16. [Dependency] private readonly MindSystem _mind = default!;
  17. [Dependency] private readonly PopupSystem _popup = default!;
  18. [Dependency] private readonly SharedRoleSystem _role = default!;
  19. [Dependency] private readonly SharedTransformSystem _transform = default!;
  20. [Dependency] private readonly SpaceNinjaSystem _ninja = default!;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<SpiderChargeComponent, AttemptEntityStickEvent>(OnAttemptStick);
  25. SubscribeLocalEvent<SpiderChargeComponent, EntityStuckEvent>(OnStuck);
  26. SubscribeLocalEvent<SpiderChargeComponent, TriggerEvent>(OnExplode);
  27. }
  28. /// <summary>
  29. /// Require that the planter is a ninja and the charge is near the target warp point.
  30. /// </summary>
  31. private void OnAttemptStick(EntityUid uid, SpiderChargeComponent comp, ref AttemptEntityStickEvent args)
  32. {
  33. if (args.Cancelled)
  34. return;
  35. var user = args.User;
  36. if (!_mind.TryGetMind(args.User, out var mind, out _))
  37. return;
  38. if (!_role.MindHasRole<NinjaRoleComponent>(mind))
  39. {
  40. _popup.PopupEntity(Loc.GetString("spider-charge-not-ninja"), user, user);
  41. args.Cancelled = true;
  42. return;
  43. }
  44. // allow planting anywhere if there is no target, which should never happen
  45. if (!_mind.TryGetObjectiveComp<SpiderChargeConditionComponent>(user, out var obj) || obj.Target == null)
  46. return;
  47. // assumes warp point still exists
  48. var targetXform = Transform(obj.Target.Value);
  49. var locXform = Transform(args.Target);
  50. if (locXform.MapID != targetXform.MapID ||
  51. (_transform.GetWorldPosition(locXform) - _transform.GetWorldPosition(targetXform)).LengthSquared() > comp.Range * comp.Range)
  52. {
  53. _popup.PopupEntity(Loc.GetString("spider-charge-too-far"), user, user);
  54. args.Cancelled = true;
  55. return;
  56. }
  57. }
  58. /// <summary>
  59. /// Allows greentext to occur after exploding.
  60. /// </summary>
  61. private void OnStuck(EntityUid uid, SpiderChargeComponent comp, ref EntityStuckEvent args)
  62. {
  63. comp.Planter = args.User;
  64. }
  65. /// <summary>
  66. /// Handles greentext after exploding.
  67. /// Assumes it didn't move and the target was destroyed so be nice.
  68. /// </summary>
  69. private void OnExplode(EntityUid uid, SpiderChargeComponent comp, TriggerEvent args)
  70. {
  71. if (!TryComp<SpaceNinjaComponent>(comp.Planter, out var ninja))
  72. return;
  73. // assumes the target was destroyed, that the charge wasn't moved somehow
  74. _ninja.DetonatedSpiderCharge((comp.Planter.Value, ninja));
  75. }
  76. }