1
0

MeteorSwarmSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Numerics;
  2. using Content.Server.Chat.Systems;
  3. using Content.Server.GameTicking.Rules;
  4. using Content.Server.Station.Components;
  5. using Content.Server.Station.Systems;
  6. using Content.Server.StationEvents.Components;
  7. using Content.Shared.GameTicking.Components;
  8. using Content.Shared.Random.Helpers;
  9. using Robust.Server.Audio;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Physics.Components;
  12. using Robust.Shared.Physics.Systems;
  13. using Robust.Shared.Player;
  14. using Robust.Shared.Random;
  15. namespace Content.Server.StationEvents.Events;
  16. public sealed class MeteorSwarmSystem : GameRuleSystem<MeteorSwarmComponent>
  17. {
  18. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  19. [Dependency] private readonly AudioSystem _audio = default!;
  20. [Dependency] private readonly ChatSystem _chat = default!;
  21. [Dependency] private readonly StationSystem _station = default!;
  22. protected override void Added(EntityUid uid, MeteorSwarmComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args)
  23. {
  24. base.Added(uid, component, gameRule, args);
  25. component.WaveCounter = component.Waves.Next(RobustRandom);
  26. // we don't want to send to players who aren't in game (i.e. in the lobby)
  27. Filter allPlayersInGame = Filter.Empty().AddWhere(GameTicker.UserHasJoinedGame);
  28. if (component.Announcement is { } locId)
  29. _chat.DispatchFilteredAnnouncement(allPlayersInGame, Loc.GetString(locId), playSound: false, colorOverride: Color.Gold);
  30. _audio.PlayGlobal(component.AnnouncementSound, allPlayersInGame, true);
  31. }
  32. protected override void ActiveTick(EntityUid uid, MeteorSwarmComponent component, GameRuleComponent gameRule, float frameTime)
  33. {
  34. if (Timing.CurTime < component.NextWaveTime)
  35. return;
  36. component.NextWaveTime += TimeSpan.FromSeconds(component.WaveCooldown.Next(RobustRandom));
  37. if (_station.GetStations().Count == 0)
  38. return;
  39. var station = RobustRandom.Pick(_station.GetStations());
  40. if (_station.GetLargestGrid(Comp<StationDataComponent>(station)) is not { } grid)
  41. return;
  42. var mapId = Transform(grid).MapID;
  43. var playableArea = _physics.GetWorldAABB(grid);
  44. var minimumDistance = (playableArea.TopRight - playableArea.Center).Length() + 50f;
  45. var maximumDistance = minimumDistance + 100f;
  46. var center = playableArea.Center;
  47. var meteorsToSpawn = component.MeteorsPerWave.Next(RobustRandom);
  48. for (var i = 0; i < meteorsToSpawn; i++)
  49. {
  50. var spawnProto = RobustRandom.Pick(component.Meteors);
  51. var angle = component.NonDirectional
  52. ? RobustRandom.NextAngle()
  53. : new Random(uid.Id).NextAngle();
  54. var offset = angle.RotateVec(new Vector2((maximumDistance - minimumDistance) * RobustRandom.NextFloat() + minimumDistance, 0));
  55. // the line at which spawns occur is perpendicular to the offset.
  56. // This means the meteors are less likely to bunch up and hit the same thing.
  57. var subOffsetAngle = RobustRandom.Prob(0.5f)
  58. ? angle + Math.PI / 2
  59. : angle - Math.PI / 2;
  60. var subOffset = subOffsetAngle.RotateVec(new Vector2( (playableArea.TopRight - playableArea.Center).Length() / 3 * RobustRandom.NextFloat(), 0));
  61. var spawnPosition = new MapCoordinates(center + offset + subOffset, mapId);
  62. var meteor = Spawn(spawnProto, spawnPosition);
  63. var physics = Comp<PhysicsComponent>(meteor);
  64. _physics.ApplyLinearImpulse(meteor, -offset.Normalized() * component.MeteorVelocity * physics.Mass, body: physics);
  65. }
  66. component.WaveCounter--;
  67. if (component.WaveCounter <= 0)
  68. {
  69. ForceEndSelf(uid, gameRule);
  70. }
  71. }
  72. }