Files
OldThink/Content.Server/Cargo/Systems/CargoSystem.Telepad.cs

110 lines
4.1 KiB
C#
Raw Normal View History

using Content.Server.Cargo.Components;
using Content.Server.Power.Components;
using Content.Shared.Cargo;
2023-05-21 06:09:31 -04:00
using Content.Shared.Cargo.Components;
using Content.Shared.DeviceLinking;
using Robust.Shared.Audio;
2023-05-21 06:09:31 -04:00
using Robust.Shared.Utility;
namespace Content.Server.Cargo.Systems;
public sealed partial class CargoSystem
{
private void InitializeTelepad()
{
SubscribeLocalEvent<CargoTelepadComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<CargoTelepadComponent, PowerChangedEvent>(OnTelepadPowerChange);
// Shouldn't need re-anchored event
SubscribeLocalEvent<CargoTelepadComponent, AnchorStateChangedEvent>(OnTelepadAnchorChange);
}
private void UpdateTelepad(float frameTime)
{
2023-05-21 06:09:31 -04:00
var query = EntityQueryEnumerator<CargoTelepadComponent>();
while (query.MoveNext(out var uid, out var comp))
{
// Don't EntityQuery for it as it's not required.
2023-05-21 06:09:31 -04:00
TryComp<AppearanceComponent>(uid, out var appearance);
2022-06-23 14:36:47 +10:00
if (comp.CurrentState == CargoTelepadState.Unpowered)
{
comp.CurrentState = CargoTelepadState.Idle;
2023-05-21 06:09:31 -04:00
_appearance.SetData(uid, CargoTelepadVisuals.State, CargoTelepadState.Idle, appearance);
comp.Accumulator = comp.Delay;
continue;
}
if (!TryComp<DeviceLinkSinkComponent>(uid, out var sinkComponent) ||
sinkComponent.LinkedSources.FirstOrNull() is not { } console ||
!HasComp<CargoOrderConsoleComponent>(console))
{
comp.Accumulator = comp.Delay;
continue;
}
comp.Accumulator -= frameTime;
// Uhh listen teleporting takes time and I just want the 1 float.
if (comp.Accumulator > 0f)
{
comp.CurrentState = CargoTelepadState.Idle;
2023-05-21 06:09:31 -04:00
_appearance.SetData(uid, CargoTelepadVisuals.State, CargoTelepadState.Idle, appearance);
continue;
}
2023-05-21 06:09:31 -04:00
var station = _station.GetOwningStation(console);
2022-06-23 14:36:47 +10:00
if (!TryComp<StationCargoOrderDatabaseComponent>(station, out var orderDatabase) ||
orderDatabase.Orders.Count == 0)
{
comp.Accumulator += comp.Delay;
continue;
}
2023-05-21 06:09:31 -04:00
var xform = Transform(uid);
if (FulfillNextOrder(orderDatabase, xform.Coordinates, comp.PrinterOutput))
2022-06-23 14:36:47 +10:00
{
2023-05-21 06:09:31 -04:00
_audio.PlayPvs(_audio.GetSound(comp.TeleportSound), uid, AudioParams.Default.WithVolume(-8f));
UpdateOrders(station.Value, orderDatabase);
2022-06-23 14:36:47 +10:00
comp.CurrentState = CargoTelepadState.Teleporting;
2023-05-21 06:09:31 -04:00
_appearance.SetData(uid, CargoTelepadVisuals.State, CargoTelepadState.Teleporting, appearance);
2022-06-23 14:36:47 +10:00
}
comp.Accumulator += comp.Delay;
}
}
private void OnInit(EntityUid uid, CargoTelepadComponent telepad, ComponentInit args)
{
_linker.EnsureSinkPorts(uid, telepad.ReceiverPort);
}
2023-05-21 06:09:31 -04:00
private void SetEnabled(EntityUid uid, CargoTelepadComponent component, ApcPowerReceiverComponent? receiver = null,
TransformComponent? xform = null)
{
// False due to AllCompsOneEntity test where they may not have the powerreceiver.
2023-05-21 06:09:31 -04:00
if (!Resolve(uid, ref receiver, ref xform, false))
return;
var disabled = !receiver.Powered || !xform.Anchored;
// Setting idle state should be handled by Update();
2023-05-21 06:09:31 -04:00
if (disabled)
return;
2023-05-21 06:09:31 -04:00
TryComp<AppearanceComponent>(uid, out var appearance);
component.CurrentState = CargoTelepadState.Unpowered;
2023-05-21 06:09:31 -04:00
_appearance.SetData(uid, CargoTelepadVisuals.State, CargoTelepadState.Unpowered, appearance);
}
private void OnTelepadPowerChange(EntityUid uid, CargoTelepadComponent component, ref PowerChangedEvent args)
{
2023-05-21 06:09:31 -04:00
SetEnabled(uid, component);
}
private void OnTelepadAnchorChange(EntityUid uid, CargoTelepadComponent component, ref AnchorStateChangedEvent args)
{
2023-05-21 06:09:31 -04:00
SetEnabled(uid, component);
}
}