RequireProjectileTargetSystem.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Content.Shared.Projectiles;
  2. using Content.Shared.Weapons.Ranged.Components;
  3. using Content.Shared.Standing;
  4. using Robust.Shared.Physics.Events;
  5. using Robust.Shared.Containers;
  6. namespace Content.Shared.Damage.Components;
  7. public sealed class RequireProjectileTargetSystem : EntitySystem
  8. {
  9. [Dependency] private readonly SharedContainerSystem _container = default!;
  10. public override void Initialize()
  11. {
  12. SubscribeLocalEvent<RequireProjectileTargetComponent, PreventCollideEvent>(PreventCollide);
  13. SubscribeLocalEvent<RequireProjectileTargetComponent, StoodEvent>(StandingBulletHit);
  14. SubscribeLocalEvent<RequireProjectileTargetComponent, DownedEvent>(LayingBulletPass);
  15. }
  16. private void PreventCollide(Entity<RequireProjectileTargetComponent> ent, ref PreventCollideEvent args)
  17. {
  18. if (args.Cancelled)
  19. return;
  20. if (!ent.Comp.Active)
  21. return;
  22. var other = args.OtherEntity;
  23. if (TryComp(other, out ProjectileComponent? projectile) &&
  24. CompOrNull<TargetedProjectileComponent>(other)?.Target != ent)
  25. {
  26. // Prevents shooting out of while inside of crates
  27. var shooter = projectile.Shooter;
  28. if (!shooter.HasValue)
  29. return;
  30. // ProjectileGrenades delete the entity that's shooting the projectile,
  31. // so it's impossible to check if the entity is in a container
  32. if (TerminatingOrDeleted(shooter.Value))
  33. return;
  34. if (!_container.IsEntityOrParentInContainer(shooter.Value))
  35. args.Cancelled = true;
  36. }
  37. }
  38. private void SetActive(Entity<RequireProjectileTargetComponent> ent, bool value)
  39. {
  40. if (ent.Comp.Active == value)
  41. return;
  42. ent.Comp.Active = value;
  43. Dirty(ent);
  44. }
  45. private void StandingBulletHit(Entity<RequireProjectileTargetComponent> ent, ref StoodEvent args)
  46. {
  47. SetActive(ent, false);
  48. }
  49. private void LayingBulletPass(Entity<RequireProjectileTargetComponent> ent, ref DownedEvent args)
  50. {
  51. SetActive(ent, true);
  52. }
  53. }