Bullet impact effects (#9530)
This commit is contained in:
61
Content.Client/Projectiles/ProjectileSystem.cs
Normal file
61
Content.Client/Projectiles/ProjectileSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Content.Shared.Projectiles;
|
||||
using Content.Shared.Spawners.Components;
|
||||
using Content.Shared.Weapons.Ranged.Systems;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Client.Projectiles;
|
||||
|
||||
public sealed class ProjectileSystem : SharedProjectileSystem
|
||||
{
|
||||
[Dependency] private readonly AnimationPlayerSystem _player = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ProjectileComponent, ComponentHandleState>(OnHandleState);
|
||||
SubscribeNetworkEvent<ImpactEffectEvent>(OnProjectileImpact);
|
||||
}
|
||||
|
||||
private void OnProjectileImpact(ImpactEffectEvent ev)
|
||||
{
|
||||
var ent = Spawn(ev.Prototype, ev.Coordinates);
|
||||
|
||||
if (TryComp<SpriteComponent>(ent, out var sprite))
|
||||
{
|
||||
sprite[EffectLayers.Unshaded].AutoAnimated = false;
|
||||
sprite.LayerMapTryGet(EffectLayers.Unshaded, out var layer);
|
||||
var state = sprite.LayerGetState(layer);
|
||||
var lifetime = 0.5f;
|
||||
|
||||
if (TryComp<TimedDespawnComponent>(ent, out var despawn))
|
||||
lifetime = despawn.Lifetime;
|
||||
|
||||
var anim = new Animation()
|
||||
{
|
||||
Length = TimeSpan.FromSeconds(lifetime),
|
||||
AnimationTracks =
|
||||
{
|
||||
new AnimationTrackSpriteFlick()
|
||||
{
|
||||
LayerKey = EffectLayers.Unshaded,
|
||||
KeyFrames =
|
||||
{
|
||||
new AnimationTrackSpriteFlick.KeyFrame(state.Name, 0f),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_player.Play(ent, anim, "impact-effect");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, ProjectileComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not ProjectileComponentState state) return;
|
||||
component.Shooter = state.Shooter;
|
||||
component.IgnoreShooter = state.IgnoreShooter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user