DamageOnHighSpeedImpactSystem.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Content.Shared.Stunnable;
  2. using Content.Shared.Damage.Components;
  3. using Content.Shared.Effects;
  4. using Robust.Shared.Audio;
  5. using Robust.Shared.Audio.Systems;
  6. using Robust.Shared.Physics.Events;
  7. using Robust.Shared.Player;
  8. using Robust.Shared.Random;
  9. using Robust.Shared.Timing;
  10. namespace Content.Shared.Damage.Systems;
  11. public sealed class DamageOnHighSpeedImpactSystem : EntitySystem
  12. {
  13. [Dependency] private readonly IGameTiming _gameTiming = default!;
  14. [Dependency] private readonly IRobustRandom _robustRandom = default!;
  15. [Dependency] private readonly DamageableSystem _damageable = default!;
  16. [Dependency] private readonly SharedAudioSystem _audio = default!;
  17. [Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
  18. [Dependency] private readonly SharedStunSystem _stun = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<DamageOnHighSpeedImpactComponent, StartCollideEvent>(HandleCollide);
  23. }
  24. private void HandleCollide(EntityUid uid, DamageOnHighSpeedImpactComponent component, ref StartCollideEvent args)
  25. {
  26. if (!args.OurFixture.Hard || !args.OtherFixture.Hard)
  27. return;
  28. if (!EntityManager.HasComponent<DamageableComponent>(uid))
  29. return;
  30. var speed = args.OurBody.LinearVelocity.Length();
  31. if (speed < component.MinimumSpeed)
  32. return;
  33. if (component.LastHit != null
  34. && (_gameTiming.CurTime - component.LastHit.Value).TotalSeconds < component.DamageCooldown)
  35. return;
  36. component.LastHit = _gameTiming.CurTime;
  37. if (_robustRandom.Prob(component.StunChance))
  38. _stun.TryStun(uid, TimeSpan.FromSeconds(component.StunSeconds), true);
  39. var damageScale = component.SpeedDamageFactor * speed / component.MinimumSpeed;
  40. _damageable.TryChangeDamage(uid, component.Damage * damageScale);
  41. if (_gameTiming.IsFirstTimePredicted)
  42. _audio.PlayPvs(component.SoundHit, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));
  43. _color.RaiseEffect(Color.Red, new List<EntityUid>() { uid }, Filter.Pvs(uid, entityManager: EntityManager));
  44. }
  45. public void ChangeCollide(EntityUid uid, float minimumSpeed, float stunSeconds, float damageCooldown, float speedDamage, DamageOnHighSpeedImpactComponent? collide = null)
  46. {
  47. if (!Resolve(uid, ref collide, false))
  48. return;
  49. collide.MinimumSpeed = minimumSpeed;
  50. collide.StunSeconds = stunSeconds;
  51. collide.DamageCooldown = damageCooldown;
  52. collide.SpeedDamageFactor = speedDamage;
  53. Dirty(uid, collide);
  54. }
  55. }