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

@@ -6,6 +6,18 @@ namespace Content.Server.Shuttles.Components
[ViewVariables]
public bool Enabled = true;
[ViewVariables]
public Vector2[] CenterOfThrust = new Vector2[4];
/// <summary>
/// Thrust gets multiplied by this value if it's for braking.
/// </summary>
public const float BrakeCoefficient = 1.5f;
public const float MaxLinearVelocity = 10f;
public const float MaxAngularVelocity = 1f;
/// <summary>
/// The cached thrust available for each cardinal direction
/// </summary>

View File

@@ -46,10 +46,10 @@ namespace Content.Server.Shuttles.Components
// Need to serialize this because RefreshParts isn't called on Init and this will break post-mapinit maps!
[ViewVariables(VVAccess.ReadWrite), DataField("thrust")]
public float Thrust;
public float Thrust = 100f;
[DataField("baseThrust"), ViewVariables(VVAccess.ReadWrite)]
public float BaseThrust = 750f;
public float BaseThrust = 100f;
[DataField("thrusterType")]
public ThrusterType Type = ThrusterType.Linear;

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)