* #1462 extracted MapGrid's HasGravity property to separate Content component * #1462 - PR suggestions * Merge to master Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
9
Content.Client/Gravity/GravitySystem.cs
Normal file
9
Content.Client/Gravity/GravitySystem.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using Content.Shared.Gravity;
|
||||||
|
|
||||||
|
namespace Content.Client.Gravity
|
||||||
|
{
|
||||||
|
internal sealed class GravitySystem : SharedGravitySystem
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Content.Server.Gravity;
|
using Content.Server.Gravity;
|
||||||
using Content.Server.Power.Components;
|
using Content.Server.Power.Components;
|
||||||
using Content.Shared.Coordinates;
|
using Content.Shared.Coordinates;
|
||||||
@@ -62,8 +62,12 @@ namespace Content.IntegrationTests.Tests
|
|||||||
|
|
||||||
Assert.That(generatorComponent.Status, Is.EqualTo(GravityGeneratorStatus.On));
|
Assert.That(generatorComponent.Status, Is.EqualTo(GravityGeneratorStatus.On));
|
||||||
|
|
||||||
Assert.That(!grid1.HasGravity);
|
var entityMan = IoCManager.Resolve<IEntityManager>();
|
||||||
Assert.That(grid2.HasGravity);
|
var grid1Entity = entityMan.GetEntity(grid1.GridEntityId);
|
||||||
|
var grid2Entity = entityMan.GetEntity(grid2.GridEntityId);
|
||||||
|
|
||||||
|
Assert.That(!grid1Entity.HasComponent<GravityComponent>());
|
||||||
|
Assert.That(grid2Entity.HasComponent<GravityComponent>());
|
||||||
});
|
});
|
||||||
|
|
||||||
await server.WaitIdleAsync();
|
await server.WaitIdleAsync();
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ using Robust.Shared.Random;
|
|||||||
namespace Content.Server.Gravity.EntitySystems
|
namespace Content.Server.Gravity.EntitySystems
|
||||||
{
|
{
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
internal sealed class GravitySystem : EntitySystem
|
internal sealed class GravitySystem : SharedGravitySystem
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||||
@@ -29,59 +29,95 @@ namespace Content.Server.Gravity.EntitySystems
|
|||||||
|
|
||||||
private float _internalTimer = 0.0f;
|
private float _internalTimer = 0.0f;
|
||||||
|
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
base.Initialize();
|
||||||
|
SubscribeLocalEvent<GravityComponent, ComponentInit>(HandleGravityInitialize);
|
||||||
|
SubscribeLocalEvent<GravityGeneratorUpdateEvent>(HandleGeneratorUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleGeneratorUpdate(GravityGeneratorUpdateEvent ev)
|
||||||
|
{
|
||||||
|
if (ev.GridId == GridId.Invalid) return;
|
||||||
|
|
||||||
|
var gravity = ComponentManager.GetComponent<GravityComponent>(_mapManager.GetGrid(ev.GridId).GridEntityId);
|
||||||
|
|
||||||
|
if (ev.Status == GravityGeneratorStatus.On)
|
||||||
|
{
|
||||||
|
EnableGravity(gravity);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DisableGravity(gravity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleGravityInitialize(EntityUid uid, GravityComponent component, ComponentInit args)
|
||||||
|
{
|
||||||
|
// Incase there's already a generator on the grid we'll just set it now.
|
||||||
|
var gridId = component.Owner.Transform.GridID;
|
||||||
|
|
||||||
|
foreach (var generator in ComponentManager.EntityQuery<GravityGeneratorComponent>(true))
|
||||||
|
{
|
||||||
|
if (generator.Owner.Transform.GridID == gridId && generator.Status == GravityGeneratorStatus.On)
|
||||||
|
{
|
||||||
|
component.Enabled = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
component.Enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
public override void Update(float frameTime)
|
public override void Update(float frameTime)
|
||||||
{
|
{
|
||||||
_internalTimer += frameTime;
|
// TODO: Pointless iteration, just make both of these event-based PLEASE
|
||||||
var gridsWithGravity = new List<GridId>();
|
|
||||||
foreach (var generator in ComponentManager.EntityQuery<GravityGeneratorComponent>(true))
|
foreach (var generator in ComponentManager.EntityQuery<GravityGeneratorComponent>(true))
|
||||||
{
|
{
|
||||||
if (generator.NeedsUpdate)
|
if (generator.NeedsUpdate)
|
||||||
{
|
{
|
||||||
generator.UpdateState();
|
generator.UpdateState();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (generator.Status == GravityGeneratorStatus.On)
|
|
||||||
{
|
|
||||||
gridsWithGravity.Add(generator.Owner.Transform.GridID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var grid in _mapManager.GetAllGrids())
|
if (_gridsToShake.Count > 0)
|
||||||
{
|
{
|
||||||
if (grid.HasGravity && !gridsWithGravity.Contains(grid.Index))
|
_internalTimer += frameTime;
|
||||||
|
|
||||||
|
if (_internalTimer > 0.2f)
|
||||||
{
|
{
|
||||||
DisableGravity(grid);
|
// TODO: Could just have clients do this themselves via event and save bandwidth.
|
||||||
}
|
ShakeGrids();
|
||||||
else if (!grid.HasGravity && gridsWithGravity.Contains(grid.Index))
|
_internalTimer -= 0.2f;
|
||||||
{
|
|
||||||
EnableGravity(grid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (_internalTimer > 0.2f)
|
|
||||||
{
|
{
|
||||||
ShakeGrids();
|
|
||||||
_internalTimer = 0.0f;
|
_internalTimer = 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnableGravity(IMapGrid grid)
|
private void EnableGravity(GravityComponent comp)
|
||||||
{
|
{
|
||||||
grid.HasGravity = true;
|
if (comp.Enabled) return;
|
||||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
comp.Enabled = true;
|
||||||
|
|
||||||
var message = new GravityChangedMessage(grid);
|
var gridId = comp.Owner.Transform.GridID;
|
||||||
|
ScheduleGridToShake(gridId, ShakeTimes);
|
||||||
|
|
||||||
|
var message = new GravityChangedMessage(gridId, true);
|
||||||
RaiseLocalEvent(message);
|
RaiseLocalEvent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DisableGravity(IMapGrid grid)
|
private void DisableGravity(GravityComponent comp)
|
||||||
{
|
{
|
||||||
grid.HasGravity = false;
|
if (!comp.Enabled) return;
|
||||||
ScheduleGridToShake(grid.Index, ShakeTimes);
|
comp.Enabled = false;
|
||||||
|
|
||||||
var message = new GravityChangedMessage(grid);
|
var gridId = comp.Owner.Transform.GridID;
|
||||||
|
ScheduleGridToShake(gridId, ShakeTimes);
|
||||||
|
|
||||||
|
var message = new GravityChangedMessage(gridId, false);
|
||||||
RaiseLocalEvent(message);
|
RaiseLocalEvent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Content.Server.Alert;
|
using Content.Server.Alert;
|
||||||
using Content.Shared.Alert;
|
using Content.Shared.Alert;
|
||||||
using Content.Shared.GameTicking;
|
using Content.Shared.GameTicking;
|
||||||
@@ -40,7 +40,8 @@ namespace Content.Server.Gravity.EntitySystems
|
|||||||
|
|
||||||
if (_mapManager.TryGetGrid(status.Owner.Transform.GridID, out var grid))
|
if (_mapManager.TryGetGrid(status.Owner.Transform.GridID, out var grid))
|
||||||
{
|
{
|
||||||
if (grid.HasGravity)
|
var gridEntity = EntityManager.GetEntity(grid.GridEntityId);
|
||||||
|
if (gridEntity.HasComponent<GravityComponent>())
|
||||||
{
|
{
|
||||||
RemoveWeightless(status);
|
RemoveWeightless(status);
|
||||||
}
|
}
|
||||||
@@ -64,7 +65,7 @@ namespace Content.Server.Gravity.EntitySystems
|
|||||||
|
|
||||||
private void GravityChanged(GravityChangedMessage ev)
|
private void GravityChanged(GravityChangedMessage ev)
|
||||||
{
|
{
|
||||||
if (!_alerts.TryGetValue(ev.Grid.Index, out var statuses))
|
if (!_alerts.TryGetValue(ev.ChangedGridIndex, out var statuses))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using Content.Shared.Interaction;
|
|||||||
using Robust.Server.GameObjects;
|
using Robust.Server.GameObjects;
|
||||||
using Robust.Server.Player;
|
using Robust.Server.Player;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.Map;
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
using Robust.Shared.ViewVariables;
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
@@ -108,6 +109,9 @@ namespace Content.Server.Gravity
|
|||||||
{
|
{
|
||||||
MakeOn();
|
MakeOn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var msg = new GravityGeneratorUpdateEvent(Owner.Transform.GridID, Status);
|
||||||
|
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleUIMessage(ServerBoundUserInterfaceMessage message)
|
private void HandleUIMessage(ServerBoundUserInterfaceMessage message)
|
||||||
@@ -163,4 +167,16 @@ namespace Content.Server.Gravity
|
|||||||
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, true);
|
_appearance?.SetData(GravityGeneratorVisuals.CoreVisible, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class GravityGeneratorUpdateEvent : EntityEventArgs
|
||||||
|
{
|
||||||
|
public GridId GridId { get; }
|
||||||
|
public GravityGeneratorStatus Status { get; }
|
||||||
|
|
||||||
|
public GravityGeneratorUpdateEvent(GridId gridId, GravityGeneratorStatus status)
|
||||||
|
{
|
||||||
|
GridId = gridId;
|
||||||
|
Status = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
|
|
||||||
@@ -6,13 +6,14 @@ namespace Content.Shared.Gravity
|
|||||||
{
|
{
|
||||||
public class GravityChangedMessage : EntityEventArgs
|
public class GravityChangedMessage : EntityEventArgs
|
||||||
{
|
{
|
||||||
public GravityChangedMessage(IMapGrid grid)
|
public GravityChangedMessage(GridId changedGridIndex, bool newGravityState)
|
||||||
{
|
{
|
||||||
Grid = grid;
|
HasGravity = newGravityState;
|
||||||
|
ChangedGridIndex = changedGridIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IMapGrid Grid { get; }
|
public GridId ChangedGridIndex { get; }
|
||||||
|
|
||||||
public bool HasGravity => Grid.HasGravity;
|
public bool HasGravity { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
Content.Shared/Gravity/GravityComponent.cs
Normal file
62
Content.Shared/Gravity/GravityComponent.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
using System;
|
||||||
|
using Content.Shared.NetIDs;
|
||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.Log;
|
||||||
|
using Robust.Shared.Players;
|
||||||
|
using Robust.Shared.Serialization;
|
||||||
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
|
namespace Content.Shared.Gravity
|
||||||
|
{
|
||||||
|
[RegisterComponent]
|
||||||
|
public sealed class GravityComponent : Component
|
||||||
|
{
|
||||||
|
public override string Name => "Gravity";
|
||||||
|
public override uint? NetID => ContentNetIDs.GRAVITY;
|
||||||
|
|
||||||
|
[ViewVariables(VVAccess.ReadWrite)]
|
||||||
|
public bool Enabled
|
||||||
|
{
|
||||||
|
get => _enabled;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_enabled == value) return;
|
||||||
|
_enabled = value;
|
||||||
|
if (_enabled)
|
||||||
|
{
|
||||||
|
Logger.Info($"Enabled gravity for {Owner}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger.Info($"Disabled gravity for {Owner}");
|
||||||
|
}
|
||||||
|
Dirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool _enabled;
|
||||||
|
|
||||||
|
public override ComponentState GetComponentState(ICommonSession player)
|
||||||
|
{
|
||||||
|
return new GravityComponentState(_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||||
|
{
|
||||||
|
base.HandleComponentState(curState, nextState);
|
||||||
|
if (curState is not GravityComponentState state) return;
|
||||||
|
Enabled = state.Enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable, NetSerializable]
|
||||||
|
protected sealed class GravityComponentState : ComponentState
|
||||||
|
{
|
||||||
|
public bool Enabled { get; }
|
||||||
|
|
||||||
|
public GravityComponentState(bool enabled) : base(ContentNetIDs.GRAVITY)
|
||||||
|
{
|
||||||
|
Enabled = enabled;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Content.Shared/Gravity/SharedGravitySystem.cs
Normal file
19
Content.Shared/Gravity/SharedGravitySystem.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
|
||||||
|
namespace Content.Shared.Gravity
|
||||||
|
{
|
||||||
|
public abstract class SharedGravitySystem : EntitySystem
|
||||||
|
{
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
base.Initialize();
|
||||||
|
SubscribeLocalEvent<GridInitializedEvent>(HandleGridInitialize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleGridInitialize(GridInitializedEvent ev)
|
||||||
|
{
|
||||||
|
var gridEnt = EntityManager.GetEntity(ev.EntityUid);
|
||||||
|
gridEnt.EnsureComponent<GravityComponent>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
|
using Content.Shared.Gravity;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
@@ -14,7 +15,7 @@ namespace Content.Shared.Movement.Components
|
|||||||
|
|
||||||
public static class GravityExtensions
|
public static class GravityExtensions
|
||||||
{
|
{
|
||||||
public static bool IsWeightless(this IEntity entity, PhysicsComponent? body = null, EntityCoordinates? coords = null, IMapManager? mapManager = null)
|
public static bool IsWeightless(this IEntity entity, PhysicsComponent? body = null, EntityCoordinates? coords = null, IMapManager? mapManager = null, IEntityManager? entityManager = null)
|
||||||
{
|
{
|
||||||
if (body == null)
|
if (body == null)
|
||||||
entity.TryGetComponent(out body);
|
entity.TryGetComponent(out body);
|
||||||
@@ -34,8 +35,11 @@ namespace Content.Shared.Movement.Components
|
|||||||
|
|
||||||
mapManager ??= IoCManager.Resolve<IMapManager>();
|
mapManager ??= IoCManager.Resolve<IMapManager>();
|
||||||
var grid = mapManager.GetGrid(gridId);
|
var grid = mapManager.GetGrid(gridId);
|
||||||
|
var gridEntityId = grid.GridEntityId;
|
||||||
|
entityManager ??= IoCManager.Resolve<IEntityManager>();
|
||||||
|
var gridEntity = entityManager.GetEntity(gridEntityId);
|
||||||
|
|
||||||
if (!grid.HasGravity)
|
if (!gridEntity.GetComponent<GravityComponent>().Enabled)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ namespace Content.Shared.NetIDs
|
|||||||
public const uint DISASSEMBLE_ON_ACTIVATE = 1089;
|
public const uint DISASSEMBLE_ON_ACTIVATE = 1089;
|
||||||
public const uint LIGHT_REPLACER = 1090;
|
public const uint LIGHT_REPLACER = 1090;
|
||||||
public const uint SINGULARITY_DISTORTION = 1091;
|
public const uint SINGULARITY_DISTORTION = 1091;
|
||||||
|
public const uint GRAVITY = 1092;
|
||||||
|
|
||||||
// Net IDs for integration tests.
|
// Net IDs for integration tests.
|
||||||
public const uint PREDICTION_TEST = 10001;
|
public const uint PREDICTION_TEST = 10001;
|
||||||
|
|||||||
Reference in New Issue
Block a user