ThrowArtifactSystem.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Numerics;
  2. using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
  3. using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
  4. using Content.Shared.Maps;
  5. using Content.Shared.Physics;
  6. using Content.Shared.Throwing;
  7. using Robust.Shared.Map.Components;
  8. using Robust.Shared.Physics.Components;
  9. using Robust.Shared.Random;
  10. namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;
  11. public sealed class ThrowArtifactSystem : EntitySystem
  12. {
  13. [Dependency] private readonly IRobustRandom _random = default!;
  14. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  15. [Dependency] private readonly ThrowingSystem _throwing = default!;
  16. [Dependency] private readonly TileSystem _tile = default!;
  17. [Dependency] private readonly SharedTransformSystem _transform = default!;
  18. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  19. /// <inheritdoc/>
  20. public override void Initialize()
  21. {
  22. SubscribeLocalEvent<ThrowArtifactComponent, ArtifactActivatedEvent>(OnActivated);
  23. }
  24. private void OnActivated(EntityUid uid, ThrowArtifactComponent component, ArtifactActivatedEvent args)
  25. {
  26. var xform = Transform(uid);
  27. if (TryComp<MapGridComponent>(xform.GridUid, out var grid))
  28. {
  29. var tiles = _mapSystem.GetTilesIntersecting(
  30. xform.GridUid.Value,
  31. grid,
  32. Box2.CenteredAround(_transform.GetWorldPosition(xform), new Vector2(component.Range * 2, component.Range)));
  33. foreach (var tile in tiles)
  34. {
  35. if (!_random.Prob(component.TilePryChance))
  36. continue;
  37. _tile.PryTile(tile);
  38. }
  39. }
  40. var lookup = _lookup.GetEntitiesInRange(uid, component.Range, LookupFlags.Dynamic | LookupFlags.Sundries);
  41. var physQuery = GetEntityQuery<PhysicsComponent>();
  42. foreach (var ent in lookup)
  43. {
  44. if (physQuery.TryGetComponent(ent, out var phys)
  45. && (phys.CollisionMask & (int) CollisionGroup.GhostImpassable) != 0)
  46. continue;
  47. var tempXform = Transform(ent);
  48. var foo = _transform.GetMapCoordinates(ent, xform: tempXform).Position - _transform.GetMapCoordinates(uid, xform: xform).Position;
  49. _throwing.TryThrow(ent, foo*2, component.ThrowStrength, uid, 0);
  50. }
  51. }
  52. }