Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Update ThrusterSystem.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vonsant authored May 4, 2024
1 parent e04dd4c commit 256ce9b
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions Content.Server/Shuttles/Systems/ThrusterSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@ private void OnShuttleTileChange(EntityUid uid, ShuttleComponent component, ref
}
}

private DirectionFlag IndexToDirectionFlag(int index)
{
return index switch
{
0 => DirectionFlag.South,
1 => DirectionFlag.East,
2 => DirectionFlag.North,
3 => DirectionFlag.West,
_ => throw new ArgumentOutOfRangeException(nameof(index))
};
}

private void OnActivateThruster(EntityUid uid, ThrusterComponent component, ActivateInWorldEvent args)
{
component.Enabled ^= true;
Expand Down Expand Up @@ -285,7 +273,7 @@ private void OnPowerChange(EntityUid uid, ThrusterComponent component, ref Power
/// <summary>
/// Tries to enable the thruster and turn it on. If it's already enabled it does nothing.
/// </summary>
public void EnableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null)
public void EnableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null)
{
if (component.IsOn ||
!Resolve(uid, ref xform))
Expand Down Expand Up @@ -323,7 +311,7 @@ public void EnableThruster(EntityUid uid, ThrusterComponent component, Transform
shape.Set(component.BurnPoly);
_fixtureSystem.TryCreateFixture(uid, shape, BurnFixture, hard: false, collisionLayer: (int) CollisionGroup.FullTileMask, body: physicsComponent);
}
RefreshCenter(uid, shuttleComponent, IndexToDirectionFlag(direction), changedDirection);

break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust += component.Thrust;
Expand Down Expand Up @@ -351,27 +339,32 @@ public void EnableThruster(EntityUid uid, ThrusterComponent component, Transform
/// <summary>
/// Refreshes the center of thrust for movement calculations.
/// </summary>
private void RefreshCenter(EntityUid uid, ShuttleComponent shuttle, DirectionFlag changedDirection, DirectionFlag? originalChangedDirection = null)
private void RefreshCenter(EntityUid uid, ShuttleComponent shuttle)
{
// TODO: Only refresh relevant directions.
var center = Vector2.Zero;
var thrustQuery = GetEntityQuery<ThrusterComponent>();
var xformQuery = GetEntityQuery<TransformComponent>();

var index = GetFlagIndex(changedDirection);
var pop = shuttle.LinearThrusters[index];
var totalThrust = 0f;

foreach (var ent in pop)
foreach (var dir in new[]
{ Direction.South, Direction.East, Direction.North, Direction.West })
{
if (!thrustQuery.TryGetComponent(ent, out var thruster) || !xformQuery.TryGetComponent(ent, out var xform))
continue;
var index = (int) dir / 2;
var pop = shuttle.LinearThrusters[index];
var totalThrust = 0f;

center += xform.LocalPosition * thruster.Thrust;
totalThrust += thruster.Thrust;
}
foreach (var ent in pop)
{
if (!thrustQuery.TryGetComponent(ent, out var thruster) || !xformQuery.TryGetComponent(ent, out var xform))
continue;

center /= pop.Count * totalThrust;
shuttle.CenterOfThrust[index] = center;
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)
Expand Down Expand Up @@ -413,7 +406,6 @@ public void DisableThruster(EntityUid uid, ThrusterComponent component, EntityUi
shuttleComponent.BaseLinearThrust[direction] -= component.BaseThrust;
DebugTools.Assert(shuttleComponent.LinearThrusters[direction].Contains(uid));
shuttleComponent.LinearThrusters[direction].Remove(uid);
RefreshCenter(uid, shuttleComponent, IndexToDirectionFlag(direction), changedDirection);
break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust -= component.Thrust;
Expand Down

0 comments on commit 256ce9b

Please sign in to comment.