1
0

TimerTriggerVisualizerSystem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Content.Shared.Trigger;
  2. using Robust.Client.Animations;
  3. using Robust.Client.GameObjects;
  4. using Robust.Shared.Audio;
  5. using Robust.Shared.Audio.Systems;
  6. using Robust.Shared.GameObjects;
  7. namespace Content.Client.Trigger;
  8. public sealed class TimerTriggerVisualizerSystem : VisualizerSystem<TimerTriggerVisualsComponent>
  9. {
  10. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<TimerTriggerVisualsComponent, ComponentInit>(OnComponentInit);
  15. }
  16. private void OnComponentInit(EntityUid uid, TimerTriggerVisualsComponent comp, ComponentInit args)
  17. {
  18. comp.PrimingAnimation = new Animation {
  19. Length = TimeSpan.MaxValue,
  20. AnimationTracks = {
  21. new AnimationTrackSpriteFlick() {
  22. LayerKey = TriggerVisualLayers.Base,
  23. KeyFrames = { new AnimationTrackSpriteFlick.KeyFrame(comp.PrimingSprite, 0f) }
  24. }
  25. },
  26. };
  27. if (comp.PrimingSound != null)
  28. {
  29. comp.PrimingAnimation.AnimationTracks.Add(
  30. new AnimationTrackPlaySound() {
  31. KeyFrames = { new AnimationTrackPlaySound.KeyFrame(_audioSystem.ResolveSound(comp.PrimingSound), 0) }
  32. }
  33. );
  34. }
  35. }
  36. protected override void OnAppearanceChange(EntityUid uid, TimerTriggerVisualsComponent comp, ref AppearanceChangeEvent args)
  37. {
  38. if (args.Sprite == null
  39. || !TryComp<AnimationPlayerComponent>(uid, out var animPlayer))
  40. return;
  41. if (!AppearanceSystem.TryGetData<TriggerVisualState>(uid, TriggerVisuals.VisualState, out var state, args.Component))
  42. state = TriggerVisualState.Unprimed;
  43. switch (state)
  44. {
  45. case TriggerVisualState.Primed:
  46. if (!AnimationSystem.HasRunningAnimation(uid, animPlayer, TimerTriggerVisualsComponent.AnimationKey))
  47. AnimationSystem.Play(uid, animPlayer, comp.PrimingAnimation, TimerTriggerVisualsComponent.AnimationKey);
  48. break;
  49. case TriggerVisualState.Unprimed:
  50. args.Sprite.LayerSetState(TriggerVisualLayers.Base, comp.UnprimedSprite);
  51. break;
  52. default:
  53. throw new ArgumentOutOfRangeException();
  54. }
  55. }
  56. }
  57. public enum TriggerVisualLayers : byte
  58. {
  59. Base
  60. }