Add docking window to shuttle consoles (#8756)

* lewd

* A

* Realtime

* Sleepy dork

* Draw radar position

* Accurate infiltrator

* experiments

* Better drawing

* Labels

* I need aan adult

* Cleanup

* Show toggles

* display I guess

* A

* fix

* fix

* cleanupsies

* Bit more polish

* Make sure mass scanners actually work

* Remove dummy state

* fren

* opposite

* aghost crash

* comment

* What's in a name

* woops

* Show docking ports

* Dock highlighting

* Drawing dock

* Shitty docks

* Lots of docking drawing

* Autodock working

* dork

* More graceful shutdown

* zoomies

* Lines and distance change

* revert

* Good enough

* cleanup

* Fix default range

* Dock UI and loc update

* Update on undock

* Loc fixes
This commit is contained in:
metalgearsloth
2022-06-16 15:28:16 +10:00
committed by GitHub
parent 2c5f492e1a
commit 1e82d0c7ed
52 changed files with 1935 additions and 666 deletions

View File

@@ -0,0 +1,104 @@
using Content.Server.Shuttles.Components;
using Content.Shared.Shuttles.Components;
using Content.Shared.Shuttles.Events;
using Robust.Shared.Players;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems;
public sealed partial class DockingSystem
{
private void UpdateAutodock()
{
// Work out what we can autodock with, what we shouldn't, and when we should stop tracking.
// Autodocking only stops when the client closes that dock viewport OR they lose pilotcomponent.
var dockingQuery = GetEntityQuery<DockingComponent>();
var xformQuery = GetEntityQuery<TransformComponent>();
var recentQuery = GetEntityQuery<RecentlyDockedComponent>();
foreach (var (comp, body) in EntityQuery<AutoDockComponent, PhysicsComponent>())
{
if (comp.Requesters.Count == 0 || !dockingQuery.TryGetComponent(comp.Owner, out var dock))
{
RemComp<AutoDockComponent>(comp.Owner);
continue;
}
// Don't re-dock if we're already docked or recently were.
if (dock.Docked || recentQuery.HasComponent(comp.Owner)) continue;
var dockable = GetDockable(body, xformQuery.GetComponent(comp.Owner));
if (dockable == null) continue;
TryDock(dock, dockable);
}
// Work out recent docks that have gone past their designated threshold.
var checkedRecent = new HashSet<EntityUid>();
foreach (var (comp, xform) in EntityQuery<RecentlyDockedComponent, TransformComponent>())
{
if (!checkedRecent.Add(comp.Owner)) continue;
if (!dockingQuery.TryGetComponent(comp.Owner, out var dock))
{
RemComp<RecentlyDockedComponent>(comp.Owner);
continue;
}
if (!xformQuery.TryGetComponent(comp.LastDocked, out var otherXform))
{
RemComp<RecentlyDockedComponent>(comp.Owner);
continue;
}
var worldPos = _transformSystem.GetWorldPosition(xform, xformQuery);
var otherWorldPos = _transformSystem.GetWorldPosition(otherXform, xformQuery);
if ((worldPos - otherWorldPos).Length < comp.Radius) continue;
_sawmill.Debug($"Removed RecentlyDocked from {ToPrettyString(comp.Owner)} and {ToPrettyString(comp.LastDocked)}");
RemComp<RecentlyDockedComponent>(comp.Owner);
RemComp<RecentlyDockedComponent>(comp.LastDocked);
}
}
private void OnRequestUndock(EntityUid uid, ShuttleConsoleComponent component, UndockRequestMessage args)
{
_sawmill.Debug($"Received undock request for {ToPrettyString(args.Entity)}");
// TODO: Validation
if (!TryComp<DockingComponent>(args.Entity, out var dock) ||
!dock.Docked) return;
Undock(dock);
}
private void OnRequestAutodock(EntityUid uid, ShuttleConsoleComponent component, AutodockRequestMessage args)
{
_sawmill.Debug($"Received autodock request for {ToPrettyString(args.Entity)}");
var player = args.Session.AttachedEntity;
if (player == null || !HasComp<DockingComponent>(args.Entity)) return;
// TODO: Validation
var comp = EnsureComp<AutoDockComponent>(args.Entity);
comp.Requesters.Add(player.Value);
}
private void OnRequestStopAutodock(EntityUid uid, ShuttleConsoleComponent component, StopAutodockRequestMessage args)
{
_sawmill.Debug($"Received stop autodock request for {ToPrettyString(args.Entity)}");
var player = args.Session.AttachedEntity;
// TODO: Validation
if (player == null || !TryComp<AutoDockComponent>(args.Entity, out var comp)) return;
comp.Requesters.Remove(player.Value);
if (comp.Requesters.Count == 0)
RemComp<AutoDockComponent>(args.Entity);
}
}

View File

@@ -0,0 +1,450 @@
using Content.Server.Doors.Systems;
using Content.Server.Power.Components;
using Content.Server.Shuttles.Components;
using Content.Server.Shuttles.Events;
using Content.Shared.Doors;
using Content.Shared.Doors.Components;
using Content.Shared.Shuttles.Events;
using Content.Shared.Verbs;
using Robust.Server.Player;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems
{
public sealed partial class DockingSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly FixtureSystem _fixtureSystem = default!;
[Dependency] private readonly SharedJointSystem _jointSystem = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly ShuttleConsoleSystem _console = default!;
[Dependency] private readonly DoorSystem _doorSystem = default!;
private ISawmill _sawmill = default!;
private const string DockingFixture = "docking";
private const string DockingJoint = "docking";
private const float DockingRadius = 0.20f;
public override void Initialize()
{
base.Initialize();
_sawmill = Logger.GetSawmill("docking");
SubscribeLocalEvent<DockingComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<DockingComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<DockingComponent, PowerChangedEvent>(OnPowerChange);
SubscribeLocalEvent<DockingComponent, AnchorStateChangedEvent>(OnAnchorChange);
SubscribeLocalEvent<DockingComponent, ReAnchorEvent>(OnDockingReAnchor);
SubscribeLocalEvent<DockingComponent, BeforeDoorAutoCloseEvent>(OnAutoClose);
// Yes this isn't in shuttle console; it may be used by other systems technically.
// in which case I would also add their subs here.
SubscribeLocalEvent<ShuttleConsoleComponent, AutodockRequestMessage>(OnRequestAutodock);
SubscribeLocalEvent<ShuttleConsoleComponent, StopAutodockRequestMessage>(OnRequestStopAutodock);
SubscribeLocalEvent<ShuttleConsoleComponent, UndockRequestMessage>(OnRequestUndock);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
UpdateAutodock();
}
private void OnAutoClose(EntityUid uid, DockingComponent component, BeforeDoorAutoCloseEvent args)
{
// We'll just pin the door open when docked.
if (component.Docked)
args.Cancel();
}
private DockingComponent? GetDockable(PhysicsComponent body, TransformComponent dockingXform)
{
// Did you know Saltern is the most dockable station?
// Assume the docking port itself (and its body) is valid
if (!_mapManager.TryGetGrid(dockingXform.GridEntityId, out var grid) ||
!HasComp<ShuttleComponent>(grid.GridEntityId)) return null;
var transform = body.GetTransform();
var dockingFixture = _fixtureSystem.GetFixtureOrNull(body, DockingFixture);
// Happens if no power or whatever
if (dockingFixture == null)
return null;
Box2? aabb = null;
for (var i = 0; i < dockingFixture.Shape.ChildCount; i++)
{
aabb = aabb?.Union(dockingFixture.Shape.ComputeAABB(transform, i)) ?? dockingFixture.Shape.ComputeAABB(transform, i);
}
if (aabb == null) return null;
var enlargedAABB = aabb.Value.Enlarged(DockingRadius * 1.5f);
// Get any docking ports in range on other grids.
_mapManager.FindGridsIntersectingEnumerator(dockingXform.MapID, enlargedAABB, out var enumerator);
while (enumerator.MoveNext(out var otherGrid))
{
if (otherGrid.GridEntityId == dockingXform.GridEntityId) continue;
foreach (var ent in otherGrid.GetAnchoredEntities(enlargedAABB))
{
if (!TryComp(ent, out DockingComponent? otherDocking) ||
!otherDocking.Enabled ||
!TryComp(ent, out PhysicsComponent? otherBody)) continue;
var otherTransform = otherBody.GetTransform();
var otherDockingFixture = _fixtureSystem.GetFixtureOrNull(otherBody, DockingFixture);
if (otherDockingFixture == null)
{
DebugTools.Assert(false);
_sawmill.Error($"Found null docking fixture on {ent}");
continue;
}
for (var i = 0; i < otherDockingFixture.Shape.ChildCount; i++)
{
var otherAABB = otherDockingFixture.Shape.ComputeAABB(otherTransform, i);
if (!aabb.Value.Intersects(otherAABB)) continue;
// TODO: Need CollisionManager's GJK for accurate bounds
// Realistically I want 2 fixtures anyway but I'll deal with that later.
return otherDocking;
}
}
}
return null;
}
private void OnShutdown(EntityUid uid, DockingComponent component, ComponentShutdown args)
{
if (component.DockedWith == null ||
EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage > EntityLifeStage.MapInitialized) return;
Cleanup(component);
}
private void Cleanup(DockingComponent dockA)
{
_jointSystem.RemoveJoint(dockA.DockJoint!);
var dockBUid = dockA.DockedWith;
if (dockBUid == null ||
dockA.DockJoint == null ||
!TryComp(dockBUid, out DockingComponent? dockB))
{
DebugTools.Assert(false);
_sawmill.Error($"Tried to cleanup {dockA.Owner} but not docked?");
dockA.DockedWith = null;
if (dockA.DockJoint != null)
{
// We'll still cleanup the dock joint on release at least
_jointSystem.RemoveJoint(dockA.DockJoint);
}
return;
}
dockB.DockedWith = null;
dockB.DockJoint = null;
dockA.DockJoint = null;
dockA.DockedWith = null;
// If these grids are ever invalid then need to look at fixing ordering for unanchored events elsewhere.
var gridAUid = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(dockA.Owner).GridEntityId).GridEntityId;
var gridBUid = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(dockB.Owner).GridEntityId).GridEntityId;
var msg = new UndockEvent
{
DockA = dockA,
DockB = dockB,
GridAUid = gridAUid,
GridBUid = gridBUid,
};
EntityManager.EventBus.RaiseLocalEvent(dockA.Owner, msg, false);
EntityManager.EventBus.RaiseLocalEvent(dockB.Owner, msg, false);
EntityManager.EventBus.RaiseEvent(EventSource.Local, msg);
}
private void OnStartup(EntityUid uid, DockingComponent component, ComponentStartup args)
{
// Use startup so transform already initialized
if (!EntityManager.GetComponent<TransformComponent>(uid).Anchored) return;
EnableDocking(uid, component);
// This little gem is for docking deserialization
if (component.DockedWith != null)
{
// They're still initialising so we'll just wait for both to be ready.
if (MetaData(component.DockedWith.Value).EntityLifeStage < EntityLifeStage.Initialized) return;
var otherDock = EntityManager.GetComponent<DockingComponent>(component.DockedWith.Value);
DebugTools.Assert(otherDock.DockedWith != null);
Dock(component, otherDock);
DebugTools.Assert(component.Docked && otherDock.Docked);
}
}
private void OnAnchorChange(EntityUid uid, DockingComponent component, ref AnchorStateChangedEvent args)
{
if (args.Anchored)
{
EnableDocking(uid, component);
}
else
{
DisableDocking(uid, component);
}
_console.RefreshShuttleConsoles();
}
private void OnDockingReAnchor(EntityUid uid, DockingComponent component, ref ReAnchorEvent args)
{
if (!component.Docked) return;
var other = Comp<DockingComponent>(component.DockedWith!.Value);
Undock(component);
Dock(component, other);
_console.RefreshShuttleConsoles();
}
private void OnPowerChange(EntityUid uid, DockingComponent component, PowerChangedEvent args)
{
var lifestage = MetaData(uid).EntityLifeStage;
// This is because power can change during startup for <Reasons> and undock
if (lifestage is < EntityLifeStage.MapInitialized or >= EntityLifeStage.Terminating) return;
if (args.Powered)
{
EnableDocking(uid, component);
}
else
{
DisableDocking(uid, component);
}
}
private void DisableDocking(EntityUid uid, DockingComponent component)
{
if (!component.Enabled) return;
component.Enabled = false;
if (component.DockedWith != null)
{
Undock(component);
}
if (!TryComp(uid, out PhysicsComponent? physicsComponent))
{
return;
}
_fixtureSystem.DestroyFixture(physicsComponent, DockingFixture);
}
private void EnableDocking(EntityUid uid, DockingComponent component)
{
if (component.Enabled)
return;
if (!TryComp(uid, out PhysicsComponent? physicsComponent))
return;
component.Enabled = true;
// TODO: WTF IS THIS GARBAGE
var shape = new PhysShapeCircle
{
// Want half of the unit vector
Position = new Vector2(0f, -0.5f),
Radius = DockingRadius
};
// Listen it makes intersection tests easier; you can probably dump this but it requires a bunch more boilerplate
var fixture = new Fixture(physicsComponent, shape)
{
ID = DockingFixture,
Hard = false,
};
// TODO: I want this to ideally be 2 fixtures to force them to have some level of alignment buuuttt
// I also need collisionmanager for that yet again so they get dis.
_fixtureSystem.TryCreateFixture(physicsComponent, fixture);
}
/// <summary>
/// Docks 2 ports together and assumes it is valid.
/// </summary>
public void Dock(DockingComponent dockA, DockingComponent dockB)
{
_sawmill.Debug($"Docking between {dockA.Owner} and {dockB.Owner}");
// https://gamedev.stackexchange.com/questions/98772/b2distancejoint-with-frequency-equal-to-0-vs-b2weldjoint
// We could also potentially use a prismatic joint? Depending if we want clamps that can extend or whatever
var dockAXform = EntityManager.GetComponent<TransformComponent>(dockA.Owner);
var dockBXform = EntityManager.GetComponent<TransformComponent>(dockB.Owner);
var gridA = _mapManager.GetGrid(dockAXform.GridEntityId).GridEntityId;
var gridB = _mapManager.GetGrid(dockBXform.GridEntityId).GridEntityId;
SharedJointSystem.LinearStiffness(
2f,
0.7f,
EntityManager.GetComponent<PhysicsComponent>(gridA).Mass,
EntityManager.GetComponent<PhysicsComponent>(gridB).Mass,
out var stiffness,
out var damping);
// These need playing around with
// Could also potentially have collideconnected false and stiffness 0 but it was a bit more suss???
var joint = _jointSystem.GetOrCreateWeldJoint(gridA, gridB, DockingJoint + dockA.Owner);
var gridAXform = EntityManager.GetComponent<TransformComponent>(gridA);
var gridBXform = EntityManager.GetComponent<TransformComponent>(gridB);
var anchorA = dockAXform.LocalPosition + dockAXform.LocalRotation.ToWorldVec() / 2f;
var anchorB = dockBXform.LocalPosition + dockBXform.LocalRotation.ToWorldVec() / 2f;
joint.LocalAnchorA = anchorA;
joint.LocalAnchorB = anchorB;
joint.ReferenceAngle = (float) (gridBXform.WorldRotation - gridAXform.WorldRotation);
joint.CollideConnected = true;
joint.Stiffness = stiffness;
joint.Damping = damping;
dockA.DockedWith = dockB.Owner;
dockB.DockedWith = dockA.Owner;
dockA.DockJoint = joint;
dockB.DockJoint = joint;
if (TryComp(dockA.Owner, out DoorComponent? doorA))
{
doorA.ChangeAirtight = false;
_doorSystem.StartOpening(doorA.Owner, doorA);
}
if (TryComp(dockB.Owner, out DoorComponent? doorB))
{
doorB.ChangeAirtight = false;
_doorSystem.StartOpening(doorB.Owner, doorB);
}
var msg = new DockEvent
{
DockA = dockA,
DockB = dockB,
GridAUid = gridA,
GridBUid = gridB,
};
EntityManager.EventBus.RaiseLocalEvent(dockA.Owner, msg, false);
EntityManager.EventBus.RaiseLocalEvent(dockB.Owner, msg, false);
EntityManager.EventBus.RaiseEvent(EventSource.Local, msg);
}
private bool CanDock(DockingComponent dockA, DockingComponent dockB)
{
if (!TryComp(dockA.Owner, out PhysicsComponent? bodyA) ||
!TryComp(dockB.Owner, out PhysicsComponent? bodyB) ||
!dockA.Enabled ||
!dockB.Enabled ||
dockA.DockedWith != null ||
dockB.DockedWith != null)
{
return false;
}
var fixtureA = _fixtureSystem.GetFixtureOrNull(bodyA, DockingFixture);
var fixtureB = _fixtureSystem.GetFixtureOrNull(bodyB, DockingFixture);
if (fixtureA == null || fixtureB == null)
{
return false;
}
var transformA = bodyA.GetTransform();
var transformB = bodyB.GetTransform();
var intersect = false;
for (var i = 0; i < fixtureA.Shape.ChildCount; i++)
{
var aabb = fixtureA.Shape.ComputeAABB(transformA, i);
for (var j = 0; j < fixtureB.Shape.ChildCount; j++)
{
var otherAABB = fixtureB.Shape.ComputeAABB(transformB, j);
if (!aabb.Intersects(otherAABB)) continue;
// TODO: Need collisionmanager's GJK for accurate checks don't @ me son
intersect = true;
break;
}
if (intersect) break;
}
return intersect;
}
/// <summary>
/// Attempts to dock 2 ports together and will return early if it's not possible.
/// </summary>
private void TryDock(DockingComponent dockA, DockingComponent dockB)
{
if (!CanDock(dockA, dockB)) return;
Dock(dockA, dockB);
}
private void Undock(DockingComponent dock)
{
if (dock.DockedWith == null)
{
DebugTools.Assert(false);
_sawmill.Error($"Tried to undock {(dock).Owner} but not docked with anything?");
return;
}
if (TryComp(dock.Owner, out DoorComponent? doorA))
{
doorA.ChangeAirtight = true;
_doorSystem.TryClose(doorA.Owner, doorA);
}
if (TryComp(dock.DockedWith, out DoorComponent? doorB))
{
doorB.ChangeAirtight = true;
_doorSystem.TryClose(doorB.Owner, doorB);
}
var recentlyDocked = EnsureComp<RecentlyDockedComponent>(dock.Owner);
recentlyDocked.LastDocked = dock.DockedWith.Value;
recentlyDocked = EnsureComp<RecentlyDockedComponent>(dock.DockedWith.Value);
recentlyDocked.LastDocked = dock.DockedWith.Value;
Cleanup(dock);
}
}
}

