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(OnGravityInit); } /// /// Iterates gravity components and checks if this entity can have gravity applied. /// 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(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(uid)) { StartGridShake(uid); } } } private void OnGravityInit(EntityUid uid, GravityComponent component, ComponentInit args) { RefreshGravity(uid); } /// /// 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. /// 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(uid)) { StartGridShake(uid); } } } }