AntagRandomSpawnRule.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Content.Server.Antag.Components;
  2. using Content.Shared.GameTicking.Components;
  3. using Content.Server.GameTicking.Rules;
  4. namespace Content.Server.Antag;
  5. public sealed class AntagRandomSpawnSystem : GameRuleSystem<AntagRandomSpawnComponent>
  6. {
  7. [Dependency] private readonly SharedTransformSystem _transform = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<AntagRandomSpawnComponent, AntagSelectLocationEvent>(OnSelectLocation);
  12. }
  13. protected override void Added(EntityUid uid, AntagRandomSpawnComponent comp, GameRuleComponent gameRule, GameRuleAddedEvent args)
  14. {
  15. base.Added(uid, comp, gameRule, args);
  16. // we have to select this here because AntagSelectLocationEvent is raised twice because MakeAntag is called twice
  17. // once when a ghost role spawner is created and once when someone takes the ghost role
  18. if (TryFindRandomTile(out _, out _, out _, out var coords))
  19. comp.Coords = coords;
  20. }
  21. private void OnSelectLocation(Entity<AntagRandomSpawnComponent> ent, ref AntagSelectLocationEvent args)
  22. {
  23. if (ent.Comp.Coords != null)
  24. args.Coordinates.Add(_transform.ToMapCoordinates(ent.Comp.Coords.Value));
  25. }
  26. }