Files
OldThink/Content.Shared/Damage/Systems/DamageOnHighSpeedImpactSystem.cs

71 lines
2.6 KiB
C#
Raw Normal View History

2023-11-09 01:43:27 +00:00
using Content.Shared.Stunnable;
using Content.Shared.Damage.Components;
2023-08-02 12:32:44 +03:00
using Content.Shared.Effects;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Physics.Events;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
2023-11-09 01:43:27 +00:00
namespace Content.Shared.Damage.Systems;
2023-08-02 12:32:44 +03:00
public sealed class DamageOnHighSpeedImpactSystem : EntitySystem
{
2023-08-02 12:32:44 +03:00
[Dependency] private readonly IGameTiming _gameTiming = default!;
2023-08-11 03:44:52 +10:00
[Dependency] private readonly IRobustRandom _robustRandom = default!;
2023-08-02 12:32:44 +03:00
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
2023-08-11 03:44:52 +10:00
[Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
2023-11-09 01:43:27 +00:00
[Dependency] private readonly SharedStunSystem _stun = default!;
2023-08-02 12:32:44 +03:00
public override void Initialize()
{
2023-08-02 12:32:44 +03:00
base.Initialize();
SubscribeLocalEvent<DamageOnHighSpeedImpactComponent, StartCollideEvent>(HandleCollide);
}
2023-08-02 12:32:44 +03:00
private void HandleCollide(EntityUid uid, DamageOnHighSpeedImpactComponent component, ref StartCollideEvent args)
{
if (!args.OurFixture.Hard || !args.OtherFixture.Hard)
return;
2023-08-02 12:32:44 +03:00
if (!EntityManager.HasComponent<DamageableComponent>(uid))
return;
2023-08-02 12:32:44 +03:00
var speed = args.OurBody.LinearVelocity.Length();
2023-08-02 12:32:44 +03:00
if (speed < component.MinimumSpeed)
return;
if (component.LastHit != null
&& (_gameTiming.CurTime - component.LastHit.Value).TotalSeconds < component.DamageCooldown)
2023-08-02 12:32:44 +03:00
return;
2023-08-02 12:32:44 +03:00
component.LastHit = _gameTiming.CurTime;
2023-08-02 12:32:44 +03:00
if (_robustRandom.Prob(component.StunChance))
_stun.TryStun(uid, TimeSpan.FromSeconds(component.StunSeconds), true);
2023-08-02 12:32:44 +03:00
var damageScale = component.SpeedDamageFactor * speed / component.MinimumSpeed;
2023-08-02 12:32:44 +03:00
_damageable.TryChangeDamage(uid, component.Damage * damageScale);
2021-11-29 02:34:44 +13:00
if (_gameTiming.IsFirstTimePredicted)
_audio.PlayPvs(component.SoundHit, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));
2023-08-11 03:44:52 +10:00
_color.RaiseEffect(Color.Red, new List<EntityUid>() { uid }, Filter.Pvs(uid, entityManager: EntityManager));
}
2023-11-09 01:43:27 +00:00
public void ChangeCollide(EntityUid uid, float minimumSpeed, float stunSeconds, float damageCooldown, float speedDamage, DamageOnHighSpeedImpactComponent? collide = null)
{
if (!Resolve(uid, ref collide, false))
return;
collide.MinimumSpeed = minimumSpeed;
collide.StunSeconds = stunSeconds;
collide.DamageCooldown = damageCooldown;
2023-11-09 01:43:27 +00:00
collide.SpeedDamageFactor = speedDamage;
Dirty(uid, collide);
}
}