Files
tbd-station-14/Content.Server/Gravity/GravitySystem.cs
Princess Cheeseballs 9de76e70c7 EVENT BASED WEIGHTLESSNESS (#37971)
* Init Commit

* Typos

* Commit 2

* Save Interaction Test Mob from failing

* ssss

* Confident I've gotten all the correct prototypes

* Whoops forgot to edit those

* aaaaa

* Better solution

* Test fail fixes

* Yaml fix

* THE FINAL TEST FIX

* Final fix(?)

* whoops

* Added a WeightlessnessChangedEvent

* Check out this diff

* Wait I'm dumb

* Final optimization and don't duplicate code

* Death to IsWeightless

* File scoped namespaces

* REVIEW

* Fix test fails

* FIX TEST FAILS REAL

* A

* Commit of doom

* borgar

* We don't need to specify on map init apparently

* Fuck it

* LOAD BEARING COMMENT

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2025-08-19 14:35:09 -04:00

82 lines
2.5 KiB
C#

using Content.Shared.Gravity;
using JetBrains.Annotations;
using Robust.Shared.Map.Components;
namespace Content.Server.Gravity
{
[UsedImplicitly]
public sealed class GravitySystem : SharedGravitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityComponent, ComponentInit>(OnGravityInit);
}
/// <summary>
/// Iterates gravity components and checks if this entity can have gravity applied.
/// </summary>
public void RefreshGravity(EntityUid uid, GravityComponent? gravity = null)
{
if (!GravityQuery.Resolve(uid, ref gravity))
return;
if (gravity.Inherent)
return;
var enabled = false;
foreach (var (comp, xform) in EntityQuery<GravityGeneratorComponent, TransformComponent>(true))
{
if (!comp.GravityActive || xform.ParentUid != uid)
continue;
enabled = true;
break;
}
if (enabled != gravity.Enabled)
{
gravity.Enabled = enabled;
var ev = new GravityChangedEvent(uid, enabled);
RaiseLocalEvent(uid, ref ev, true);
Dirty(uid, gravity);
if (HasComp<MapGridComponent>(uid))
{
StartGridShake(uid);
}
}
}
private void OnGravityInit(EntityUid uid, GravityComponent component, ComponentInit args)
{
RefreshGravity(uid);
}
/// <summary>
/// Enables gravity. Note that this is a fast-path for GravityGeneratorSystem.
/// This means it does nothing if Inherent is set and it might be wiped away with a refresh
/// if you're not supposed to be doing whatever you're doing.
/// </summary>
public void EnableGravity(EntityUid uid, GravityComponent? gravity = null)
{
if (!GravityQuery.Resolve(uid, ref gravity))
return;
if (gravity.Enabled || gravity.Inherent)
return;
gravity.Enabled = true;
var ev = new GravityChangedEvent(uid, true);
RaiseLocalEvent(uid, ref ev, true);
Dirty(uid, gravity);
if (HasComp<MapGridComponent>(uid))
{
StartGridShake(uid);
}
}
}
}