1
0

ExpendableLightSystem.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Content.Client.Light.Components;
  2. using Content.Shared.Light.Components;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Graphics;
  5. using Robust.Shared.Audio;
  6. using Robust.Shared.Audio.Systems;
  7. namespace Content.Client.Light.EntitySystems;
  8. public sealed class ExpendableLightSystem : VisualizerSystem<ExpendableLightComponent>
  9. {
  10. [Dependency] private readonly PointLightSystem _pointLightSystem = default!;
  11. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  12. [Dependency] private readonly LightBehaviorSystem _lightBehavior = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<ExpendableLightComponent, ComponentShutdown>(OnLightShutdown);
  17. }
  18. private void OnLightShutdown(EntityUid uid, ExpendableLightComponent component, ComponentShutdown args)
  19. {
  20. component.PlayingStream = _audioSystem.Stop(component.PlayingStream);
  21. }
  22. protected override void OnAppearanceChange(EntityUid uid, ExpendableLightComponent comp, ref AppearanceChangeEvent args)
  23. {
  24. if (args.Sprite == null)
  25. return;
  26. if (AppearanceSystem.TryGetData<string>(uid, ExpendableLightVisuals.Behavior, out var lightBehaviourID, args.Component)
  27. && TryComp<LightBehaviourComponent>(uid, out var lightBehaviour))
  28. {
  29. _lightBehavior.StopLightBehaviour((uid, lightBehaviour));
  30. if (!string.IsNullOrEmpty(lightBehaviourID))
  31. {
  32. _lightBehavior.StartLightBehaviour((uid, lightBehaviour), lightBehaviourID);
  33. }
  34. else if (TryComp<PointLightComponent>(uid, out var light))
  35. {
  36. _pointLightSystem.SetEnabled(uid, false, light);
  37. }
  38. }
  39. if (!AppearanceSystem.TryGetData<ExpendableLightState>(uid, ExpendableLightVisuals.State, out var state, args.Component))
  40. return;
  41. switch (state)
  42. {
  43. case ExpendableLightState.Lit:
  44. _audioSystem.Stop(comp.PlayingStream);
  45. comp.PlayingStream = _audioSystem.PlayPvs(
  46. comp.LoopedSound, uid)?.Entity;
  47. if (args.Sprite.LayerMapTryGet(ExpendableLightVisualLayers.Overlay, out var layerIdx, true))
  48. {
  49. if (!string.IsNullOrWhiteSpace(comp.IconStateLit))
  50. args.Sprite.LayerSetState(layerIdx, comp.IconStateLit);
  51. if (!string.IsNullOrWhiteSpace(comp.SpriteShaderLit))
  52. args.Sprite.LayerSetShader(layerIdx, comp.SpriteShaderLit);
  53. else
  54. args.Sprite.LayerSetShader(layerIdx, null, null);
  55. if (comp.GlowColorLit.HasValue)
  56. args.Sprite.LayerSetColor(layerIdx, comp.GlowColorLit.Value);
  57. args.Sprite.LayerSetVisible(layerIdx, true);
  58. }
  59. if (comp.GlowColorLit.HasValue)
  60. args.Sprite.LayerSetColor(ExpendableLightVisualLayers.Glow, comp.GlowColorLit.Value);
  61. args.Sprite.LayerSetVisible(ExpendableLightVisualLayers.Glow, true);
  62. break;
  63. case ExpendableLightState.Dead:
  64. comp.PlayingStream = _audioSystem.Stop(comp.PlayingStream);
  65. if (args.Sprite.LayerMapTryGet(ExpendableLightVisualLayers.Overlay, out layerIdx, true))
  66. {
  67. if (!string.IsNullOrWhiteSpace(comp.IconStateSpent))
  68. args.Sprite.LayerSetState(layerIdx, comp.IconStateSpent);
  69. if (!string.IsNullOrWhiteSpace(comp.SpriteShaderSpent))
  70. args.Sprite.LayerSetShader(layerIdx, comp.SpriteShaderSpent);
  71. else
  72. args.Sprite.LayerSetShader(layerIdx, null, null);
  73. }
  74. args.Sprite.LayerSetVisible(ExpendableLightVisualLayers.Glow, false);
  75. break;
  76. }
  77. }
  78. }