MatchstickSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Light.Components;
  3. using Content.Shared.Audio;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Item;
  6. using Content.Shared.Smoking;
  7. using Content.Shared.Temperature;
  8. using Robust.Server.GameObjects;
  9. using Robust.Shared.Audio;
  10. using Robust.Shared.Audio.Systems;
  11. using Robust.Shared.Player;
  12. namespace Content.Server.Light.EntitySystems
  13. {
  14. public sealed class MatchstickSystem : EntitySystem
  15. {
  16. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  17. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  18. [Dependency] private readonly SharedAudioSystem _audio = default!;
  19. [Dependency] private readonly SharedItemSystem _item = default!;
  20. [Dependency] private readonly SharedPointLightSystem _lights = default!;
  21. [Dependency] private readonly TransformSystem _transformSystem = default!;
  22. private readonly HashSet<Entity<MatchstickComponent>> _litMatches = new();
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<MatchstickComponent, InteractUsingEvent>(OnInteractUsing);
  27. SubscribeLocalEvent<MatchstickComponent, IsHotEvent>(OnIsHotEvent);
  28. SubscribeLocalEvent<MatchstickComponent, ComponentShutdown>(OnShutdown);
  29. }
  30. private void OnShutdown(Entity<MatchstickComponent> ent, ref ComponentShutdown args)
  31. {
  32. _litMatches.Remove(ent);
  33. }
  34. public override void Update(float frameTime)
  35. {
  36. base.Update(frameTime);
  37. foreach (var match in _litMatches)
  38. {
  39. if (match.Comp.CurrentState != SmokableState.Lit || Paused(match) || match.Comp.Deleted)
  40. continue;
  41. var xform = Transform(match);
  42. if (xform.GridUid is not {} gridUid)
  43. return;
  44. var position = _transformSystem.GetGridOrMapTilePosition(match, xform);
  45. _atmosphereSystem.HotspotExpose(gridUid, position, 400, 50, match, true);
  46. }
  47. }
  48. private void OnInteractUsing(Entity<MatchstickComponent> ent, ref InteractUsingEvent args)
  49. {
  50. if (args.Handled || ent.Comp.CurrentState != SmokableState.Unlit)
  51. return;
  52. var isHotEvent = new IsHotEvent();
  53. RaiseLocalEvent(args.Used, isHotEvent);
  54. if (!isHotEvent.IsHot)
  55. return;
  56. Ignite(ent, args.User);
  57. args.Handled = true;
  58. }
  59. private void OnIsHotEvent(EntityUid uid, MatchstickComponent component, IsHotEvent args)
  60. {
  61. args.IsHot = component.CurrentState == SmokableState.Lit;
  62. }
  63. public void Ignite(Entity<MatchstickComponent> matchstick, EntityUid user)
  64. {
  65. var component = matchstick.Comp;
  66. // Play Sound
  67. _audio.PlayPvs(component.IgniteSound, matchstick, AudioParams.Default.WithVariation(0.125f).WithVolume(-0.125f));
  68. // Change state
  69. SetState(matchstick, component, SmokableState.Lit);
  70. _litMatches.Add(matchstick);
  71. matchstick.Owner.SpawnTimer(component.Duration * 1000, delegate
  72. {
  73. SetState(matchstick, component, SmokableState.Burnt);
  74. _litMatches.Remove(matchstick);
  75. });
  76. }
  77. private void SetState(EntityUid uid, MatchstickComponent component, SmokableState value)
  78. {
  79. component.CurrentState = value;
  80. if (_lights.TryGetLight(uid, out var pointLightComponent))
  81. {
  82. _lights.SetEnabled(uid, component.CurrentState == SmokableState.Lit, pointLightComponent);
  83. }
  84. if (EntityManager.TryGetComponent(uid, out ItemComponent? item))
  85. {
  86. switch (component.CurrentState)
  87. {
  88. case SmokableState.Lit:
  89. _item.SetHeldPrefix(uid, "lit", component: item);
  90. break;
  91. default:
  92. _item.SetHeldPrefix(uid, "unlit", component: item);
  93. break;
  94. }
  95. }
  96. if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
  97. {
  98. _appearance.SetData(uid, SmokingVisuals.Smoking, component.CurrentState, appearance);
  99. }
  100. }
  101. }
  102. }