using Content.Shared.DoAfter; namespace Content.Shared.Power.Generator; public sealed class ActiveGeneratorRevvingSystem: EntitySystem { [Dependency] private readonly SharedDoAfterSystem _doAfter = default!; [Dependency] private readonly EntityManager _entity = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnAnchorStateChanged); } /// /// Handles the AnchorStateChangedEvent to stop auto-revving when unanchored. /// private void OnAnchorStateChanged(EntityUid uid, ActiveGeneratorRevvingComponent component, AnchorStateChangedEvent args) { if (!args.Anchored) StopAutoRevving(uid); } /// /// Start revving a generator entity automatically, without another entity doing a do-after. /// Used for remotely activating a generator. /// /// Uid of the generator entity. /// ActiveGeneratorRevvingComponent of the generator entity. public void StartAutoRevving(EntityUid uid, ActiveGeneratorRevvingComponent? component = null) { if (Resolve(uid, ref component)) { // reset the revving component.CurrentTime = TimeSpan.FromSeconds(0); return; } AddComp(uid, new ActiveGeneratorRevvingComponent()); } /// /// Stop revving a generator entity. /// /// Uid of the generator entity. /// True if the auto-revving was cancelled, false if it was never revving in the first place. public bool StopAutoRevving(EntityUid uid) { return RemComp(uid); } /// /// Raise an event on a generator entity to start it. /// /// This is not the same as revving it, when this is called the generator will start producing power. /// Uid of the generator entity. /// True if the generator was successfully started, false otherwise. private bool StartGenerator(EntityUid uid) { var ev = new AutoGeneratorStartedEvent(); RaiseLocalEvent(uid, ref ev); return ev.Started; } /// /// Updates the timers on ActiveGeneratorRevvingComponent(s), and stops them when they are finished. /// public override void Update(float frameTime) { base.Update(frameTime); var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var activeGeneratorRevvingComponent, out var portableGeneratorComponent)) { activeGeneratorRevvingComponent.CurrentTime += TimeSpan.FromSeconds(frameTime); Dirty(uid, activeGeneratorRevvingComponent); if (activeGeneratorRevvingComponent.CurrentTime < portableGeneratorComponent.StartTime) continue; if (StartGenerator(uid)) StopAutoRevving(uid); } } }