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

74 lines
2.3 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]
internal sealed class GravitySystem : SharedGravitySystem
2020-05-02 15:02:52 +01:00
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GravityComponent, ComponentInit>(HandleGravityInitialize);
2022-05-25 13:17:56 -07:00
SubscribeLocalEvent<GravityComponent, ComponentShutdown>(HandleGravityShutdown);
}
private void HandleGravityInitialize(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;
GravityChangedMessage 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;
2022-06-20 12:14:35 +12:00
message = new GravityChangedMessage(gridId.Value, true);
RaiseLocalEvent(message);
return;
2020-05-02 15:02:52 +01:00
}
}
component.Enabled = false;
2022-06-20 12:14:35 +12:00
message = new GravityChangedMessage(gridId.Value, false);
RaiseLocalEvent(message);
}
2022-05-25 13:17:56 -07:00
private void HandleGravityShutdown(EntityUid uid, GravityComponent component, ComponentShutdown args)
{
DisableGravity(component);
}
public void EnableGravity(GravityComponent comp)
{
if (comp.Enabled) return;
2022-06-20 12:14:35 +12:00
var gridId = Transform(comp.Owner).GridUid;
if (gridId == null)
return;
comp.Enabled = true;
var message = new GravityChangedMessage(gridId.Value, true);
RaiseLocalEvent(message);
}
public void DisableGravity(GravityComponent comp)
{
if (!comp.Enabled) return;
comp.Enabled = false;
2022-06-20 12:14:35 +12:00
var gridId = Transform(comp.Owner).GridUid;
if (gridId == null)
return;
var message = new GravityChangedMessage(gridId.Value, false);
RaiseLocalEvent(message);
}
2020-05-02 15:02:52 +01:00
}
}