Refactor DamageOnHighSpeedImpact to be ECS (#4297)
* Refactor DamageOnHighSpeedImpact to be ecs * Comment
This commit is contained in:
@@ -1,26 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using Content.Server.Stunnable.Components;
|
|
||||||
using Content.Shared.Audio;
|
|
||||||
using Content.Shared.Damage;
|
using Content.Shared.Damage;
|
||||||
using Content.Shared.Damage.Components;
|
|
||||||
using Robust.Shared.Audio;
|
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Physics.Collision;
|
|
||||||
using Robust.Shared.Physics.Dynamics;
|
|
||||||
using Robust.Shared.Player;
|
|
||||||
using Robust.Shared.Random;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
using Robust.Shared.Timing;
|
|
||||||
|
|
||||||
namespace Content.Server.Damage.Components
|
namespace Content.Server.Damage.Components
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Should the entity take damage / be stunned if colliding at a speed above MinimumSpeed?
|
||||||
|
/// </summary>
|
||||||
[RegisterComponent]
|
[RegisterComponent]
|
||||||
public class DamageOnHighSpeedImpactComponent : Component, IStartCollide
|
internal sealed class DamageOnHighSpeedImpactComponent : Component
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
|
||||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
||||||
|
|
||||||
public override string Name => "DamageOnHighSpeedImpact";
|
public override string Name => "DamageOnHighSpeedImpact";
|
||||||
|
|
||||||
[DataField("damage")]
|
[DataField("damage")]
|
||||||
@@ -41,30 +31,7 @@ namespace Content.Server.Damage.Components
|
|||||||
public float StunSeconds { get; set; } = 1f;
|
public float StunSeconds { get; set; } = 1f;
|
||||||
[DataField("damageCooldown")]
|
[DataField("damageCooldown")]
|
||||||
public float DamageCooldown { get; set; } = 2f;
|
public float DamageCooldown { get; set; } = 2f;
|
||||||
private TimeSpan _lastHit = TimeSpan.Zero;
|
|
||||||
|
|
||||||
void IStartCollide.CollideWith(Fixture ourFixture, Fixture otherFixture, in Manifold manifold)
|
internal TimeSpan LastHit = TimeSpan.Zero;
|
||||||
{
|
|
||||||
if (!Owner.TryGetComponent(out IDamageableComponent? damageable)) return;
|
|
||||||
|
|
||||||
var speed = ourFixture.Body.LinearVelocity.Length;
|
|
||||||
|
|
||||||
if (speed < MinimumSpeed) return;
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(SoundHit))
|
|
||||||
SoundSystem.Play(Filter.Pvs(otherFixture.Body.Owner), SoundHit, otherFixture.Body.Owner, AudioHelpers.WithVariation(0.125f).WithVolume(-0.125f));
|
|
||||||
|
|
||||||
if ((_gameTiming.CurTime - _lastHit).TotalSeconds < DamageCooldown)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_lastHit = _gameTiming.CurTime;
|
|
||||||
|
|
||||||
var damage = (int) (BaseDamage * (speed / MinimumSpeed) * Factor);
|
|
||||||
|
|
||||||
if (Owner.TryGetComponent(out StunnableComponent? stun) && _robustRandom.Prob(StunChance))
|
|
||||||
stun.Stun(StunSeconds);
|
|
||||||
|
|
||||||
damageable.ChangeDamage(Damage, damage, false, otherFixture.Body.Owner);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
Content.Server/Damage/DamageOnHighSpeedImpactSystem.cs
Normal file
53
Content.Server/Damage/DamageOnHighSpeedImpactSystem.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using Content.Server.Damage.Components;
|
||||||
|
using Content.Server.Stunnable.Components;
|
||||||
|
using Content.Shared.Audio;
|
||||||
|
using Content.Shared.Damage.Components;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using Robust.Shared.Audio;
|
||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Physics.Dynamics;
|
||||||
|
using Robust.Shared.Player;
|
||||||
|
using Robust.Shared.Random;
|
||||||
|
using Robust.Shared.Timing;
|
||||||
|
|
||||||
|
namespace Content.Server.Damage
|
||||||
|
{
|
||||||
|
[UsedImplicitly]
|
||||||
|
internal sealed class DamageOnHighSpeedImpactSystem: EntitySystem
|
||||||
|
{
|
||||||
|
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||||
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||||
|
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
base.Initialize();
|
||||||
|
SubscribeLocalEvent<DamageOnHighSpeedImpactComponent, StartCollideEvent>(HandleCollide);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleCollide(EntityUid uid, DamageOnHighSpeedImpactComponent component, StartCollideEvent args)
|
||||||
|
{
|
||||||
|
if (!ComponentManager.TryGetComponent(uid, out IDamageableComponent? damageable)) return;
|
||||||
|
|
||||||
|
var otherBody = args.OtherFixture.Body.Owner;
|
||||||
|
var speed = args.OurFixture.Body.LinearVelocity.Length;
|
||||||
|
|
||||||
|
if (speed < component.MinimumSpeed) return;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(component.SoundHit))
|
||||||
|
SoundSystem.Play(Filter.Pvs(otherBody), component.SoundHit, otherBody, AudioHelpers.WithVariation(0.125f).WithVolume(-0.125f));
|
||||||
|
|
||||||
|
if ((_gameTiming.CurTime - component.LastHit).TotalSeconds < component.DamageCooldown)
|
||||||
|
return;
|
||||||
|
|
||||||
|
component.LastHit = _gameTiming.CurTime;
|
||||||
|
|
||||||
|
var damage = (int) (component.BaseDamage * (speed / component.MinimumSpeed) * component.Factor);
|
||||||
|
|
||||||
|
if (ComponentManager.TryGetComponent(uid, out StunnableComponent? stun) && _robustRandom.Prob(component.StunChance))
|
||||||
|
stun.Stun(component.StunSeconds);
|
||||||
|
|
||||||
|
damageable.ChangeDamage(component.Damage, damage, false, args.OtherFixture.Body.Owner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user