SpaceSpawnRule.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Content.Server.Antag;
  2. using Content.Server.GameTicking.Rules.Components;
  3. using Content.Server.Station.Components;
  4. using Content.Server.StationEvents.Components;
  5. using Content.Shared.GameTicking.Components;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Map.Components;
  8. namespace Content.Server.StationEvents.Events;
  9. /// <summary>
  10. /// Station event component for spawning this rules antags in space around a station.
  11. /// </summary>
  12. public sealed class SpaceSpawnRule : StationEventSystem<SpaceSpawnRuleComponent>
  13. {
  14. [Dependency] private readonly SharedTransformSystem _transform = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<SpaceSpawnRuleComponent, AntagSelectLocationEvent>(OnSelectLocation);
  19. }
  20. protected override void Added(EntityUid uid, SpaceSpawnRuleComponent comp, GameRuleComponent gameRule, GameRuleAddedEvent args)
  21. {
  22. base.Added(uid, comp, gameRule, args);
  23. if (!TryGetRandomStation(out var station))
  24. {
  25. ForceEndSelf(uid, gameRule);
  26. return;
  27. }
  28. var stationData = Comp<StationDataComponent>(station.Value);
  29. // find a station grid
  30. var gridUid = StationSystem.GetLargestGrid(stationData);
  31. if (gridUid == null || !TryComp<MapGridComponent>(gridUid, out var grid))
  32. {
  33. Sawmill.Warning("Chosen station has no grids, cannot pick location for {ToPrettyString(uid):rule}");
  34. ForceEndSelf(uid, gameRule);
  35. return;
  36. }
  37. // figure out its AABB size and use that as a guide to how far the spawner should be
  38. var size = grid.LocalAABB.Size.Length() / 2;
  39. var distance = size + comp.SpawnDistance;
  40. var angle = RobustRandom.NextAngle();
  41. // position relative to station center
  42. var location = angle.ToVec() * distance;
  43. // create the spawner!
  44. var xform = Transform(gridUid.Value);
  45. var position = _transform.GetWorldPosition(xform) + location;
  46. comp.Coords = new MapCoordinates(position, xform.MapID);
  47. Sawmill.Info($"Picked location {comp.Coords} for {ToPrettyString(uid):rule}");
  48. }
  49. private void OnSelectLocation(Entity<SpaceSpawnRuleComponent> ent, ref AntagSelectLocationEvent args)
  50. {
  51. if (ent.Comp.Coords is {} coords)
  52. args.Coordinates.Add(coords);
  53. }
  54. }