ArtifactGasTriggerSystem.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
  3. using Content.Server.Xenoarchaeology.XenoArtifacts.Triggers.Components;
  4. namespace Content.Server.Xenoarchaeology.XenoArtifacts.Triggers.Systems;
  5. public sealed class ArtifactGasTriggerSystem : EntitySystem
  6. {
  7. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  8. [Dependency] private readonly ArtifactSystem _artifactSystem = default!;
  9. public override void Initialize()
  10. {
  11. base.Initialize();
  12. SubscribeLocalEvent<ArtifactGasTriggerComponent, ArtifactNodeEnteredEvent>(OnRandomizeTrigger);
  13. }
  14. private void OnRandomizeTrigger(EntityUid uid, ArtifactGasTriggerComponent component, ArtifactNodeEnteredEvent args)
  15. {
  16. if (component.ActivationGas != null)
  17. return;
  18. var gas = component.PossibleGases[args.RandomSeed % component.PossibleGases.Count];
  19. component.ActivationGas = gas;
  20. }
  21. public override void Update(float frameTime)
  22. {
  23. base.Update(frameTime);
  24. List<Entity<ArtifactComponent>> toUpdate = new();
  25. var query = EntityQueryEnumerator<ArtifactGasTriggerComponent, ArtifactComponent, TransformComponent>();
  26. while (query.MoveNext(out var uid, out var trigger, out var artifact, out var transform))
  27. {
  28. if (trigger.ActivationGas == null)
  29. continue;
  30. var environment = _atmosphereSystem.GetTileMixture((uid, transform));
  31. if (environment == null)
  32. continue;
  33. // check if outside there is enough moles to activate artifact
  34. var moles = environment.GetMoles(trigger.ActivationGas.Value);
  35. if (moles < trigger.ActivationMoles)
  36. continue;
  37. toUpdate.Add((uid, artifact));
  38. }
  39. foreach (var a in toUpdate)
  40. {
  41. _artifactSystem.TryActivateArtifact(a, null, a);
  42. }
  43. }
  44. }