Gravity well improvements (#35027)
* Gravity well improvements - Fixed persistence - Removed dummy fields - Performance * Update Content.Server/Singularity/Components/GravityWellComponent.cs --------- Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
@@ -7,22 +7,20 @@ namespace Content.Server.Singularity.Components;
|
||||
/// The server-side version of <see cref="SharedGravityWellComponent"/>.
|
||||
/// Primarily managed by <see cref="GravityWellSystem"/>.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[RegisterComponent, AutoGenerateComponentPause]
|
||||
public sealed partial class GravityWellComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum range at which the gravity well can push/pull entities.
|
||||
/// </summary>
|
||||
[DataField("maxRange")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public float MaxRange;
|
||||
|
||||
/// <summary>
|
||||
/// The minimum range at which the gravity well can push/pull entities.
|
||||
/// This is effectively hardfloored at <see cref="GravityWellSystem.MinGravPulseRange"/>.
|
||||
/// </summary>
|
||||
[DataField("minRange")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public float MinRange = 0f;
|
||||
|
||||
/// <summary>
|
||||
@@ -30,8 +28,7 @@ public sealed partial class GravityWellComponent : Component
|
||||
/// Negative values accelerate entities away from the gravity well.
|
||||
/// Actual acceleration scales with the inverse of the distance to the singularity.
|
||||
/// </summary>
|
||||
[DataField("baseRadialAcceleration")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public float BaseRadialAcceleration = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
@@ -39,8 +36,7 @@ public sealed partial class GravityWellComponent : Component
|
||||
/// Positive tangential acceleration is counter-clockwise.
|
||||
/// Actual acceleration scales with the inverse of the distance to the singularity.
|
||||
/// </summary>
|
||||
[DataField("baseTangentialAcceleration")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public float BaseTangentialAcceleration = 0.0f;
|
||||
|
||||
#region Update Timing
|
||||
@@ -56,16 +52,14 @@ public sealed partial class GravityWellComponent : Component
|
||||
/// <summary>
|
||||
/// The next time at which this gravity well should pulse.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
[Access(typeof(GravityWellSystem))]
|
||||
[DataField, Access(typeof(GravityWellSystem)), AutoPausedField]
|
||||
public TimeSpan NextPulseTime { get; internal set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The last time this gravity well pulsed.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
[Access(typeof(GravityWellSystem))]
|
||||
public TimeSpan LastPulseTime { get; internal set; } = default!;
|
||||
public TimeSpan LastPulseTime => NextPulseTime - TargetPulsePeriod;
|
||||
|
||||
#endregion Update Timing
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
private EntityQuery<MapGridComponent> _gridQuery;
|
||||
private EntityQuery<PhysicsComponent> _physicsQuery;
|
||||
|
||||
private HashSet<EntityUid> _entSet = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -46,12 +48,17 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
_mapQuery = GetEntityQuery<MapComponent>();
|
||||
_gridQuery = GetEntityQuery<MapGridComponent>();
|
||||
_physicsQuery = GetEntityQuery<PhysicsComponent>();
|
||||
SubscribeLocalEvent<GravityWellComponent, ComponentStartup>(OnGravityWellStartup);
|
||||
SubscribeLocalEvent<GravityWellComponent, MapInitEvent>(OnGravityWellMapInit);
|
||||
|
||||
var vvHandle = _vvManager.GetTypeHandler<GravityWellComponent>();
|
||||
vvHandle.AddPath(nameof(GravityWellComponent.TargetPulsePeriod), (_, comp) => comp.TargetPulsePeriod, SetPulsePeriod);
|
||||
}
|
||||
|
||||
private void OnGravityWellMapInit(Entity<GravityWellComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
ent.Comp.NextPulseTime = _timing.CurTime + ent.Comp.TargetPulsePeriod;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
var vvHandle = _vvManager.GetTypeHandler<GravityWellComponent>();
|
||||
@@ -73,6 +80,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
while (query.MoveNext(out var uid, out var gravWell, out var xform))
|
||||
{
|
||||
var curTime = _timing.CurTime;
|
||||
|
||||
if (gravWell.NextPulseTime <= curTime)
|
||||
Update(uid, curTime - gravWell.LastPulseTime, gravWell, xform);
|
||||
}
|
||||
@@ -103,8 +111,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
if(!Resolve(uid, ref gravWell))
|
||||
return;
|
||||
|
||||
gravWell.LastPulseTime = _timing.CurTime;
|
||||
gravWell.NextPulseTime = gravWell.LastPulseTime + gravWell.TargetPulsePeriod;
|
||||
gravWell.NextPulseTime += gravWell.TargetPulsePeriod;
|
||||
if (gravWell.MaxRange < 0.0f || !Resolve(uid, ref xform))
|
||||
return;
|
||||
|
||||
@@ -195,15 +202,18 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
if (mapPos == MapCoordinates.Nullspace)
|
||||
return; // No gravpulses in nullspace please.
|
||||
|
||||
_entSet.Clear();
|
||||
var epicenter = mapPos.Position;
|
||||
var minRange2 = MathF.Max(minRange * minRange, MinGravPulseRange); // Cache square value for speed. Also apply a sane minimum value to the minimum value so that div/0s don't happen.
|
||||
var bodyQuery = GetEntityQuery<PhysicsComponent>();
|
||||
var xformQuery = GetEntityQuery<TransformComponent>();
|
||||
_lookup.GetEntitiesInRange(mapPos.MapId,
|
||||
epicenter,
|
||||
maxRange,
|
||||
_entSet,
|
||||
flags: LookupFlags.Dynamic | LookupFlags.Sundries);
|
||||
|
||||
foreach(var entity in _lookup.GetEntitiesInRange(mapPos.MapId, epicenter, maxRange, flags: LookupFlags.Dynamic | LookupFlags.Sundries))
|
||||
foreach (var entity in _entSet)
|
||||
{
|
||||
if (!bodyQuery.TryGetComponent(entity, out var physics)
|
||||
|| physics.BodyType == BodyType.Static)
|
||||
if (!_physicsQuery.TryGetComponent(entity, out var physics))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -214,7 +224,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
if(!CanGravPulseAffect(entity))
|
||||
continue;
|
||||
|
||||
var displacement = epicenter - _transform.GetWorldPosition(entity, xformQuery);
|
||||
var displacement = epicenter - _transform.GetWorldPosition(entity);
|
||||
var distance2 = displacement.LengthSquared();
|
||||
if (distance2 < minRange2)
|
||||
continue;
|
||||
@@ -263,20 +273,4 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
|
||||
}
|
||||
|
||||
#endregion Getters/Setters
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Resets the pulse timings of the gravity well when the components starts up.
|
||||
/// </summary>
|
||||
/// <param name="uid">The uid of the gravity well to start up.</param>
|
||||
/// <param name="comp">The state of the gravity well to start up.</param>
|
||||
/// <param name="args">The startup prompt arguments.</param>
|
||||
public void OnGravityWellStartup(EntityUid uid, GravityWellComponent comp, ComponentStartup args)
|
||||
{
|
||||
comp.LastPulseTime = _timing.CurTime;
|
||||
comp.NextPulseTime = comp.LastPulseTime + comp.TargetPulsePeriod;
|
||||
}
|
||||
|
||||
#endregion Event Handlers
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user