Files
tbd-station-14/Content.Server/Gravity/GravityGeneratorSystem.cs
Justin Pfeifler 4f0b1f377e Gravity Generators cannot be unanchored while active (#41256)
* Add unanchor attempt check

* Combine shared component and server component

- Combines SharedGravityGeneratorComponent and GravityGeneratorComponent
- AutoNetworked the GravityActiveBool

* Remove SharedGravityGeneratorComponent

* Update to SharedGravityGeneratorComponent

* Fix to be a complete sentence

* Dirty GravityActive whenever changed

* Rename component and remove view variables

* Update referenced component name

* Move unanchor attempt to shared system

* Add client system

* Revert popup to PopupEntity

* Fix popup to be PopupClient

* Set access restriction on GravityActive
2025-11-07 00:53:27 +00:00

70 lines
2.4 KiB
C#

using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Gravity;
namespace Content.Server.Gravity;
public sealed class GravityGeneratorSystem : SharedGravityGeneratorSystem
{
[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;
Dirty(ent, ent.Comp);
var xform = Transform(ent);
if (TryComp(xform.ParentUid, out GravityComponent? gravity))
{
_gravitySystem.EnableGravity(xform.ParentUid, gravity);
}
}
private void OnDeactivated(Entity<GravityGeneratorComponent> ent, ref ChargedMachineDeactivatedEvent args)
{
ent.Comp.GravityActive = false;
Dirty(ent, ent.Comp);
var xform = Transform(ent);
if (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);
}
}
}