View File

@@ -0,0 +1,29 @@
using Content.Shared.Shuttles.BUIStates;
using Content.Shared.Shuttles.Components;
using Content.Shared.Shuttles.Systems;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
namespace Content.Server.Shuttles.Systems;
public sealed class RadarConsoleSystem : SharedRadarConsoleSystem
{
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RadarConsoleComponent, ComponentStartup>(OnRadarStartup);
}
private void OnRadarStartup(EntityUid uid, RadarConsoleComponent component, ComponentStartup args)
{
UpdateState(component);
}
protected override void UpdateState(RadarConsoleComponent component)
{
var radarState = new RadarConsoleBoundInterfaceState(component.MaxRange, component.Owner, new List<DockingInterfaceState>());
_uiSystem.GetUiOrNull(component.Owner, RadarConsoleUiKey.Key)?.SetState(radarState);
}
}

View File

@@ -0,0 +1,337 @@
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Shuttles.Components;
using Content.Server.Shuttles.Events;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Popups;
using Content.Shared.Shuttles.BUIStates;
using Content.Shared.Shuttles.Components;
using Content.Shared.Shuttles.Events;
using Content.Shared.Shuttles.Systems;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems
{
public sealed class ShuttleConsoleSystem : SharedShuttleConsoleSystem
{
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly TagSystem _tags = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShuttleConsoleComponent, ComponentShutdown>(OnConsoleShutdown);
SubscribeLocalEvent<ShuttleConsoleComponent, PowerChangedEvent>(OnConsolePowerChange);
SubscribeLocalEvent<ShuttleConsoleComponent, AnchorStateChangedEvent>(OnConsoleAnchorChange);
SubscribeLocalEvent<ShuttleConsoleComponent, ActivatableUIOpenAttemptEvent>(OnConsoleUIOpenAttempt);
SubscribeLocalEvent<ShuttleConsoleComponent, ShuttleModeRequestMessage>(OnModeRequest);
SubscribeLocalEvent<ShuttleConsoleComponent, BoundUIClosedEvent>(OnConsoleUIClose);
SubscribeLocalEvent<DockEvent>(OnDock);
SubscribeLocalEvent<UndockEvent>(OnUndock);
SubscribeLocalEvent<PilotComponent, MoveEvent>(HandlePilotMove);
SubscribeLocalEvent<PilotComponent, ComponentGetState>(OnGetState);
}
private void OnDock(DockEvent ev)
{
RefreshShuttleConsoles();
}
private void OnUndock(UndockEvent ev)
{
RefreshShuttleConsoles();
}
/// <summary>
/// Refreshes all of the data for shuttle consoles.
/// </summary>
public void RefreshShuttleConsoles()
{
// TODO: Should really call this per shuttle in some instances.
var docks = GetAllDocks();
foreach (var comp in EntityQuery<ShuttleConsoleComponent>(true))
{
UpdateState(comp, docks);
}
}
/// <summary>
/// Stop piloting if the window is closed.
/// </summary>
private void OnConsoleUIClose(EntityUid uid, ShuttleConsoleComponent component, BoundUIClosedEvent args)
{
if ((ShuttleConsoleUiKey) args.UiKey != ShuttleConsoleUiKey.Key ||
args.Session.AttachedEntity is not {} user) return;
// In case they D/C should still clean them up.
foreach (var comp in EntityQuery<AutoDockComponent>(true))
{
comp.Requesters.Remove(user);
}
RemovePilot(user);
}
private void OnConsoleUIOpenAttempt(EntityUid uid, ShuttleConsoleComponent component, ActivatableUIOpenAttemptEvent args)
{
if (!TryPilot(args.User, uid))
args.Cancel();
}
private void OnConsoleAnchorChange(EntityUid uid, ShuttleConsoleComponent component, ref AnchorStateChangedEvent args)
{
UpdateState(component);
}
private void OnConsolePowerChange(EntityUid uid, ShuttleConsoleComponent component, PowerChangedEvent args)
{
UpdateState(component);
}
private bool TryPilot(EntityUid user, EntityUid uid)
{
if (!_tags.HasTag(user, "CanPilot") ||
!TryComp<ShuttleConsoleComponent>(uid, out var component) ||
!this.IsPowered(uid, EntityManager) ||
!Transform(uid).Anchored ||
!_blocker.CanInteract(user, uid))
{
return false;
}
var pilotComponent = EntityManager.EnsureComponent<PilotComponent>(user);
var console = pilotComponent.Console;
if (console != null)
{
RemovePilot(pilotComponent);
if (console == component)
{
return false;
}
}
AddPilot(user, component);
return true;
}
private void OnGetState(EntityUid uid, PilotComponent component, ref ComponentGetState args)
{
args.State = new PilotComponentState(component.Console?.Owner);
}
/// <summary>
/// Client is requesting a change in the shuttle's driving mode.
/// </summary>
private void OnModeRequest(EntityUid uid, ShuttleConsoleComponent component, ShuttleModeRequestMessage args)
{
if (args.Session.AttachedEntity is not { } player ||
!TryComp<PilotComponent>(player, out var pilot) ||
!TryComp<TransformComponent>(player, out var xform) ||
pilot.Console is not ShuttleConsoleComponent console) return;
if (!console.SubscribedPilots.Contains(pilot) ||
!TryComp<ShuttleComponent>(xform.GridEntityId, out var shuttle)) return;
SetShuttleMode(args.Mode, console, shuttle);
}
/// <summary>
/// Sets the shuttle's movement mode. Does minimal revalidation.
/// </summary>
private void SetShuttleMode(ShuttleMode mode, ShuttleConsoleComponent consoleComponent,
ShuttleComponent shuttleComponent, TransformComponent? consoleXform = null)
{
// Re-validate
if (!this.IsPowered(consoleComponent.Owner, EntityManager) ||
!Resolve(consoleComponent.Owner, ref consoleXform) ||
!consoleXform.Anchored ||
consoleXform.GridID != Transform(shuttleComponent.Owner).GridID)
{
UpdateState(consoleComponent);
return;
}
shuttleComponent.Mode = mode;
switch (mode)
{
case ShuttleMode.Strafing:
break;
case ShuttleMode.Cruise:
break;
default:
throw new ArgumentOutOfRangeException();
}
UpdateState(consoleComponent);
}
/// <summary>
/// Returns the position and angle of all dockingcomponents.
/// </summary>
private List<DockingInterfaceState> GetAllDocks()
{
// TODO: NEED TO MAKE SURE THIS UPDATES ON ANCHORING CHANGES!
var result = new List<DockingInterfaceState>();
foreach (var (comp, xform) in EntityQuery<DockingComponent, TransformComponent>(true))
{
if (xform.ParentUid != xform.GridUid) continue;
var state = new DockingInterfaceState()
{
Coordinates = xform.Coordinates,
Angle = xform.LocalRotation,
Entity = comp.Owner,
Connected = comp.Docked,
};
result.Add(state);
}
return result;
}
private void UpdateState(ShuttleConsoleComponent component, List<DockingInterfaceState>? docks = null)
{
TryComp<RadarConsoleComponent>(component.Owner, out var radar);
var range = radar?.MaxRange ?? 0f;
TryComp<ShuttleComponent>(Transform(component.Owner).GridUid, out var shuttle);
var mode = shuttle?.Mode ?? ShuttleMode.Cruise;
docks ??= GetAllDocks();
_ui.GetUiOrNull(component.Owner, ShuttleConsoleUiKey.Key)
?.SetState(new ShuttleConsoleBoundInterfaceState(
mode,
range,
component.Owner,
docks));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var toRemove = new RemQueue<PilotComponent>();
foreach (var comp in EntityManager.EntityQuery<PilotComponent>())
{
if (comp.Console == null) continue;
if (!_blocker.CanInteract(comp.Owner, comp.Console.Owner))
{
toRemove.Add(comp);
}
}
foreach (var comp in toRemove)
{
RemovePilot(comp);
}
}
/// <summary>
/// If pilot is moved then we'll stop them from piloting.
/// </summary>
private void HandlePilotMove(EntityUid uid, PilotComponent component, ref MoveEvent args)
{
if (component.Console == null || component.Position == null)
{
DebugTools.Assert(component.Position == null && component.Console == null);
EntityManager.RemoveComponent<PilotComponent>(uid);
return;
}
if (args.NewPosition.TryDistance(EntityManager, component.Position.Value, out var distance) &&
distance < PilotComponent.BreakDistance) return;
RemovePilot(component);
}
protected override void HandlePilotShutdown(EntityUid uid, PilotComponent component, ComponentShutdown args)
{
base.HandlePilotShutdown(uid, component, args);
RemovePilot(component);
}
private void OnConsoleShutdown(EntityUid uid, ShuttleConsoleComponent component, ComponentShutdown args)
{
ClearPilots(component);
}
public void AddPilot(EntityUid entity, ShuttleConsoleComponent component)
{
if (!EntityManager.TryGetComponent(entity, out PilotComponent? pilotComponent) ||
component.SubscribedPilots.Contains(pilotComponent))
{
return;
}
if (TryComp<SharedEyeComponent>(entity, out var eye))
{
eye.Zoom = component.Zoom;
}
component.SubscribedPilots.Add(pilotComponent);
_alertsSystem.ShowAlert(entity, AlertType.PilotingShuttle);
pilotComponent.Console = component;
ActionBlockerSystem.UpdateCanMove(entity);
pilotComponent.Position = EntityManager.GetComponent<TransformComponent>(entity).Coordinates;
Dirty(pilotComponent);
}
public void RemovePilot(PilotComponent pilotComponent)
{
var console = pilotComponent.Console;
if (console is not ShuttleConsoleComponent helmsman) return;
pilotComponent.Console = null;
pilotComponent.Position = null;
if (TryComp<SharedEyeComponent>(pilotComponent.Owner, out var eye))
{
eye.Zoom = new(1.0f, 1.0f);
}
if (!helmsman.SubscribedPilots.Remove(pilotComponent)) return;
_alertsSystem.ClearAlert(pilotComponent.Owner, AlertType.PilotingShuttle);
pilotComponent.Owner.PopupMessage(Loc.GetString("shuttle-pilot-end"));
if (pilotComponent.LifeStage < ComponentLifeStage.Stopping)
EntityManager.RemoveComponent<PilotComponent>(pilotComponent.Owner);
}
public void RemovePilot(EntityUid entity)
{
if (!EntityManager.TryGetComponent(entity, out PilotComponent? pilotComponent)) return;
RemovePilot(pilotComponent);
}
public void ClearPilots(ShuttleConsoleComponent component)
{
while (component.SubscribedPilots.TryGetValue(0, out var pilot))
{
RemovePilot(pilot);
}
}
}
}

