GrillFuelBurnSystem.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using Content.Server.Temperature.Components;
  2. using Content.Server.Temperature.Systems;
  3. using Content.Shared.Examine;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Kitchen;
  6. using Content.Shared.Placeable;
  7. using Content.Shared.Stacks;
  8. using Content.Shared.Temperature;
  9. using Robust.Server.Audio;
  10. using Robust.Shared.Timing;
  11. using System.Linq;
  12. using Content.Server.IgnitionSource;
  13. using Robust.Server.GameObjects;
  14. using Robust.Shared.GameObjects;
  15. namespace Content.Server.Kitchen;
  16. public sealed class GrillFuelBurnSystem : EntitySystem
  17. {
  18. [Dependency] private readonly IGameTiming _gameTiming = default!;
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. [Dependency] private readonly TemperatureSystem _temperature = default!;
  21. [Dependency] private readonly AudioSystem _audio = default!;
  22. [Dependency] private readonly IEntityManager _entityManager = default!;
  23. [Dependency] private readonly SharedStackSystem _stackSystem = default!;
  24. [Dependency] private readonly SharedPointLightSystem _pointLightSystem = default!;
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. SubscribeLocalEvent<GrillFuelBurnComponent, MapInitEvent>(OnMapInit);
  29. SubscribeLocalEvent<GrillFuelBurnComponent, ExaminedEvent>(OnExamined);
  30. SubscribeLocalEvent<GrillFuelBurnComponent, ItemPlacedEvent>(OnItemPlaced);
  31. SubscribeLocalEvent<GrillFuelBurnComponent, InteractUsingEvent>(OnInteractUsing);
  32. }
  33. private void OnMapInit(EntityUid uid, GrillFuelBurnComponent component, MapInitEvent args)
  34. {
  35. component.IsLit = false;
  36. }
  37. private void OnItemPlaced(EntityUid uid, GrillFuelBurnComponent comp, ref ItemPlacedEvent args)
  38. {
  39. var itemProto = _entityManager.GetComponent<MetaDataComponent>(args.OtherEntity).EntityPrototype?.ID;
  40. }
  41. private void OnInteractUsing(EntityUid uid, GrillFuelBurnComponent comp, InteractUsingEvent args)
  42. {
  43. if (_entityManager.TryGetComponent<IgnitionSourceComponent>(args.Used, out var ignitionSource))
  44. {
  45. if (!comp.IsLit && ignitionSource.Ignited && comp.Fuel > 0)
  46. {
  47. comp.IsLit = true;
  48. AdjustHeaterSetting(uid, comp);
  49. }
  50. args.Handled = true;
  51. return;
  52. }
  53. // Add fuel
  54. if (!_entityManager.TryGetComponent<BurnFuelComponent>(args.Used, out var burnFuel))
  55. {
  56. return;
  57. }
  58. var itemProto = _entityManager.GetComponent<MetaDataComponent>(args.Used).EntityPrototype?.ID;
  59. if (_entityManager.TryGetComponent<StackComponent>(args.Used, out var stackComp))
  60. {
  61. float availableFuel = stackComp.Count;
  62. float fuelNeeded = comp.MaxFuel - comp.Fuel;
  63. float fuelToAdd = Math.Min(availableFuel, fuelNeeded);
  64. if (fuelToAdd > 0)
  65. {
  66. comp.Fuel += fuelToAdd * burnFuel.BurnTime;
  67. _stackSystem.SetCount(args.Used, stackComp.Count - (int)fuelToAdd, stackComp);
  68. if (stackComp.Count <= 0)
  69. {
  70. QueueDel(args.Used);
  71. }
  72. AdjustHeaterSetting(uid, comp);
  73. args.Handled = true;
  74. }
  75. }
  76. else
  77. {
  78. if (comp.Fuel < comp.MaxFuel)
  79. {
  80. comp.Fuel += burnFuel.BurnTime;
  81. QueueDel(args.Used);
  82. AdjustHeaterSetting(uid, comp);
  83. args.Handled = true;
  84. }
  85. }
  86. }
  87. public override void Update(float deltaTime)
  88. {
  89. var query = EntityQueryEnumerator<GrillFuelBurnComponent, ItemPlacerComponent>();
  90. while (query.MoveNext(out var uid, out var comp, out var placer))
  91. {
  92. if (comp.IsLit)
  93. {
  94. if (comp.Fuel <= 0)
  95. {
  96. comp.IsLit = false;
  97. AdjustHeaterSetting(uid, comp);
  98. if (comp.Expends == true)
  99. {
  100. var coordinates = Transform(uid).Coordinates;
  101. Spawn("Coal1", coordinates);
  102. QueueDel(uid);
  103. }
  104. continue;
  105. }
  106. comp.Fuel -= deltaTime / 60f;
  107. AdjustHeaterSetting(uid, comp);
  108. if (comp.Setting != EntityHeaterSetting.Off)
  109. {
  110. var energy = SettingPower(comp.Setting) * deltaTime;
  111. foreach (var ent in placer.PlacedEntities)
  112. {
  113. _temperature.ChangeHeat(ent, energy);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. private void OnExamined(EntityUid uid, GrillFuelBurnComponent comp, ExaminedEvent args)
  120. {
  121. if (!args.IsInDetailsRange)
  122. return;
  123. var remainingTime = comp.Fuel;
  124. args.PushMarkup($"Has approximately {remainingTime:F1} minutes of fuel remaining.");
  125. }
  126. public void ChangeSetting(EntityUid uid, EntityHeaterSetting setting, GrillFuelBurnComponent? comp = null)
  127. {
  128. if (!Resolve(uid, ref comp))
  129. return;
  130. comp.Setting = setting;
  131. _appearance.SetData(uid, EntityHeaterVisuals.Setting, setting);
  132. // Adjust the PointLight based on the Setting
  133. if (_entityManager.TryGetComponent<PointLightComponent>(uid, out var lightComp))
  134. {
  135. switch (setting)
  136. {
  137. case EntityHeaterSetting.Off:
  138. _pointLightSystem.SetEnabled(uid, false, lightComp);
  139. break;
  140. case EntityHeaterSetting.Low:
  141. _pointLightSystem.SetEnabled(uid, true, lightComp);
  142. _pointLightSystem.SetRadius(uid, 2.0f, lightComp);
  143. _pointLightSystem.SetEnergy(uid, 2.0f, lightComp);
  144. break;
  145. case EntityHeaterSetting.Medium:
  146. _pointLightSystem.SetEnabled(uid, true, lightComp);
  147. _pointLightSystem.SetRadius(uid, 4.0f, lightComp);
  148. _pointLightSystem.SetEnergy(uid, 4.0f, lightComp);
  149. break;
  150. case EntityHeaterSetting.High:
  151. _pointLightSystem.SetEnabled(uid, true, lightComp);
  152. _pointLightSystem.SetRadius(uid, 6.0f, lightComp);
  153. _pointLightSystem.SetEnergy(uid, 6.0f, lightComp);
  154. break;
  155. }
  156. }
  157. else
  158. {
  159. Log.Warning($"No PointLightComponent found for campfire {uid}");
  160. }
  161. }
  162. private void AdjustHeaterSetting(EntityUid uid, GrillFuelBurnComponent comp)
  163. {
  164. if (!comp.IsLit) // If its not lit, updates to Off
  165. {
  166. if (comp.Setting != EntityHeaterSetting.Off)
  167. {
  168. ChangeSetting(uid, EntityHeaterSetting.Off, comp);
  169. }
  170. return;
  171. }
  172. var remainingTimeSeconds = comp.Fuel * 60f;
  173. EntityHeaterSetting newSetting;
  174. if (remainingTimeSeconds > 600f) // > 10 minutes
  175. newSetting = EntityHeaterSetting.High;
  176. else if (remainingTimeSeconds > 300f) // 5-10 minutes
  177. newSetting = EntityHeaterSetting.Medium;
  178. else if (remainingTimeSeconds > 0f) // < 5 minutes
  179. newSetting = EntityHeaterSetting.Low;
  180. else
  181. newSetting = EntityHeaterSetting.Off;
  182. if (comp.Setting != newSetting)
  183. {
  184. ChangeSetting(uid, newSetting, comp);
  185. }
  186. }
  187. private float SettingPower(EntityHeaterSetting setting)
  188. {
  189. switch (setting)
  190. {
  191. case EntityHeaterSetting.Low:
  192. return 400f;
  193. case EntityHeaterSetting.Medium:
  194. return 800f;
  195. case EntityHeaterSetting.High:
  196. return 1600f;
  197. default:
  198. return 0f;
  199. }
  200. }
  201. }