Thrusters (shuttle go nyoom) (#5352)

This commit is contained in:
metalgearsloth
2021-11-21 17:09:49 +11:00
committed by GitHub
parent c21a9e32b1
commit bdead40a80
37 changed files with 1100 additions and 140 deletions

View File

@@ -0,0 +1,465 @@
using Content.Server.Doors.Components;
using Content.Server.Power.Components;
using Content.Server.Shuttles.Components;
using Content.Shared.Doors;
using Content.Shared.Verbs;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.EntitySystems
{
public sealed class DockingSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly SharedBroadphaseSystem _broadphaseSystem = default!;
[Dependency] private readonly SharedJointSystem _jointSystem = default!;
private const string DockingFixture = "docking";
private const string DockingJoint = "docking";
private const float DockingRadius = 0.20f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DockingComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<DockingComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<DockingComponent, PowerChangedEvent>(OnPowerChange);
SubscribeLocalEvent<DockingComponent, AnchorStateChangedEvent>(OnAnchorChange);
SubscribeLocalEvent<DockingComponent, GetInteractionVerbsEvent>(OnVerb);
SubscribeLocalEvent<DockingComponent, BeforeDoorAutoCloseEvent>(OnAutoClose);
}
private void OnAutoClose(EntityUid uid, DockingComponent component, BeforeDoorAutoCloseEvent args)
{
// We'll just pin the door open when docked.
if (component.Docked)
args.Cancel();
}
private void OnVerb(EntityUid uid, DockingComponent component, GetInteractionVerbsEvent args)
{
if (!args.CanInteract ||
!args.CanAccess) return;
Verb? verb;
// TODO: Have it open the UI and have the UI do this.
if (!component.Docked &&
EntityManager.TryGetComponent(uid, out PhysicsComponent? body) &&
EntityManager.TryGetComponent(uid, out TransformComponent? xform))
{
DockingComponent? otherDock = null;
if (component.Enabled)
otherDock = GetDockable(body, xform);
verb = new Verb
{
Disabled = otherDock == null,
Text = Loc.GetString("docking-component-dock"),
Act = () =>
{
if (otherDock == null) return;
TryDock(component, otherDock);
}
};
}
else if (component.Docked)
{
verb = new Verb
{
Disabled = !component.Docked,
Text = Loc.GetString("docking-component-undock"),
Act = () =>
{
if (component.DockedWith == null || !component.Enabled) return;
Undock(component);
}
};
}
else
{
return;
}
args.Verbs.Add(verb);
}
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.GridID, out var grid) ||
!EntityManager.HasComponent<ShuttleComponent>(grid.GridEntityId)) return null;
var transform = body.GetTransform();
var dockingFixture = body.GetFixture(DockingFixture);
if (dockingFixture == null)
{
DebugTools.Assert(false);
Logger.ErrorS("docking", $"Found null fixture on {EntityManager.GetEntity(body.OwnerUid)}");
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.Index == dockingXform.GridID) continue;
foreach (var ent in otherGrid.GetAnchoredEntities(enlargedAABB))
{
if (!EntityManager.TryGetComponent(ent, out DockingComponent? otherDocking) ||
!otherDocking.Enabled ||
!EntityManager.TryGetComponent(ent, out PhysicsComponent? otherBody)) continue;
var otherTransform = otherBody.GetTransform();
var otherDockingFixture = otherBody.GetFixture(DockingFixture);
if (otherDockingFixture == null)
{
DebugTools.Assert(false);
Logger.ErrorS("docking", $"Found null docking fixture on {EntityManager.GetEntity(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 dockB = dockA.DockedWith;
if (dockB == null || dockA.DockJoint == null)
{
DebugTools.Assert(false);
Logger.Error("docking", $"Tried to cleanup {dockA.OwnerUid} 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.OwnerUid).GridID).GridEntityId;
var gridBUid = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(dockB.OwnerUid).GridID).GridEntityId;
var msg = new UndockEvent
{
DockA = dockA,
DockB = dockB,
GridAUid = gridAUid,
GridBUid = gridBUid,
};
EntityManager.EventBus.RaiseLocalEvent(dockA.OwnerUid, msg, false);
EntityManager.EventBus.RaiseLocalEvent(dockB.OwnerUid, 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);
}
private void OnAnchorChange(EntityUid uid, DockingComponent component, ref AnchorStateChangedEvent args)
{
if (args.Anchored)
{
EnableDocking(uid, component);
}
else
{
DisableDocking(uid, component);
}
}
private void OnPowerChange(EntityUid uid, DockingComponent component, PowerChangedEvent args)
{
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 (!EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent))
{
return;
}
_broadphaseSystem.DestroyFixture(physicsComponent, DockingFixture);
}
private void EnableDocking(EntityUid uid, DockingComponent component)
{
if (component.Enabled)
return;
if (!EntityManager.TryGetComponent(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.
_broadphaseSystem.CreateFixture(physicsComponent, fixture);
}
/// <summary>
/// Docks 2 ports together and assumes it is valid.
/// </summary>
private void Dock(DockingComponent dockA, DockingComponent dockB)
{
Logger.DebugS("docking", $"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.OwnerUid);
var dockBXform = EntityManager.GetComponent<TransformComponent>(dockB.OwnerUid);
var gridA = _mapManager.GetGrid(dockAXform.GridID).GridEntityId;
var gridB = _mapManager.GetGrid(dockBXform.GridID).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.CreateWeldJoint(gridA, gridB, DockingJoint + dockA.OwnerUid);
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;
dockB.DockedWith = dockA;
dockA.DockJoint = joint;
dockB.DockJoint = joint;
if (EntityManager.TryGetComponent(dockA.OwnerUid, out ServerDoorComponent? doorA))
{
doorA.Open();
}
if (EntityManager.TryGetComponent(dockB.OwnerUid, out ServerDoorComponent? doorB))
{
doorB.Open();
}
var msg = new DockEvent
{
DockA = dockA,
DockB = dockB,
GridAUid = gridA,
GridBUid = gridB,
};
EntityManager.EventBus.RaiseLocalEvent(dockA.OwnerUid, msg, false);
EntityManager.EventBus.RaiseLocalEvent(dockB.OwnerUid, msg, false);
EntityManager.EventBus.RaiseEvent(EventSource.Local, msg);
}
/// <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 (!EntityManager.TryGetComponent(dockA.OwnerUid, out PhysicsComponent? bodyA) ||
!EntityManager.TryGetComponent(dockB.OwnerUid, out PhysicsComponent? bodyB) ||
!dockA.Enabled ||
!dockB.Enabled)
{
return;
}
var fixtureA = bodyA.GetFixture(DockingFixture);
var fixtureB = bodyB.GetFixture(DockingFixture);
if (fixtureA == null || fixtureB == null)
{
return;
}
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;
}
if (!intersect) return;
Dock(dockA, dockB);
}
private void Undock(DockingComponent dock)
{
if (dock.DockedWith == null)
{
DebugTools.Assert(false);
Logger.ErrorS("docking", $"Tried to undock {dock.OwnerUid} but not docked with anything?");
return;
}
if (EntityManager.TryGetComponent(dock.OwnerUid, out ServerDoorComponent? doorA))
{
doorA.Close();
}
if (EntityManager.TryGetComponent(dock.DockedWith.OwnerUid, out ServerDoorComponent? doorB))
{
doorB.Close();
}
// Could maybe give the shuttle a light push away, or at least if there's no other docks left?
Cleanup(dock);
}
/// <summary>
/// Raised whenever 2 airlocks dock.
/// </summary>
public sealed class DockEvent : EntityEventArgs
{
public DockingComponent DockA = default!;
public DockingComponent DockB = default!;
public EntityUid GridAUid = default!;
public EntityUid GridBUid = default!;
}
/// <summary>
/// Raised whenever 2 grids undock.
/// </summary>
public sealed class UndockEvent : EntityEventArgs
{
public DockingComponent DockA = default!;
public DockingComponent DockB = default!;
public EntityUid GridAUid = default!;
public EntityUid GridBUid = default!;
}
}
}

