1
0

LightBulbSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Content.Server.Light.Components;
  2. using Content.Shared.Destructible;
  3. using Content.Shared.Light.Components;
  4. using Content.Shared.Throwing;
  5. using Robust.Server.GameObjects;
  6. using Robust.Shared.Audio;
  7. using Robust.Shared.Audio.Systems;
  8. using Robust.Shared.Player;
  9. namespace Content.Server.Light.EntitySystems
  10. {
  11. public sealed class LightBulbSystem : EntitySystem
  12. {
  13. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  14. [Dependency] private readonly SharedAudioSystem _audio = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<LightBulbComponent, ComponentInit>(OnInit);
  19. SubscribeLocalEvent<LightBulbComponent, LandEvent>(HandleLand);
  20. SubscribeLocalEvent<LightBulbComponent, BreakageEventArgs>(OnBreak);
  21. }
  22. private void OnInit(EntityUid uid, LightBulbComponent bulb, ComponentInit args)
  23. {
  24. // update default state of bulbs
  25. SetColor(uid, bulb.Color, bulb);
  26. SetState(uid, bulb.State, bulb);
  27. }
  28. private void HandleLand(EntityUid uid, LightBulbComponent bulb, ref LandEvent args)
  29. {
  30. PlayBreakSound(uid, bulb);
  31. SetState(uid, LightBulbState.Broken, bulb);
  32. }
  33. private void OnBreak(EntityUid uid, LightBulbComponent component, BreakageEventArgs args)
  34. {
  35. SetState(uid, LightBulbState.Broken, component);
  36. }
  37. /// <summary>
  38. /// Set a new color for a light bulb and raise event about change
  39. /// </summary>
  40. public void SetColor(EntityUid uid, Color color, LightBulbComponent? bulb = null)
  41. {
  42. if (!Resolve(uid, ref bulb))
  43. return;
  44. bulb.Color = color;
  45. UpdateAppearance(uid, bulb);
  46. }
  47. /// <summary>
  48. /// Set a new state for a light bulb (broken, burned) and raise event about change
  49. /// </summary>
  50. public void SetState(EntityUid uid, LightBulbState state, LightBulbComponent? bulb = null)
  51. {
  52. if (!Resolve(uid, ref bulb))
  53. return;
  54. bulb.State = state;
  55. UpdateAppearance(uid, bulb);
  56. }
  57. public void PlayBreakSound(EntityUid uid, LightBulbComponent? bulb = null)
  58. {
  59. if (!Resolve(uid, ref bulb))
  60. return;
  61. _audio.PlayPvs(bulb.BreakSound, uid);
  62. }
  63. private void UpdateAppearance(EntityUid uid, LightBulbComponent? bulb = null,
  64. AppearanceComponent? appearance = null)
  65. {
  66. if (!Resolve(uid, ref bulb, ref appearance, logMissing: false))
  67. return;
  68. // try to update appearance and color
  69. _appearance.SetData(uid, LightBulbVisuals.State, bulb.State, appearance);
  70. _appearance.SetData(uid, LightBulbVisuals.Color, bulb.Color, appearance);
  71. }
  72. }
  73. }