Fix gravity shenanigans (#12866)
This commit is contained in:
@@ -1,15 +1,5 @@
|
||||
namespace Content.Shared.Gravity
|
||||
{
|
||||
public sealed class GravityChangedEvent : EntityEventArgs
|
||||
{
|
||||
public GravityChangedEvent(EntityUid changedGridIndex, bool newGravityState)
|
||||
{
|
||||
HasGravity = newGravityState;
|
||||
ChangedGridIndex = changedGridIndex;
|
||||
}
|
||||
|
||||
public EntityUid ChangedGridIndex { get; }
|
||||
|
||||
public bool HasGravity { get; }
|
||||
}
|
||||
[ByRefEvent]
|
||||
public readonly record struct GravityChangedEvent(EntityUid ChangedGridIndex, bool HasGravity);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ namespace Content.Shared.Gravity
|
||||
{
|
||||
if (Enabled == value) return;
|
||||
Enabled = value;
|
||||
IoCManager.Resolve<IEntityManager>().EventBus.RaiseLocalEvent(Owner, new GravityChangedEvent(Owner, value));
|
||||
var ev = new GravityChangedEvent(Owner, value);
|
||||
IoCManager.Resolve<IEntityManager>().EventBus.RaiseLocalEvent(Owner, ref ev);
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
17
Content.Shared/Gravity/GravityShakeComponent.cs
Normal file
17
Content.Shared/Gravity/GravityShakeComponent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
||||
|
||||
namespace Content.Shared.Gravity;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates this entity is shaking due to gravity changes.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class GravityShakeComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("shakeTimes")]
|
||||
public int ShakeTimes;
|
||||
|
||||
[DataField("nextShake", customTypeSerializer:typeof(TimeOffsetSerializer))]
|
||||
public TimeSpan NextShake;
|
||||
}
|
||||
87
Content.Shared/Gravity/SharedGravitySystem.Shake.cs
Normal file
87
Content.Shared/Gravity/SharedGravitySystem.Shake.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Gravity;
|
||||
|
||||
public abstract partial class SharedGravitySystem
|
||||
{
|
||||
protected const float GravityKick = 100.0f;
|
||||
protected const float ShakeCooldown = 0.2f;
|
||||
|
||||
private void InitializeShake()
|
||||
{
|
||||
SubscribeLocalEvent<GravityShakeComponent, EntityUnpausedEvent>(OnShakeUnpaused);
|
||||
SubscribeLocalEvent<GravityShakeComponent, ComponentGetState>(OnShakeGetState);
|
||||
SubscribeLocalEvent<GravityShakeComponent, ComponentHandleState>(OnShakeHandleState);
|
||||
}
|
||||
|
||||
private void OnShakeUnpaused(EntityUid uid, GravityShakeComponent component, ref EntityUnpausedEvent args)
|
||||
{
|
||||
component.NextShake += args.PausedTime;
|
||||
}
|
||||
|
||||
private void UpdateShake()
|
||||
{
|
||||
var curTime = Timing.CurTime;
|
||||
var gravityQuery = GetEntityQuery<GravityComponent>();
|
||||
|
||||
foreach (var comp in EntityQuery<GravityShakeComponent>())
|
||||
{
|
||||
if (comp.NextShake <= curTime)
|
||||
{
|
||||
if (comp.ShakeTimes == 0 || !gravityQuery.TryGetComponent(comp.Owner, out var gravity))
|
||||
{
|
||||
RemCompDeferred<GravityShakeComponent>(comp.Owner);
|
||||
continue;
|
||||
}
|
||||
|
||||
ShakeGrid(comp.Owner, gravity);
|
||||
comp.ShakeTimes--;
|
||||
comp.NextShake += TimeSpan.FromSeconds(ShakeCooldown);
|
||||
Dirty(comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StartGridShake(EntityUid uid, GravityComponent? gravity = null)
|
||||
{
|
||||
if (!Resolve(uid, ref gravity, false))
|
||||
return;
|
||||
|
||||
if (!TryComp<GravityShakeComponent>(uid, out var shake))
|
||||
{
|
||||
shake = AddComp<GravityShakeComponent>(uid);
|
||||
shake.NextShake = Timing.CurTime;
|
||||
}
|
||||
|
||||
shake.ShakeTimes = 10;
|
||||
Dirty(shake);
|
||||
}
|
||||
|
||||
protected virtual void ShakeGrid(EntityUid uid, GravityComponent? comp = null) {}
|
||||
|
||||
private void OnShakeHandleState(EntityUid uid, GravityShakeComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not GravityShakeComponentState state)
|
||||
return;
|
||||
|
||||
component.ShakeTimes = state.ShakeTimes;
|
||||
component.NextShake = state.NextShake;
|
||||
}
|
||||
|
||||
private void OnShakeGetState(EntityUid uid, GravityShakeComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new GravityShakeComponentState()
|
||||
{
|
||||
ShakeTimes = component.ShakeTimes,
|
||||
NextShake = component.NextShake,
|
||||
};
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
protected sealed class GravityShakeComponentState : ComponentState
|
||||
{
|
||||
public int ShakeTimes;
|
||||
public TimeSpan NextShake;
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,13 @@ using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.Gravity
|
||||
{
|
||||
public abstract class SharedGravitySystem : EntitySystem
|
||||
public abstract partial class SharedGravitySystem : EntitySystem
|
||||
{
|
||||
[Dependency] protected readonly IGameTiming Timing = default!;
|
||||
[Dependency] private readonly AlertsSystem _alerts = default!;
|
||||
[Dependency] private readonly InventorySystem _inventory = default!;
|
||||
|
||||
@@ -29,8 +31,8 @@ namespace Content.Shared.Gravity
|
||||
return true;
|
||||
|
||||
// If grid / map has gravity
|
||||
if ((TryComp<GravityComponent>(xform.GridUid, out var gravity) ||
|
||||
TryComp(xform.MapUid, out gravity)) && gravity.Enabled)
|
||||
if (TryComp<GravityComponent>(xform.GridUid, out var gravity) && gravity.Enabled ||
|
||||
TryComp(xform.MapUid, out gravity) && gravity.Enabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -50,11 +52,20 @@ namespace Content.Shared.Gravity
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<GridInitializeEvent>(HandleGridInitialize);
|
||||
SubscribeLocalEvent<GridInitializeEvent>(OnGridInit);
|
||||
SubscribeLocalEvent<AlertSyncEvent>(OnAlertsSync);
|
||||
SubscribeLocalEvent<AlertsComponent, EntParentChangedMessage>(OnAlertsParentChange);
|
||||
SubscribeLocalEvent<GravityChangedEvent>(OnGravityChange);
|
||||
SubscribeLocalEvent<GravityComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<GravityComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
InitializeShake();
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
UpdateShake();
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, GravityComponent component, ref ComponentHandleState args)
|
||||
@@ -71,7 +82,7 @@ namespace Content.Shared.Gravity
|
||||
args.State = new GravityComponentState(component.EnabledVV);
|
||||
}
|
||||
|
||||
private void OnGravityChange(GravityChangedEvent ev)
|
||||
private void OnGravityChange(ref GravityChangedEvent ev)
|
||||
{
|
||||
foreach (var (comp, xform) in EntityQuery<AlertsComponent, TransformComponent>(true))
|
||||
{
|
||||
@@ -88,6 +99,18 @@ namespace Content.Shared.Gravity
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAlertsSync(AlertSyncEvent ev)
|
||||
{
|
||||
if (IsWeightless(ev.Euid))
|
||||
{
|
||||
_alerts.ShowAlert(ev.Euid, AlertType.Weightless);
|
||||
}
|
||||
else
|
||||
{
|
||||
_alerts.ClearAlert(ev.Euid, AlertType.Weightless);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAlertsParentChange(EntityUid uid, AlertsComponent component, ref EntParentChangedMessage args)
|
||||
{
|
||||
if (IsWeightless(component.Owner))
|
||||
@@ -100,7 +123,7 @@ namespace Content.Shared.Gravity
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleGridInitialize(GridInitializeEvent ev)
|
||||
private void OnGridInit(GridInitializeEvent ev)
|
||||
{
|
||||
EntityManager.EnsureComponent<GravityComponent>(ev.EntityUid);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public abstract class SharedJetpackSystem : EntitySystem
|
||||
args.CanMove = true;
|
||||
}
|
||||
|
||||
private void OnJetpackUserGravityChanged(GravityChangedEvent ev)
|
||||
private void OnJetpackUserGravityChanged(ref GravityChangedEvent ev)
|
||||
{
|
||||
var gridUid = ev.ChangedGridIndex;
|
||||
var jetpackQuery = GetEntityQuery<JetpackComponent>();
|
||||
|
||||
Reference in New Issue
Block a user