Files
tbd-station-14/Content.Client/Projectiles/ProjectileSystem.cs
2023-09-30 14:35:32 +10:00

60 lines
1.8 KiB
C#

using Content.Shared.Projectiles;
using Robust.Shared.Spawners;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
using TimedDespawnComponent = Robust.Shared.Spawners.TimedDespawnComponent;
namespace Content.Client.Projectiles;
public sealed class ProjectileSystem : SharedProjectileSystem
{
[Dependency] private readonly AnimationPlayerSystem _player = default!;
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<ImpactEffectEvent>(OnProjectileImpact);
}
private void OnProjectileImpact(ImpactEffectEvent ev)
{
var coords = GetCoordinates(ev.Coordinates);
if (Deleted(coords.EntityId))
return;
var ent = Spawn(ev.Prototype, coords);
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");
}
}
}