Refactor thrusters (#15698)

This commit is contained in:
metalgearsloth
2023-04-29 18:17:31 +10:00
committed by GitHub
parent b27f33fb1f
commit 1515a3faff
5 changed files with 105 additions and 121 deletions

View File

@@ -13,6 +13,7 @@ using Content.Shared.Shuttles.Components;
using Content.Shared.Temperature;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
@@ -295,6 +296,38 @@ public sealed class ThrusterSystem : EntitySystem
}
_ambient.SetAmbience(uid, true);
RefreshCenter(uid, shuttleComponent);
}
/// <summary>
/// Refreshes the center of thrust for movement calculations.
/// </summary>
private void RefreshCenter(EntityUid uid, ShuttleComponent shuttle)
{
// TODO: Only refresh relevant directions.
var center = Vector2.Zero;
var thrustQuery = GetEntityQuery<ThrusterComponent>();
var xformQuery = GetEntityQuery<TransformComponent>();
foreach (var dir in new[]
{ Direction.South, Direction.East, Direction.North, Direction.West })
{
var index = (int) dir / 2;
var pop = shuttle.LinearThrusters[index];
var totalThrust = 0f;
foreach (var ent in pop)
{
if (!thrustQuery.TryGetComponent(ent, out var thruster) || !xformQuery.TryGetComponent(ent, out var xform))
continue;
center += xform.LocalPosition * thruster.Thrust;
totalThrust += thruster.Thrust;
}
center /= pop.Count * totalThrust;
shuttle.CenterOfThrust[index] = center;
}
}
public void DisableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null, Angle? angle = null)
@@ -358,6 +391,7 @@ public sealed class ThrusterSystem : EntitySystem
}
component.Colliding.Clear();
RefreshCenter(uid, shuttleComponent);
}
public bool CanEnable(EntityUid uid, ThrusterComponent component)