Files
crystall-punk-14/Content.Server/Gravity/EntitySystems/GravitySystem.cs

81 lines
2.4 KiB
C#
Raw Normal View History

2021-06-09 22:19:39 +02:00
using Content.Shared.Gravity;
2020-05-02 15:02:52 +01:00
using JetBrains.Annotations;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Gravity.EntitySystems
2020-05-02 15:02:52 +01:00
{
[UsedImplicitly]
public sealed class GravitySystem : SharedGravitySystem
2020-05-02 15:02:52 +01:00
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityComponent, ComponentInit>(OnGravityInit);
SubscribeLocalEvent<GravityComponent, ComponentShutdown>(OnGravityShutdown);
}
private void OnGravityInit(EntityUid uid, GravityComponent component, ComponentInit args)
2020-05-02 15:02:52 +01:00
{
// Incase there's already a generator on the grid we'll just set it now.
2022-06-20 12:14:35 +12:00
var gridId = Transform(component.Owner).GridUid;
if (gridId == null)
return;
GravityChangedEvent message;
foreach (var generator in EntityManager.EntityQuery<GravityGeneratorComponent>())
2020-05-02 15:02:52 +01:00
{
2022-06-20 12:14:35 +12:00
if (Transform(generator.Owner).GridUid == gridId && generator.GravityActive)
2020-05-02 15:02:52 +01:00
{
component.Enabled = true;
message = new GravityChangedEvent(gridId.Value, true);
RaiseLocalEvent(message);
return;
2020-05-02 15:02:52 +01:00
}
}
component.Enabled = false;
message = new GravityChangedEvent(gridId.Value, false);
RaiseLocalEvent(message);
}
private void OnGravityShutdown(EntityUid uid, GravityComponent component, ComponentShutdown args)
2022-05-25 13:17:56 -07:00
{
DisableGravity(component);
}
public void EnableGravity(GravityComponent comp)
{
2022-07-25 16:55:24 +10:00
if (comp.Enabled)
return;
2022-06-20 12:14:35 +12:00
var gridId = Transform(comp.Owner).GridUid;
2022-07-25 16:55:24 +10:00
Dirty(comp);
2022-06-20 12:14:35 +12:00
if (gridId == null)
return;
comp.Enabled = true;
var message = new GravityChangedEvent(gridId.Value, true);
RaiseLocalEvent(message);
2022-07-25 16:55:24 +10:00
}
public void DisableGravity(GravityComponent comp)
{
2022-07-25 16:55:24 +10:00
if (!comp.Enabled)
return;
comp.Enabled = false;
2022-07-25 16:55:24 +10:00
Dirty(comp);
2022-06-20 12:14:35 +12:00
var gridId = Transform(comp.Owner).GridUid;
if (gridId == null)
return;
var message = new GravityChangedEvent(gridId.Value, false);
RaiseLocalEvent(message);
}
2020-05-02 15:02:52 +01:00
}
}