1
0

RequireProjectileTargetSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. using Robust.Shared.Random;
  7. using Content.Shared.Mobs.Systems;
  8. namespace Content.Shared.Damage.Components;
  9. public sealed class RequireProjectileTargetSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedContainerSystem _container = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly ILogManager _logManager = default!;
  14. [Dependency] private readonly MobStateSystem _mobState = default!;
  15. private ISawmill _sawmill = default!;
  16. public override void Initialize()
  17. {
  18. SubscribeLocalEvent<RequireProjectileTargetComponent, PreventCollideEvent>(PreventCollide);
  19. SubscribeLocalEvent<RequireProjectileTargetComponent, StoodEvent>(StandingBulletHit);
  20. SubscribeLocalEvent<RequireProjectileTargetComponent, DownedEvent>(LayingBulletPass);
  21. _sawmill = _logManager.GetSawmill("targeting");
  22. }
  23. private void PreventCollide(Entity<RequireProjectileTargetComponent> ent, ref PreventCollideEvent args)
  24. {
  25. if (args.Cancelled)
  26. return;
  27. if (!ent.Comp.Active)
  28. {
  29. return;
  30. }
  31. else
  32. {
  33. if (_mobState.IsDead(args.OtherEntity))
  34. { args.Cancelled = true; }
  35. //_sawmill.Info("checking");
  36. var rando = _random.NextFloat(0.0f, 100.0f);
  37. // 20% chance get hit
  38. if (rando >= 80.0f)
  39. {
  40. //_sawmill.Info("20%");
  41. return;
  42. }
  43. }
  44. var other = args.OtherEntity;
  45. // Resolve the ProjectileComponent on the 'other' entity (the projectile)
  46. if (TryComp(other, out ProjectileComponent? projectileComp) &&
  47. CompOrNull<TargetedProjectileComponent>(other)?.Target != ent)
  48. {
  49. // Prevents shooting out of while inside of crates
  50. var shooter = projectileComp.Shooter;
  51. if (!shooter.HasValue)
  52. return;
  53. // ProjectileGrenades delete the entity that's shooting the projectile,
  54. // so it's impossible to check if the entity is in a container
  55. if (TerminatingOrDeleted(shooter.Value))
  56. {
  57. // If the shooter is deleted, nullify the reference in the projectile component
  58. // to prevent network errors when serializing ProjectileComponent's state.
  59. // This ensures that GetNetEntity won't be called on a deleted entity.
  60. projectileComp.Shooter = null;
  61. Dirty(other, projectileComp); // Mark the ProjectileComponent as dirty so the change is networked.
  62. return;
  63. }
  64. if (!_container.IsEntityOrParentInContainer(shooter.Value))
  65. args.Cancelled = true;
  66. }
  67. }
  68. private void SetActive(Entity<RequireProjectileTargetComponent> ent, bool value)
  69. {
  70. if (ent.Comp.Active == value)
  71. return;
  72. ent.Comp.Active = value;
  73. Dirty(ent);
  74. }
  75. private void StandingBulletHit(Entity<RequireProjectileTargetComponent> ent, ref StoodEvent args)
  76. {
  77. SetActive(ent, false);
  78. }
  79. private void LayingBulletPass(Entity<RequireProjectileTargetComponent> ent, ref DownedEvent args)
  80. {
  81. SetActive(ent, true); // stalker-changes
  82. }
  83. }