View File

@@ -0,0 +1,192 @@
using Content.Server.Alert;
using Content.Server.Power.Components;
using Content.Server.Shuttles.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Shuttles;
using Content.Shared.Shuttles.Components;
using Content.Shared.Tag;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.EntitySystems
{
internal sealed class ShuttleConsoleSystem : SharedShuttleConsoleSystem
{
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShuttleConsoleComponent, ComponentShutdown>(HandleConsoleShutdown);
SubscribeLocalEvent<PilotComponent, ComponentShutdown>(HandlePilotShutdown);
SubscribeLocalEvent<ShuttleConsoleComponent, ActivateInWorldEvent>(HandleConsoleInteract);
SubscribeLocalEvent<PilotComponent, MoveEvent>(HandlePilotMove);
SubscribeLocalEvent<ShuttleConsoleComponent, PowerChangedEvent>(HandlePowerChange);
}
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.OwnerUid))
{
toRemove.Add(comp);
}
}
foreach (var comp in toRemove)
{
RemovePilot(comp);
}
}
/// <summary>
/// Console requires power to operate.
/// </summary>
private void HandlePowerChange(EntityUid uid, ShuttleConsoleComponent component, PowerChangedEvent args)
{
if (!args.Powered)
{
component.Enabled = false;
ClearPilots(component);
}
else
{
component.Enabled = true;
}
}
/// <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);
}
/// <summary>
/// For now pilots just interact with the console and can start piloting with wasd.
/// </summary>
private void HandleConsoleInteract(EntityUid uid, ShuttleConsoleComponent component, ActivateInWorldEvent args)
{
if (!args.User.HasTag("CanPilot"))
{
return;
}
var pilotComponent = EntityManager.EnsureComponent<PilotComponent>(args.User.Uid);
if (!component.Enabled)
{
args.User.PopupMessage($"Console is not powered.");
return;
}
args.Handled = true;
var console = pilotComponent.Console;
if (console != null)
{
RemovePilot(pilotComponent);
if (console == component)
{
return;
}
}
AddPilot(args.User, component);
}
private void HandlePilotShutdown(EntityUid uid, PilotComponent component, ComponentShutdown args)
{
RemovePilot(component);
}
private void HandleConsoleShutdown(EntityUid uid, ShuttleConsoleComponent component, ComponentShutdown args)
{
ClearPilots(component);
}
public void AddPilot(IEntity entity, ShuttleConsoleComponent component)
{
if (!_blocker.CanInteract(entity.Uid) ||
!entity.TryGetComponent(out PilotComponent? pilotComponent) ||
component.SubscribedPilots.Contains(pilotComponent))
{
return;
}
component.SubscribedPilots.Add(pilotComponent);
if (entity.TryGetComponent(out ServerAlertsComponent? alertsComponent))
{
alertsComponent.ShowAlert(AlertType.PilotingShuttle);
}
entity.PopupMessage(Loc.GetString("shuttle-pilot-start"));
pilotComponent.Console = component;
pilotComponent.Position = EntityManager.GetComponent<TransformComponent>(entity.Uid).Coordinates;
pilotComponent.Dirty();
}
public void RemovePilot(PilotComponent pilotComponent)
{
var console = pilotComponent.Console;
if (console is not ShuttleConsoleComponent helmsman) return;
pilotComponent.Console = null;
pilotComponent.Position = null;
if (!helmsman.SubscribedPilots.Remove(pilotComponent)) return;
if (pilotComponent.Owner.TryGetComponent(out ServerAlertsComponent? alertsComponent))
{
alertsComponent.ClearAlert(AlertType.PilotingShuttle);
}
pilotComponent.Owner.PopupMessage(Loc.GetString("shuttle-pilot-end"));
if (pilotComponent.LifeStage < ComponentLifeStage.Stopping)
EntityManager.RemoveComponent<PilotComponent>(pilotComponent.Owner.Uid);
}
public void RemovePilot(IEntity entity)
{
if (!entity.TryGetComponent(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,117 @@
using System.Collections.Generic;
using Content.Server.Shuttles.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics;
namespace Content.Server.Shuttles.EntitySystems
{
[UsedImplicitly]
internal sealed class ShuttleSystem : EntitySystem
{
private const float TileMassMultiplier = 4f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShuttleComponent, ComponentAdd>(OnShuttleAdd);
SubscribeLocalEvent<ShuttleComponent, ComponentStartup>(OnShuttleStartup);
SubscribeLocalEvent<ShuttleComponent, ComponentShutdown>(OnShuttleShutdown);
SubscribeLocalEvent<GridInitializeEvent>(OnGridInit);
SubscribeLocalEvent<GridFixtureChangeEvent>(OnGridFixtureChange);
}
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;
foreach (var fixture in args.NewFixtures)
{
fixture.Mass = fixture.Area * TileMassMultiplier;
fixture.Restitution = 0.1f;
}
}
private void OnGridInit(GridInitializeEvent ev)
{
EntityManager.GetEntity(ev.EntityUid).EnsureComponent<ShuttleComponent>();
}
private void OnShuttleStartup(EntityUid uid, ShuttleComponent component, ComponentStartup args)
{
if (!component.Owner.HasComponent<IMapGridComponent>())
{
return;
}
if (!component.Owner.TryGetComponent(out PhysicsComponent? physicsComponent))
{
return;
}
if (component.Enabled)
{
Enable(physicsComponent);
}
}
public void Toggle(ShuttleComponent component)
{
if (!component.Owner.TryGetComponent(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 = 0.2f;
component.AngularDamping = 0.3f;
}
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)
{
if (!component.Owner.TryGetComponent(out PhysicsComponent? physicsComponent))
{
return;
}
Disable(physicsComponent);
foreach (var fixture in physicsComponent.Fixtures)
{
fixture.Mass = 0f;
}
}
}
}

View File

@@ -0,0 +1,476 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Audio;
using Content.Server.Power.Components;
using Content.Server.Shuttles.Components;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Physics;
using Content.Shared.Shuttles.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.EntitySystems
{
public sealed class ThrusterSystem : EntitySystem
{
[Robust.Shared.IoC.Dependency] private readonly IMapManager _mapManager = default!;
[Robust.Shared.IoC.Dependency] private readonly AmbientSoundSystem _ambient = default!;
[Robust.Shared.IoC.Dependency] private readonly SharedBroadphaseSystem _broadphase = 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, RotateEvent>(OnRotate);
SubscribeLocalEvent<ThrusterComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<ThrusterComponent, EndCollideEvent>(OnEndCollide);
_mapManager.TileChanged += OnTileChange;
}
public override void Shutdown()
{
base.Shutdown();
_mapManager.TileChanged -= OnTileChange;
}
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() || !e.OldTile.IsSpace()) return;
var tilePos = e.NewTile.GridIndices;
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);
foreach (var ent in _mapManager.GetGrid(e.NewTile.GridIndex).GetAnchoredEntities(checkPos))
{
if (!EntityManager.TryGetComponent(ent, out ThrusterComponent? thruster) || thruster.Type == ThrusterType.Angular) continue;
// Work out if the thruster is facing this direction
var direction = EntityManager.GetComponent<TransformComponent>(ent).LocalRotation.ToWorldVec();
if (new Vector2i((int) direction.X, (int) direction.Y) != new Vector2i(x, y)) continue;
DisableThruster(ent, thruster);
}
}
}
}
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.IsOn ||
component.Type != ThrusterType.Linear ||
!EntityManager.TryGetComponent(uid, out TransformComponent? xform) ||
!_mapManager.TryGetGrid(xform.GridID, out var grid) ||
!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent)) return;
var oldDirection = (int) args.OldRotation.GetCardinalDir() / 2;
var direction = (int) args.NewRotation.GetCardinalDir() / 2;
shuttleComponent.LinearThrusterImpulse[oldDirection] -= component.Impulse;
DebugTools.Assert(shuttleComponent.LinearThrusters[oldDirection].Contains(component));
shuttleComponent.LinearThrusters[oldDirection].Remove(component);
shuttleComponent.LinearThrusterImpulse[direction] += component.Impulse;
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 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.GridID, 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.LinearThrusterImpulse[direction] += component.Impulse;
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.MobImpassable
};
_broadphase.CreateFixture(physicsComponent, fixture);
}
break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust += component.Impulse;
DebugTools.Assert(!shuttleComponent.AngularThrusters.Contains(component));
shuttleComponent.AngularThrusters.Add(component);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (EntityManager.TryGetComponent(uid, out SharedAppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(ThrusterVisualState.State, true);
}
_ambient.SetAmbience(uid, true);
}
/// <summary>
/// Tries to disable the thruster.
/// </summary>
/// <param name="uid"></param>
/// <param name="component"></param>
/// <param name="xform"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void DisableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null)
{
if (!component.IsOn ||
!Resolve(uid, ref xform) ||
!_mapManager.TryGetGrid(xform.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:
var direction = ((int) xform.LocalRotation.GetCardinalDir() / 2);
shuttleComponent.LinearThrusterImpulse[direction] -= component.Impulse;
DebugTools.Assert(shuttleComponent.LinearThrusters[direction].Contains(component));
shuttleComponent.LinearThrusters[direction].Remove(component);
break;
case ThrusterType.Angular:
shuttleComponent.AngularThrust -= component.Impulse;
DebugTools.Assert(shuttleComponent.AngularThrusters.Contains(component));
shuttleComponent.AngularThrusters.Remove(component);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (EntityManager.TryGetComponent(uid, out SharedAppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(ThrusterVisualState.State, false);
}
_ambient.SetAmbience(uid, false);
if (EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent))
{
_broadphase.DestroyFixture(physicsComponent, BurnFixture);
}
_activeThrusters.Remove(component);
component.Colliding.Clear();
}
public bool CanEnable(EntityUid uid, ThrusterComponent component)
{
if (!component.Enabled) return false;
var xform = EntityManager.GetComponent<TransformComponent>(uid);
if (!xform.Anchored ||
EntityManager.TryGetComponent(uid, out ApcPowerReceiverComponent? receiver) && !receiver.Powered)
{
return false;
}
if (component.Type == ThrusterType.Angular)
return true;
var (x, y) = xform.LocalPosition + xform.LocalRotation.Opposite().ToWorldVec();
var tile = _mapManager.GetGrid(xform.GridID).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())
{
if (!comp.Firing || comp.Damage == null || comp.Paused || comp.Deleted) 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.OwnerUid);
}
private void OnEndCollide(EntityUid uid, ThrusterComponent component, EndCollideEvent args)
{
if (args.OurFixture.ID != BurnFixture) return;
component.Colliding.Remove(args.OtherFixture.Body.OwnerUid);
if (component.Colliding.Count == 0)
{
_activeThrusters.Remove(component);
}
}
/// <summary>
/// Considers a thrust direction as being active.
/// </summary>
public void EnableThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) != 0x0) return;
component.ThrustDirections |= direction;
if ((direction & (DirectionFlag.East | DirectionFlag.West)) != 0x0)
{
switch (component.Mode)
{
case ShuttleMode.Cruise:
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
break;
case ShuttleMode.Docking:
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
break;
}
}
else
{
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = true;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, true);
}
}
}
/// <summary>
/// Disables a thrust direction.
/// </summary>
public void DisableThrustDirection(ShuttleComponent component, DirectionFlag direction)
{
if ((component.ThrustDirections & direction) == 0x0) return;
component.ThrustDirections &= ~direction;
if ((direction & (DirectionFlag.East | DirectionFlag.West)) != 0x0)
{
switch (component.Mode)
{
case ShuttleMode.Cruise:
foreach (var comp in component.AngularThrusters)
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
break;
case ShuttleMode.Docking:
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
break;
}
}
else
{
var index = GetFlagIndex(direction);
foreach (var comp in component.LinearThrusters[index])
{
if (!EntityManager.TryGetComponent(comp.OwnerUid, out SharedAppearanceComponent? appearanceComponent))
continue;
comp.Firing = false;
appearanceComponent.SetData(ThrusterVisualState.Thrusting, false);
}
}
}
public void DisableAllThrustDirections(ShuttleComponent component)
{
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
{
DisableThrustDirection(component, dir);
}
DebugTools.Assert(component.ThrustDirections == DirectionFlag.None);
}
#endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetFlagIndex(DirectionFlag flag)
{
return (int) Math.Log2((int) flag);
}
}
}