* #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:
@@ -1,4 +1,4 @@
|
||||
#nullable enable
|
||||
#nullable enable
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
@@ -6,13 +6,14 @@ namespace Content.Shared.Gravity
|
||||
{
|
||||
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
|
||||
using Content.Shared.Gravity;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
@@ -14,7 +15,7 @@ namespace Content.Shared.Movement.Components
|
||||
|
||||
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)
|
||||
entity.TryGetComponent(out body);
|
||||
@@ -34,8 +35,11 @@ namespace Content.Shared.Movement.Components
|
||||
|
||||
mapManager ??= IoCManager.Resolve<IMapManager>();
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ namespace Content.Shared.NetIDs
|
||||
public const uint DISASSEMBLE_ON_ACTIVATE = 1089;
|
||||
public const uint LIGHT_REPLACER = 1090;
|
||||
public const uint SINGULARITY_DISTORTION = 1091;
|
||||
public const uint GRAVITY = 1092;
|
||||
|
||||
// Net IDs for integration tests.
|
||||
public const uint PREDICTION_TEST = 10001;
|
||||
|
||||
Reference in New Issue
Block a user