Files
tbd-station-14/Content.Server/Gravity/GravityGeneratorSystem.cs
Julian Giebel 417d3a87a2 Station Anchor (#26098)
* Work on abstracting out chargeup functionality/ui from grav gen

* Work on station anchor

* Finish implementing station anchors

* uhh yeah

* ok.

* fix tests

* whoops

* Get the last extraneous yaml fail

* PJB review

* beast mode... ACTIVATE!

---------

Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
2024-08-31 10:40:28 -04:00

64 lines
2.3 KiB
C#

using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Gravity;
namespace Content.Server.Gravity;
public sealed class GravityGeneratorSystem : EntitySystem
{
[Dependency] private readonly GravitySystem _gravitySystem = default!;
[Dependency] private readonly SharedPointLightSystem _lights = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityGeneratorComponent, EntParentChangedMessage>(OnParentChanged);
SubscribeLocalEvent<GravityGeneratorComponent, ChargedMachineActivatedEvent>(OnActivated);
SubscribeLocalEvent<GravityGeneratorComponent, ChargedMachineDeactivatedEvent>(OnDeactivated);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<GravityGeneratorComponent, PowerChargeComponent>();
while (query.MoveNext(out var uid, out var grav, out var charge))
{
if (!_lights.TryGetLight(uid, out var pointLight))
continue;
_lights.SetEnabled(uid, charge.Charge > 0, pointLight);
_lights.SetRadius(uid, MathHelper.Lerp(grav.LightRadiusMin, grav.LightRadiusMax, charge.Charge),
pointLight);
}
}
private void OnActivated(Entity<GravityGeneratorComponent> ent, ref ChargedMachineActivatedEvent args)
{
ent.Comp.GravityActive = true;
if (TryComp<TransformComponent>(ent, out var xform) &&
TryComp(xform.ParentUid, out GravityComponent? gravity))
{
_gravitySystem.EnableGravity(xform.ParentUid, gravity);
}
}
private void OnDeactivated(Entity<GravityGeneratorComponent> ent, ref ChargedMachineDeactivatedEvent args)
{
ent.Comp.GravityActive = false;
if (TryComp<TransformComponent>(ent, out var xform) &&
TryComp(xform.ParentUid, out GravityComponent? gravity))
{
_gravitySystem.RefreshGravity(xform.ParentUid, gravity);
}
}
private void OnParentChanged(EntityUid uid, GravityGeneratorComponent component, ref EntParentChangedMessage args)
{
if (component.GravityActive && TryComp(args.OldParent, out GravityComponent? gravity))
{
_gravitySystem.RefreshGravity(args.OldParent.Value, gravity);
}
}
}