View File

@@ -0,0 +1,166 @@
using Content.Server.Shuttles.Components;
using Content.Shared.CCVar;
using JetBrains.Annotations;
using Robust.Shared.Configuration;
using Robust.Shared.Physics;
namespace Content.Server.Shuttles.Systems
{
[UsedImplicitly]
public sealed class ShuttleSystem : EntitySystem
{
[Dependency] private readonly FixtureSystem _fixtures = default!;
public const float TileMassMultiplier = 0.5f;
public float ShuttleMaxLinearSpeed;
public float ShuttleMaxAngularMomentum;
public float ShuttleMaxAngularAcc;
public float ShuttleMaxAngularSpeed;
public float ShuttleIdleLinearDamping;
public float ShuttleIdleAngularDamping;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShuttleComponent, ComponentAdd>(OnShuttleAdd);
SubscribeLocalEvent<ShuttleComponent, ComponentStartup>(OnShuttleStartup);
SubscribeLocalEvent<ShuttleComponent, ComponentShutdown>(OnShuttleShutdown);
SubscribeLocalEvent<GridInitializeEvent>(OnGridInit);
SubscribeLocalEvent<GridFixtureChangeEvent>(OnGridFixtureChange);
var configManager = IoCManager.Resolve<IConfigurationManager>();
configManager.OnValueChanged(CCVars.ShuttleMaxLinearSpeed, SetShuttleMaxLinearSpeed, true);
configManager.OnValueChanged(CCVars.ShuttleMaxAngularSpeed, SetShuttleMaxAngularSpeed, true);
configManager.OnValueChanged(CCVars.ShuttleIdleLinearDamping, SetShuttleIdleLinearDamping, true);
configManager.OnValueChanged(CCVars.ShuttleIdleAngularDamping, SetShuttleIdleAngularDamping, true);
configManager.OnValueChanged(CCVars.ShuttleMaxAngularAcc, SetShuttleMaxAngularAcc, true);
configManager.OnValueChanged(CCVars.ShuttleMaxAngularMomentum, SetShuttleMaxAngularMomentum, true);
}
private void SetShuttleMaxLinearSpeed(float value) => ShuttleMaxLinearSpeed = value;
private void SetShuttleMaxAngularSpeed(float value) => ShuttleMaxAngularSpeed = value;
private void SetShuttleMaxAngularAcc(float value) => ShuttleMaxAngularAcc = value;
private void SetShuttleMaxAngularMomentum(float value) => ShuttleMaxAngularMomentum = value;
private void SetShuttleIdleLinearDamping(float value) => ShuttleIdleLinearDamping = value;
private void SetShuttleIdleAngularDamping(float value) => ShuttleIdleAngularDamping = value;
public override void Shutdown()
{
base.Shutdown();
var configManager = IoCManager.Resolve<IConfigurationManager>();
configManager.UnsubValueChanged(CCVars.ShuttleMaxLinearSpeed, SetShuttleMaxLinearSpeed);
configManager.UnsubValueChanged(CCVars.ShuttleMaxAngularSpeed, SetShuttleMaxAngularSpeed);
configManager.UnsubValueChanged(CCVars.ShuttleIdleLinearDamping, SetShuttleIdleLinearDamping);
configManager.UnsubValueChanged(CCVars.ShuttleIdleAngularDamping, SetShuttleIdleAngularDamping);
configManager.UnsubValueChanged(CCVars.ShuttleMaxAngularMomentum, SetShuttleMaxAngularMomentum);
}
private void OnShuttleAdd(EntityUid uid, ShuttleComponent component, ComponentAdd args)
{
// Easier than doing it in the comp and they don't have constructors.
for (var i = 0; i < component.LinearThrusters.Length; i++)
{
component.LinearThrusters[i] = new List<ThrusterComponent>();
}
}
private void OnGridFixtureChange(GridFixtureChangeEvent args)
{
// Look this is jank but it's a placeholder until we design it.
if (args.NewFixtures.Count == 0) return;
var manager = Comp<FixturesComponent>(args.NewFixtures[0].Body.Owner);
foreach (var fixture in args.NewFixtures)
{
_fixtures.SetMass(fixture, fixture.Area * TileMassMultiplier, manager, false);
_fixtures.SetRestitution(fixture, 0.1f, manager, false);
}
_fixtures.FixtureUpdate(manager, args.NewFixtures[0].Body);
}
private void OnGridInit(GridInitializeEvent ev)
{
EntityManager.EnsureComponent<ShuttleComponent>(ev.EntityUid);
}
private void OnShuttleStartup(EntityUid uid, ShuttleComponent component, ComponentStartup args)
{
if (!EntityManager.HasComponent<IMapGridComponent>(component.Owner))
{
return;
}
if (!EntityManager.TryGetComponent(component.Owner, out PhysicsComponent? physicsComponent))
{
return;
}
if (component.Enabled)
{
Enable(physicsComponent);
}
}
public void Toggle(ShuttleComponent component)
{
if (!EntityManager.TryGetComponent(component.Owner, out PhysicsComponent? physicsComponent)) return;
component.Enabled = !component.Enabled;
if (component.Enabled)
{
Enable(physicsComponent);
}
else
{
Disable(physicsComponent);
}
}
private void Enable(PhysicsComponent component)
{
component.BodyType = BodyType.Dynamic;
component.BodyStatus = BodyStatus.InAir;
//component.FixedRotation = false; TODO WHEN ROTATING SHUTTLES FIXED.
component.FixedRotation = false;
component.LinearDamping = ShuttleIdleLinearDamping;
component.AngularDamping = ShuttleIdleAngularDamping;
}
private void Disable(PhysicsComponent component)
{
component.BodyType = BodyType.Static;
component.BodyStatus = BodyStatus.OnGround;
component.FixedRotation = true;
}
private void OnShuttleShutdown(EntityUid uid, ShuttleComponent component, ComponentShutdown args)
{
// None of the below is necessary for any cleanup if we're just deleting.
if (EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage >= EntityLifeStage.Terminating) return;
if (!EntityManager.TryGetComponent(component.Owner, out PhysicsComponent? physicsComponent))
{
return;
}
Disable(physicsComponent);
if (!EntityManager.TryGetComponent(component.Owner, out FixturesComponent? fixturesComponent))
{
return;
}
foreach (var fixture in fixturesComponent.Fixtures.Values)
{
fixture.Mass = 0f;
}
}
}
}

