Adds signal control to portable generators (#24157)
* added signal control to portable generators * added documentation * Discard changes to Content.Server/Radio/EntitySystems/HeadsetSystem.cs * added DeviceNetworkComponent and WirelessNetworkConnectionComponent to generator prototype * made GeneratorSignalControlComponent nicer * implemented auto-revving * added back necessary dependency * can't send do-after event manually * repeat now works with auto revving * fixed * removed vv * stopping generating when it is revving now makes it stop revving * Update Content.Shared/Power/Generator/ActiveGeneratorRevvingComponent.cs Co-authored-by: Kara <lunarautomaton6@gmail.com> * used resolve instead of TryComp --------- Co-authored-by: Julian Giebel <juliangiebel@live.de> Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
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<ActiveGeneratorRevvingComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the AnchorStateChangedEvent to stop auto-revving when unanchored.
|
||||
/// </summary>
|
||||
private void OnAnchorStateChanged(EntityUid uid, ActiveGeneratorRevvingComponent component, AnchorStateChangedEvent args)
|
||||
{
|
||||
if (!args.Anchored)
|
||||
StopAutoRevving(uid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start revving a generator entity automatically, without another entity doing a do-after.
|
||||
/// Used for remotely activating a generator.
|
||||
/// </summary>
|
||||
/// <param name="uid">Uid of the generator entity.</param>
|
||||
/// <param name="component">ActiveGeneratorRevvingComponent of the generator entity.</param>
|
||||
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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop revving a generator entity.
|
||||
/// </summary>
|
||||
/// <param name="uid">Uid of the generator entity.</param>
|
||||
/// <returns>True if the auto-revving was cancelled, false if it was never revving in the first place.</returns>
|
||||
public bool StopAutoRevving(EntityUid uid)
|
||||
{
|
||||
return RemComp<ActiveGeneratorRevvingComponent>(uid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raise an event on a generator entity to start it.
|
||||
/// </summary>
|
||||
/// <remarks>This is not the same as revving it, when this is called the generator will start producing power.</remarks>
|
||||
/// <param name="uid">Uid of the generator entity.</param>
|
||||
/// <returns>True if the generator was successfully started, false otherwise.</returns>
|
||||
private bool StartGenerator(EntityUid uid)
|
||||
{
|
||||
var ev = new AutoGeneratorStartedEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
return ev.Started;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the timers on ActiveGeneratorRevvingComponent(s), and stops them when they are finished.
|
||||
/// </summary>
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
var query = EntityQueryEnumerator<ActiveGeneratorRevvingComponent, PortableGeneratorComponent>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user