1
0

ActiveGeneratorRevvingSystem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. namespace Content.Shared.Power.Generator;
  2. public sealed class ActiveGeneratorRevvingSystem : EntitySystem
  3. {
  4. public override void Initialize()
  5. {
  6. base.Initialize();
  7. SubscribeLocalEvent<ActiveGeneratorRevvingComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
  8. }
  9. /// <summary>
  10. /// Handles the AnchorStateChangedEvent to stop auto-revving when unanchored.
  11. /// </summary>
  12. private void OnAnchorStateChanged(EntityUid uid, ActiveGeneratorRevvingComponent component, AnchorStateChangedEvent args)
  13. {
  14. if (!args.Anchored)
  15. StopAutoRevving(uid);
  16. }
  17. /// <summary>
  18. /// Start revving a generator entity automatically, without another entity doing a do-after.
  19. /// Used for remotely activating a generator.
  20. /// </summary>
  21. /// <param name="uid">Uid of the generator entity.</param>
  22. /// <param name="component">ActiveGeneratorRevvingComponent of the generator entity.</param>
  23. public void StartAutoRevving(EntityUid uid, ActiveGeneratorRevvingComponent? component = null)
  24. {
  25. if (Resolve(uid, ref component, false))
  26. {
  27. // reset the revving
  28. component.CurrentTime = TimeSpan.FromSeconds(0);
  29. return;
  30. }
  31. AddComp(uid, new ActiveGeneratorRevvingComponent());
  32. }
  33. /// <summary>
  34. /// Stop revving a generator entity.
  35. /// </summary>
  36. /// <param name="uid">Uid of the generator entity.</param>
  37. /// <returns>True if the auto-revving was cancelled, false if it was never revving in the first place.</returns>
  38. public bool StopAutoRevving(EntityUid uid)
  39. {
  40. return RemComp<ActiveGeneratorRevvingComponent>(uid);
  41. }
  42. /// <summary>
  43. /// Raise an event on a generator entity to start it.
  44. /// </summary>
  45. /// <remarks>This is not the same as revving it, when this is called the generator will start producing power.</remarks>
  46. /// <param name="uid">Uid of the generator entity.</param>
  47. /// <returns>True if the generator was successfully started, false otherwise.</returns>
  48. private bool StartGenerator(EntityUid uid)
  49. {
  50. var ev = new AutoGeneratorStartedEvent();
  51. RaiseLocalEvent(uid, ref ev);
  52. return ev.Started;
  53. }
  54. /// <summary>
  55. /// Updates the timers on ActiveGeneratorRevvingComponent(s), and stops them when they are finished.
  56. /// </summary>
  57. public override void Update(float frameTime)
  58. {
  59. base.Update(frameTime);
  60. var query = EntityQueryEnumerator<ActiveGeneratorRevvingComponent, PortableGeneratorComponent>();
  61. while (query.MoveNext(out var uid, out var activeGeneratorRevvingComponent, out var portableGeneratorComponent))
  62. {
  63. activeGeneratorRevvingComponent.CurrentTime += TimeSpan.FromSeconds(frameTime);
  64. Dirty(uid, activeGeneratorRevvingComponent);
  65. if (activeGeneratorRevvingComponent.CurrentTime < portableGeneratorComponent.StartTime)
  66. continue;
  67. if (StartGenerator(uid))
  68. StopAutoRevving(uid);
  69. }
  70. }
  71. }