Damage on high speed effect (#18566)

This commit is contained in:
Slava0135
2023-08-02 12:32:44 +03:00
committed by GitHub
parent a7519580c0
commit 52a60f698f
2 changed files with 65 additions and 58 deletions

View File

@@ -1,33 +1,40 @@
using Content.Server.Damage.Systems;
using Content.Shared.Damage;
using Robust.Shared.Audio;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
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]
internal sealed class DamageOnHighSpeedImpactComponent : Component
[RegisterComponent, Access(typeof(DamageOnHighSpeedImpactSystem))]
public sealed class DamageOnHighSpeedImpactComponent : Component
{
[DataField("minimumSpeed")]
public float MinimumSpeed { get; set; } = 20f;
[DataField("factor")]
public float Factor { get; set; } = 0.5f;
[DataField("soundHit", required: true)]
public SoundSpecifier SoundHit { get; set; } = default!;
[DataField("stunChance")]
public float StunChance { get; set; } = 0.25f;
[DataField("stunMinimumDamage")]
public int StunMinimumDamage { get; set; } = 10;
[DataField("stunSeconds")]
public float StunSeconds { get; set; } = 1f;
[DataField("damageCooldown")]
public float DamageCooldown { get; set; } = 2f;
[DataField("minimumSpeed"), ViewVariables(VVAccess.ReadWrite)]
public float MinimumSpeed = 20f;
internal TimeSpan LastHit = TimeSpan.Zero;
[DataField("speedDamageFactor"), ViewVariables(VVAccess.ReadWrite)]
public float SpeedDamageFactor = 0.5f;
[DataField("damage", required: true)]
[ViewVariables(VVAccess.ReadWrite)]
[DataField("soundHit", required: true), ViewVariables(VVAccess.ReadWrite)]
public SoundSpecifier SoundHit = default!;
[DataField("stunChance"), ViewVariables(VVAccess.ReadWrite)]
public float StunChance = 0.25f;
[DataField("stunMinimumDamage"), ViewVariables(VVAccess.ReadWrite)]
public int StunMinimumDamage = 10;
[DataField("stunSeconds"), ViewVariables(VVAccess.ReadWrite)]
public float StunSeconds = 1f;
[DataField("damageCooldown"), ViewVariables(VVAccess.ReadWrite)]
public float DamageCooldown = 2f;
[DataField("lastHit", customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan LastHit = TimeSpan.Zero;
[DataField("damage", required: true), ViewVariables(VVAccess.ReadWrite)]
public DamageSpecifier Damage = default!;
}
}

View File

@@ -1,24 +1,22 @@
using Content.Server.Damage.Components;
using Content.Server.Stunnable;
using Content.Shared.Audio;
using Content.Shared.Damage;
using JetBrains.Annotations;
using Content.Shared.Effects;
using Robust.Shared.Audio;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Damage.Systems
{
[UsedImplicitly]
internal sealed class DamageOnHighSpeedImpactSystem: EntitySystem
namespace Content.Server.Damage.Systems;
public sealed class DamageOnHighSpeedImpactSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly StunSystem _stunSystem = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly StunSystem _stun = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
@@ -28,28 +26,30 @@ namespace Content.Server.Damage.Systems
private void HandleCollide(EntityUid uid, DamageOnHighSpeedImpactComponent component, ref StartCollideEvent args)
{
if (!args.OurFixture.Hard || !args.OtherFixture.Hard)
return;
if (!EntityManager.HasComponent<DamageableComponent>(uid))
return;
var otherBody = args.OtherEntity;
var speed = args.OurBody.LinearVelocity.Length();
if (speed < component.MinimumSpeed)
return;
SoundSystem.Play(component.SoundHit.GetSound(), Filter.Pvs(otherBody), otherBody, AudioHelpers.WithVariation(0.125f).WithVolume(-0.125f));
if ((_gameTiming.CurTime - component.LastHit).TotalSeconds < component.DamageCooldown)
return;
component.LastHit = _gameTiming.CurTime;
if (_robustRandom.Prob(component.StunChance))
_stunSystem.TryStun(uid, TimeSpan.FromSeconds(component.StunSeconds), true);
_stun.TryStun(uid, TimeSpan.FromSeconds(component.StunSeconds), true);
var damageScale = (speed / component.MinimumSpeed) * component.Factor;
var damageScale = component.SpeedDamageFactor * speed / component.MinimumSpeed;
_damageableSystem.TryChangeDamage(uid, component.Damage * damageScale);
}
_damageable.TryChangeDamage(uid, component.Damage * damageScale);
_audio.PlayPvs(component.SoundHit, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));
RaiseNetworkEvent(new ColorFlashEffectEvent(Color.Red, new List<EntityUid> { uid }), Filter.Pvs(uid, entityManager: EntityManager));
}
}