ShuffleArtifactSystem.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
  2. using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
  3. using Content.Shared.Mobs.Components;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Random;
  6. namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
  7. public sealed class ShuffleArtifactSystem : EntitySystem
  8. {
  9. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  10. [Dependency] private readonly IRobustRandom _random = default!;
  11. [Dependency] private readonly SharedTransformSystem _xform = default!;
  12. /// <inheritdoc/>
  13. public override void Initialize()
  14. {
  15. SubscribeLocalEvent<ShuffleArtifactComponent, ArtifactActivatedEvent>(OnActivated);
  16. }
  17. private void OnActivated(EntityUid uid, ShuffleArtifactComponent component, ArtifactActivatedEvent args)
  18. {
  19. var mobState = GetEntityQuery<MobStateComponent>();
  20. List<Entity<TransformComponent>> toShuffle = new();
  21. foreach (var ent in _lookup.GetEntitiesInRange(uid, component.Radius, LookupFlags.Dynamic | LookupFlags.Sundries))
  22. {
  23. if (!mobState.HasComponent(ent))
  24. continue;
  25. var xform = Transform(ent);
  26. toShuffle.Add((ent, xform));
  27. }
  28. _random.Shuffle(toShuffle);
  29. while (toShuffle.Count > 1)
  30. {
  31. var ent1 = _random.PickAndTake(toShuffle);
  32. var ent2 = _random.PickAndTake(toShuffle);
  33. _xform.SwapPositions((ent1, ent1), (ent2, ent2));
  34. }
  35. }
  36. }