View File

@@ -0,0 +1,28 @@
using Content.Server.Shuttles.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;
namespace Content.Server.Shuttles.Systems;
/// <summary>
/// Deletes anything with <see cref="SpaceGarbageComponent"/> that has a cross-grid collision with a static body.
/// </summary>
public sealed class SpaceGarbageSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpaceGarbageComponent, StartCollideEvent>(OnCollide);
}
private void OnCollide(EntityUid uid, SpaceGarbageComponent component, StartCollideEvent args)
{
var ourXform = Transform(args.OurFixture.Body.Owner);
var otherXform = Transform(args.OtherFixture.Body.Owner);
if (ourXform.GridEntityId == otherXform.GridEntityId ||
args.OtherFixture.Body.BodyType != BodyType.Static) return;
QueueDel(uid);
}
}

View File

@@ -0,0 +1,524 @@
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Audio;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Shuttles.Components;
using Content.Shared.Damage;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Physics;
using Content.Shared.Shuttles.Components;
using Content.Shared.Temperature;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems
{
public sealed class ThrusterSystem : EntitySystem
{
[Robust.Shared.IoC.Dependency] private readonly IMapManager _mapManager = default!;
[Robust.Shared.IoC.Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Robust.Shared.IoC.Dependency] private readonly AmbientSoundSystem _ambient = default!;
[Robust.Shared.IoC.Dependency] private readonly FixtureSystem _fixtureSystem = default!;
[Robust.Shared.IoC.Dependency] private readonly DamageableSystem _damageable = default!;
// Essentially whenever thruster enables we update the shuttle's available impulses which are used for movement.
// This is done for each direction available.
public const string BurnFixture = "thruster-burn";
private readonly HashSet<ThrusterComponent> _activeThrusters = new();
// Used for accumulating burn if someone touches a firing thruster.
private float _accumulator;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ThrusterComponent, ActivateInWorldEvent>(OnActivateThruster);
SubscribeLocalEvent<ThrusterComponent, ComponentInit>(OnThrusterInit);
SubscribeLocalEvent<ThrusterComponent, ComponentShutdown>(OnThrusterShutdown);
SubscribeLocalEvent<ThrusterComponent, PowerChangedEvent>(OnPowerChange);
SubscribeLocalEvent<ThrusterComponent, AnchorStateChangedEvent>(OnAnchorChange);
SubscribeLocalEvent<ThrusterComponent, ReAnchorEvent>(OnThrusterReAnchor);
SubscribeLocalEvent<ThrusterComponent, RotateEvent>(OnRotate);
SubscribeLocalEvent<ThrusterComponent, IsHotEvent>(OnIsHotEvent);
SubscribeLocalEvent<ThrusterComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<ThrusterComponent, EndCollideEvent>(OnEndCollide);
SubscribeLocalEvent<ThrusterComponent, ExaminedEvent>(OnThrusterExamine);
_mapManager.TileChanged += OnTileChange;
}
private void OnThrusterExamine(EntityUid uid, ThrusterComponent component, ExaminedEvent args)
{
// Powered is already handled by other power components
var enabled = Loc.GetString("thruster-comp-enabled",
("enabledColor", component.Enabled ? "green": "red"),
("enabled", component.Enabled ? "on": "off"));
args.PushMarkup(enabled);
if (component.Type == ThrusterType.Linear &&
EntityManager.TryGetComponent(uid, out TransformComponent? xform) &&
xform.Anchored)
{
var nozzleDir = Loc.GetString("thruster-comp-nozzle-direction",
("direction", xform.LocalRotation.Opposite().ToWorldVec().GetDir().ToString().ToLowerInvariant()));
args.PushMarkup(nozzleDir);
var exposed = NozzleExposed(xform);
var nozzleText = Loc.GetString("thruster-comp-nozzle-exposed",
("exposedColor", exposed ? "green" : "red"),
("exposed", exposed ? "is": "is not"));
args.PushMarkup(nozzleText);
}
}
public override void Shutdown()
{
base.Shutdown();
_mapManager.TileChanged -= OnTileChange;
}
private void OnIsHotEvent(EntityUid uid, ThrusterComponent component, IsHotEvent args)
{
args.IsHot = component.Type != ThrusterType.Angular && component.IsOn;
}
private void OnTileChange(object? sender, TileChangedEventArgs e)
{
// If the old tile was space but the new one isn't then disable all adjacent thrusters
if (e.NewTile.IsSpace(_tileDefManager) || !e.OldTile.IsSpace(_tileDefManager)) return;
var tilePos = e.NewTile.GridIndices;
var grid = _mapManager.GetGrid(e.NewTile.GridUid);
var xformQuery = GetEntityQuery<TransformComponent>();
var thrusterQuery = GetEntityQuery<ThrusterComponent>();
for (var x = -1; x <= 1; x++)
{
for (var y = -1; y <= 1; y++)
{
if (x != 0 && y != 0) continue;
var checkPos = tilePos + new Vector2i(x, y);
var enumerator = grid.GetAnchoredEntitiesEnumerator(checkPos);
while (enumerator.MoveNext(out var ent))
{
if (!thrusterQuery.TryGetComponent(ent.Value, out var thruster) || !thruster.RequireSpace) continue;
// Work out if the thruster is facing this direction
var xform = xformQuery.GetComponent(ent.Value);
var direction = xform.LocalRotation.ToWorldVec();
if (new Vector2i((int) direction.X, (int) direction.Y) != new Vector2i(x, y)) continue;
DisableThruster(ent.Value, thruster, xform.GridEntityId);
}
}
}
}
private void OnActivateThruster(EntityUid uid, ThrusterComponent component, ActivateInWorldEvent args)
{
component.Enabled ^= true;
}
/// <summary>
/// If the thruster rotates change the direction where the linear thrust is applied
/// </summary>
private void OnRotate(EntityUid uid, ThrusterComponent component, ref RotateEvent args)
{
// TODO: Disable visualizer for old direction
if (!component.Enabled ||
component.Type != ThrusterType.Linear ||
!EntityManager.TryGetComponent(uid, out TransformComponent? xform) ||
!_mapManager.TryGetGrid(xform.GridEntityId, out var grid) ||
!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent))
{
return;
}
var canEnable = CanEnable(uid, component);
// If it's not on then don't enable it inadvertantly (given we don't have an old rotation)
if (!canEnable && !component.IsOn) return;
// Enable it if it was turned off but new tile is valid
if (!component.IsOn && canEnable)
{
EnableThruster(uid, component);
return;
}
// Disable if new tile invalid
if (component.IsOn && !canEnable)
{
DisableThruster(uid, component, xform, args.OldRotation);
return;
}
var oldDirection = (int) args.OldRotation.GetCardinalDir() / 2;
var direction = (int) args.NewRotation.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[oldDirection] -= component.Thrust;
DebugTools.Assert(shuttleComponent.LinearThrusters[oldDirection].Contains(component));
shuttleComponent.LinearThrusters[oldDirection].Remove(component);
shuttleComponent.LinearThrust[direction] += component.Thrust;
DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(component));
shuttleComponent.LinearThrusters[direction].Add(component);
}
private void OnAnchorChange(EntityUid uid, ThrusterComponent component, ref AnchorStateChangedEvent args)
{
if (args.Anchored && CanEnable(uid, component))
{
EnableThruster(uid, component);
}
else
{
DisableThruster(uid, component);
}
}
private void OnThrusterReAnchor(EntityUid uid, ThrusterComponent component, ref ReAnchorEvent args)
{
DisableThruster(uid, component, args.OldGrid);
if (CanEnable(uid, component))
EnableThruster(uid, component);
}
private void OnThrusterInit(EntityUid uid, ThrusterComponent component, ComponentInit args)
{
_ambient.SetAmbience(uid, false);
if (!component.Enabled)
{
return;
}
if (CanEnable(uid, component))
{
EnableThruster(uid, component);
}
}
private void OnThrusterShutdown(EntityUid uid, ThrusterComponent component, ComponentShutdown args)
{
DisableThruster(uid, component);
}
private void OnPowerChange(EntityUid uid, ThrusterComponent component, PowerChangedEvent args)
{
if (args.Powered && CanEnable(uid, component))
{
EnableThruster(uid, component);
}
else
{
DisableThruster(uid, component);
}
}
/// <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)
{
if (component.IsOn ||
!Resolve(uid, ref xform) ||
!_mapManager.TryGetGrid(xform.GridEntityId, out var grid)) return;
component.IsOn = true;
if (!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent)) return;
// Logger.DebugS("thruster", $"Enabled thruster {uid}");
switch (component.Type)
{
case ThrusterType.Linear:
var direction = (int) xform.LocalRotation.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[direction] += component.Thrust;
DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(component));
shuttleComponent.LinearThrusters[direction].Add(component);
// Don't just add / remove the fixture whenever the thruster fires because perf
if (EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent) &&
component.BurnPoly.Count > 0)
{
var shape = new PolygonShape();
shape.SetVertices(component.BurnPoly);
var fixture = new Fixture(physicsComponent, shape)
{
ID = BurnFixture,
Hard = false,
CollisionLayer = (int) CollisionGroup.FullTileMask
};
_fixtureSystem.TryCreateFixture(physicsComponent, fixture);
}
break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust += component.Thrust;
DebugTools.Assert(!shuttleComponent.AngularThrusters.Contains(component));
shuttleComponent.AngularThrusters.Add(component);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(ThrusterVisualState.State, true);
}
if (EntityManager.TryGetComponent(uid, out PointLightComponent? pointLightComponent))
{
pointLightComponent.Enabled = true;
}
_ambient.SetAmbience(uid, true);
}
public void DisableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null, Angle? angle = null)
{
if (!Resolve(uid, ref xform)) return;
DisableThruster(uid, component, xform.GridEntityId, xform);
}
/// <summary>
/// Tries to disable the thruster.
/// </summary>
public void DisableThruster(EntityUid uid, ThrusterComponent component, EntityUid gridId, TransformComponent? xform = null, Angle? angle = null)
{
if (!component.IsOn ||
!Resolve(uid, ref xform) ||
!_mapManager.TryGetGrid(gridId, out var grid)) return;
component.IsOn = false;
if (!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent)) return;
// Logger.DebugS("thruster", $"Disabled thruster {uid}");
switch (component.Type)
{
case ThrusterType.Linear:
angle ??= xform.LocalRotation;
var direction = (int) angle.Value.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[direction] -= component.Thrust;
DebugTools.Assert(shuttleComponent.LinearThrusters[direction].Contains(component));
shuttleComponent.LinearThrusters[direction].Remove(component);
break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust -= component.Thrust;
DebugTools.Assert(shuttleComponent.AngularThrusters.Contains(component));
shuttleComponent.AngularThrusters.Remove(component);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(ThrusterVisualState.State, false);
}
if (EntityManager.TryGetComponent(uid, out PointLightComponent? pointLightComponent))
{
pointLightComponent.Enabled = false;
}
_ambient.SetAmbience(uid, false);
if (EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent))
{
_fixtureSystem.DestroyFixture(physicsComponent, BurnFixture);
}
_activeThrusters.Remove(component);
component.Colliding.Clear();
}
public bool CanEnable(EntityUid uid, ThrusterComponent component)
{
if (!component.Enabled) return false;
if (component.LifeStage > ComponentLifeStage.Running) return false;
var xform = Transform(uid);
if (!xform.Anchored ||!this.IsPowered(uid, EntityManager))
{
return false;
}
if (!component.RequireSpace)
return true;
return NozzleExposed(xform);
}
private bool NozzleExposed(TransformComponent xform)
{
var (x, y) = xform.LocalPosition + xform.LocalRotation.Opposite().ToWorldVec();
var tile = _mapManager.GetGrid(xform.GridEntityId).GetTileRef(new Vector2i((int) Math.Floor(x), (int) Math.Floor(y)));
return tile.Tile.IsSpace();
}
#region Burning
public override void Update(float frameTime)
{
base.Update(frameTime);
_accumulator += frameTime;
if (_accumulator < 1) return;
_accumulator -= 1;
foreach (var comp in _activeThrusters.ToArray())
{
MetaDataComponent? metaData = null;
if (!comp.Firing || comp.Damage == null || Paused(comp.Owner, metaData) || Deleted(comp.Owner, metaData)) continue;
DebugTools.Assert(comp.Colliding.Count > 0);
foreach (var uid in comp.Colliding.ToArray())
{
_damageable.TryChangeDamage(uid, comp.Damage);
}
}
}
private void OnStartCollide(EntityUid uid, ThrusterComponent component, StartCollideEvent args)
{
if (args.OurFixture.ID != BurnFixture) return;
_activeThrusters.Add(component);
component.Colliding.Add((args.OtherFixture.Body).Owner);
}
private void OnEndCollide(EntityUid uid, ThrusterComponent component, EndCollideEvent args)
{
if (args.OurFixture.ID != BurnFixture) return;
component.Colliding.Remove((args.OtherFixture.Body).Owner);
if (component.Colliding.Count == 0)
{
_activeThrusters.Remove(component);
}
}
/// <summary>
/// Considers a thrust direction as being active.
/// </summary>
public void EnableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) != 0x0) return;
component.ThrustDirections |= direction;
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent((comp).Owner, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
}
/// <summary>
/// Disables a thrust direction.
/// </summary>
public void DisableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) == 0x0) return;
component.ThrustDirections &= ~direction;
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent((comp).Owner, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
}
public void DisableLinearThrusters(ShuttleComponent component)
{
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
{
DisableLinearThrustDirection(component, dir);
}
DebugTools.Assert(component.ThrustDirections == DirectionFlag.None);
}
public void SetAngularThrust(ShuttleComponent component, bool on)
{
if (on)
{
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent((comp).Owner, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
}
else
{
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent((comp).Owner, out AppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
}
}
#endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetFlagIndex(DirectionFlag flag)
{
return (int) Math.Log2((int) flag);
}
}
}