SpraySystem.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Content.Server.Chemistry.Components;
  2. using Content.Server.Chemistry.EntitySystems;
  3. using Content.Server.Fluids.Components;
  4. using Content.Server.Gravity;
  5. using Content.Server.Popups;
  6. using Content.Shared.FixedPoint;
  7. using Content.Shared.Fluids;
  8. using Content.Shared.Interaction;
  9. using Content.Shared.Timing;
  10. using Content.Shared.Vapor;
  11. using Content.Shared.Chemistry.EntitySystems;
  12. using Robust.Server.GameObjects;
  13. using Robust.Shared.Audio.Systems;
  14. using Robust.Shared.Physics.Components;
  15. using Robust.Shared.Prototypes;
  16. using System.Numerics;
  17. using Robust.Shared.Map;
  18. namespace Content.Server.Fluids.EntitySystems;
  19. public sealed class SpraySystem : EntitySystem
  20. {
  21. [Dependency] private readonly IPrototypeManager _proto = default!;
  22. [Dependency] private readonly GravitySystem _gravity = default!;
  23. [Dependency] private readonly PhysicsSystem _physics = default!;
  24. [Dependency] private readonly UseDelaySystem _useDelay = default!;
  25. [Dependency] private readonly PopupSystem _popupSystem = default!;
  26. [Dependency] private readonly SharedAudioSystem _audio = default!;
  27. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  28. [Dependency] private readonly VaporSystem _vapor = default!;
  29. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  30. [Dependency] private readonly SharedTransformSystem _transform = default!;
  31. public override void Initialize()
  32. {
  33. base.Initialize();
  34. SubscribeLocalEvent<SprayComponent, AfterInteractEvent>(OnAfterInteract);
  35. SubscribeLocalEvent<SprayComponent, UserActivateInWorldEvent>(OnActivateInWorld);
  36. }
  37. private void OnActivateInWorld(Entity<SprayComponent> entity, ref UserActivateInWorldEvent args)
  38. {
  39. if (args.Handled)
  40. return;
  41. args.Handled = true;
  42. var targetMapPos = _transform.GetMapCoordinates(GetEntityQuery<TransformComponent>().GetComponent(args.Target));
  43. Spray(entity, args.User, targetMapPos);
  44. }
  45. private void OnAfterInteract(Entity<SprayComponent> entity, ref AfterInteractEvent args)
  46. {
  47. if (args.Handled)
  48. return;
  49. args.Handled = true;
  50. var clickPos = _transform.ToMapCoordinates(args.ClickLocation);
  51. Spray(entity, args.User, clickPos);
  52. }
  53. public void Spray(Entity<SprayComponent> entity, EntityUid user, MapCoordinates mapcoord)
  54. {
  55. if (!_solutionContainer.TryGetSolution(entity.Owner, SprayComponent.SolutionName, out var soln, out var solution))
  56. return;
  57. var ev = new SprayAttemptEvent(user);
  58. RaiseLocalEvent(entity, ref ev);
  59. if (ev.Cancelled)
  60. return;
  61. if (TryComp<UseDelayComponent>(entity, out var useDelay)
  62. && _useDelay.IsDelayed((entity, useDelay)))
  63. return;
  64. if (solution.Volume <= 0)
  65. {
  66. _popupSystem.PopupEntity(Loc.GetString("spray-component-is-empty-message"), entity.Owner, user);
  67. return;
  68. }
  69. var xformQuery = GetEntityQuery<TransformComponent>();
  70. var userXform = xformQuery.GetComponent(user);
  71. var userMapPos = _transform.GetMapCoordinates(userXform);
  72. var clickMapPos = mapcoord;
  73. var diffPos = clickMapPos.Position - userMapPos.Position;
  74. if (diffPos == Vector2.Zero || diffPos == Vector2Helpers.NaN)
  75. return;
  76. var diffNorm = diffPos.Normalized();
  77. var diffLength = diffPos.Length();
  78. if (diffLength > entity.Comp.SprayDistance)
  79. {
  80. diffLength = entity.Comp.SprayDistance;
  81. }
  82. var diffAngle = diffNorm.ToAngle();
  83. // Vectors to determine the spawn offset of the vapor clouds.
  84. var threeQuarters = diffNorm * 0.75f;
  85. var quarter = diffNorm * 0.25f;
  86. var amount = Math.Max(Math.Min((solution.Volume / entity.Comp.TransferAmount).Int(), entity.Comp.VaporAmount), 1);
  87. var spread = entity.Comp.VaporSpread / amount;
  88. for (var i = 0; i < amount; i++)
  89. {
  90. var rotation = new Angle(diffAngle + Angle.FromDegrees(spread * i) -
  91. Angle.FromDegrees(spread * (amount - 1) / 2));
  92. // Calculate the destination for the vapor cloud. Limit to the maximum spray distance.
  93. var target = userMapPos
  94. .Offset((diffNorm + rotation.ToVec()).Normalized() * diffLength + quarter);
  95. var distance = (target.Position - userMapPos.Position).Length();
  96. if (distance > entity.Comp.SprayDistance)
  97. target = userMapPos.Offset(diffNorm * entity.Comp.SprayDistance);
  98. var adjustedSolutionAmount = entity.Comp.TransferAmount / entity.Comp.VaporAmount;
  99. var newSolution = _solutionContainer.SplitSolution(soln.Value, adjustedSolutionAmount);
  100. if (newSolution.Volume <= FixedPoint2.Zero)
  101. break;
  102. // Spawn the vapor cloud onto the grid/map the user is present on. Offset the start position based on how far the target destination is.
  103. var vaporPos = userMapPos.Offset(distance < 1 ? quarter : threeQuarters);
  104. var vapor = Spawn(entity.Comp.SprayedPrototype, vaporPos);
  105. var vaporXform = xformQuery.GetComponent(vapor);
  106. _transform.SetWorldRotation(vaporXform, rotation);
  107. if (TryComp(vapor, out AppearanceComponent? appearance))
  108. {
  109. _appearance.SetData(vapor, VaporVisuals.Color, solution.GetColor(_proto).WithAlpha(1f), appearance);
  110. _appearance.SetData(vapor, VaporVisuals.State, true, appearance);
  111. }
  112. // Add the solution to the vapor and actually send the thing
  113. var vaporComponent = Comp<VaporComponent>(vapor);
  114. var ent = (vapor, vaporComponent);
  115. _vapor.TryAddSolution(ent, newSolution);
  116. // impulse direction is defined in world-coordinates, not local coordinates
  117. var impulseDirection = rotation.ToVec();
  118. var time = diffLength / entity.Comp.SprayVelocity;
  119. _vapor.Start(ent, vaporXform, impulseDirection * diffLength, entity.Comp.SprayVelocity, target, time, user);
  120. if (TryComp<PhysicsComponent>(user, out var body))
  121. {
  122. if (_gravity.IsWeightless(user, body))
  123. _physics.ApplyLinearImpulse(user, -impulseDirection.Normalized() * entity.Comp.PushbackAmount, body: body);
  124. }
  125. }
  126. _audio.PlayPvs(entity.Comp.SpraySound, entity, entity.Comp.SpraySound.Params.WithVariation(0.125f));
  127. if (useDelay != null)
  128. _useDelay.TryResetDelay((entity, useDelay));
  129. }
  130. }