Use red damage animation for guns too (#10938)

This commit is contained in:
metalgearsloth
2022-09-06 18:01:35 +10:00
committed by GitHub
parent 4e8267d40a
commit fae71aeb3e
6 changed files with 161 additions and 4 deletions

View File

@@ -0,0 +1,112 @@
using Content.Shared.Weapons;
using Content.Shared.Weapons.Melee;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Utility;
namespace Content.Client.Weapons.Melee;
public sealed partial class MeleeWeaponSystem
{
private static readonly Animation DefaultDamageAnimation = new()
{
Length = TimeSpan.FromSeconds(DamageAnimationLength),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Color),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(Color.Red, 0f),
new AnimationTrackProperty.KeyFrame(Color.White, DamageAnimationLength)
}
}
}
};
private const float DamageAnimationLength = 0.15f;
private const string DamageAnimationKey = "damage-effect";
private void InitializeEffect()
{
SubscribeLocalEvent<DamageEffectComponent, AnimationCompletedEvent>(OnEffectAnimation);
}
private void OnEffectAnimation(EntityUid uid, DamageEffectComponent component, AnimationCompletedEvent args)
{
if (TryComp<SpriteComponent>(uid, out var sprite))
{
sprite.Color = component.Color;
}
RemCompDeferred<DamageEffectComponent>(uid);
}
/// <summary>
/// Gets the red effect animation whenever the server confirms something is hit
/// </summary>
public Animation? GetDamageAnimation(EntityUid uid, SpriteComponent? sprite = null)
{
if (!Resolve(uid, ref sprite, false))
return null;
// 90% of them are going to be this so why allocate a new class.
if (sprite.Color.Equals(Color.White))
return DefaultDamageAnimation;
return new Animation
{
Length = TimeSpan.FromSeconds(DamageAnimationLength),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Color),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(Color.Red * sprite.Color, 0f),
new AnimationTrackProperty.KeyFrame(sprite.Color, DamageAnimationLength)
}
}
}
};
}
private void OnDamageEffect(DamageEffectEvent ev)
{
if (Deleted(ev.Entity))
return;
var player = EnsureComp<AnimationPlayerComponent>(ev.Entity);
// Need to stop the existing animation first to ensure the sprite color is fixed.
// Otherwise we might lerp to a red colour instead.
if (_animation.HasRunningAnimation(ev.Entity, player, DamageAnimationKey))
{
_animation.Stop(ev.Entity, player, DamageAnimationKey);
}
if (!TryComp<SpriteComponent>(ev.Entity, out var sprite))
{
return;
}
if (TryComp<DamageEffectComponent>(ev.Entity, out var effect))
{
sprite.Color = effect.Color;
}
var animation = GetDamageAnimation(ev.Entity, sprite);
if (animation == null)
return;
var comp = EnsureComp<DamageEffectComponent>(ev.Entity);
comp.NetSyncEnabled = false;
comp.Color = sprite.Color;
_animation.Play(player, DefaultDamageAnimation, DamageAnimationKey);
}
}

View File

@@ -13,17 +13,19 @@ using static Content.Shared.Weapons.Melee.MeleeWeaponSystemMessages;
namespace Content.Client.Weapons.Melee
{
[UsedImplicitly]
public sealed class MeleeWeaponSystem : EntitySystem
public sealed partial class MeleeWeaponSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly AnimationPlayerSystem _animation = default!;
[Dependency] private readonly EffectSystem _effectSystem = default!;
public override void Initialize()
{
InitializeEffect();
SubscribeNetworkEvent<PlayMeleeWeaponAnimationMessage>(PlayWeaponArc);
SubscribeNetworkEvent<PlayLungeAnimationMessage>(PlayLunge);
SubscribeNetworkEvent<DamageEffectEvent>(OnDamageEffect);
}
public override void FrameUpdate(float frameTime)

View File

@@ -3,11 +3,14 @@ using Content.Server.Projectiles.Components;
using Content.Shared.Camera;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Melee;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Player;
using GunSystem = Content.Server.Weapon.Ranged.Systems.GunSystem;
namespace Content.Server.Projectiles
@@ -45,6 +48,11 @@ namespace Content.Server.Projectiles
if (modifiedDamage is not null && EntityManager.EntityExists(component.Shooter))
{
if (modifiedDamage.Total > FixedPoint2.Zero)
{
RaiseNetworkEvent(new DamageEffectEvent(otherEntity), Filter.Pvs(otherEntity, entityManager: EntityManager));
}
_adminLogger.Add(LogType.BulletHit,
HasComp<ActorComponent>(otherEntity) ? LogImpact.Extreme : LogImpact.High,
$"Projectile {ToPrettyString(component.Owner):projectile} shot by {ToPrettyString(component.Shooter):user} hit {ToPrettyString(otherEntity):target} and dealt {modifiedDamage.Total:damage} damage");
@@ -65,7 +73,7 @@ namespace Content.Server.Projectiles
if (component.ImpactEffect != null && TryComp<TransformComponent>(uid, out var xform))
{
RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates));
RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates), Filter.Pvs(xform.Coordinates, entityMan: EntityManager));
}
}
}

View File

@@ -6,6 +6,8 @@ using Content.Server.Weapon.Ranged.Components;
using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using Content.Shared.Weapons.Melee;
using Content.Shared.Weapons.Ranged;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
@@ -124,6 +126,11 @@ public sealed partial class GunSystem : SharedGunSystem
if (dmg != null)
{
if (dmg.Total > FixedPoint2.Zero)
{
RaiseNetworkEvent(new DamageEffectEvent(result.HitEntity), Filter.Pvs(result.HitEntity, entityManager: EntityManager));
}
PlayImpactSound(result.HitEntity, dmg, hitscan.Sound, hitscan.ForceSound);
if (user != null)
@@ -292,7 +299,7 @@ public sealed partial class GunSystem : SharedGunSystem
if (sprites.Count > 0)
{
RaiseNetworkEvent(new HitscanEvent()
RaiseNetworkEvent(new HitscanEvent
{
Sprites = sprites,
}, Filter.Pvs(fromCoordinates, entityMan: EntityManager));

View File

@@ -0,0 +1,11 @@
namespace Content.Shared.Weapons;
/// <summary>
/// Stores the original sprite color for a damaged entity to be able to restore it later.
/// </summary>
[RegisterComponent]
public sealed class DamageEffectComponent : Component
{
[ViewVariables]
public Color Color = Color.White;
}

View File

@@ -0,0 +1,17 @@
using Robust.Shared.Serialization;
namespace Content.Shared.Weapons.Melee;
/// <summary>
/// Raised on the server and sent to a client to play the damage animation.
/// </summary>
[Serializable, NetSerializable]
public sealed class DamageEffectEvent : EntityEventArgs
{
public EntityUid Entity;
public DamageEffectEvent(EntityUid entity)
{
Entity = entity;
}
}