ECS cargo telepad and cleanup (#6450)

Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2022-02-15 15:01:45 +11:00
committed by GitHub
parent baa16c96b4
commit 334568dad2
16 changed files with 368 additions and 160 deletions

View File

@@ -11,7 +11,7 @@ using Robust.Shared.IoC;
namespace Content.Server.Cargo
{
public class CargoConsoleSystem : EntitySystem
public sealed partial class CargoSystem
{
/// <summary>
/// How much time to wait (in seconds) before increasing bank accounts balance.
@@ -50,36 +50,41 @@ namespace Content.Server.Cargo
[Dependency] private readonly IdCardSystem _idCardSystem = default!;
[Dependency] private readonly AccessReaderSystem _accessReaderSystem = default!;
public override void Initialize()
private void InitializeConsole()
{
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
CreateBankAccount("Space Station 14", 1000);
CreateOrderDatabase(0);
Reset();
}
public override void Update(float frameTime)
private void Reset(RoundRestartCleanupEvent ev)
{
_timer += frameTime;
if (_timer < Delay)
{
return;
}
_timer -= Delay;
foreach (var account in BankAccounts)
{
account.Balance += PointIncrease;
}
Reset();
}
public void Reset(RoundRestartCleanupEvent ev)
private void Reset()
{
_accountsDict.Clear();
_databasesDict.Clear();
_timer = 0;
_accountIndex = 0;
Initialize();
CreateBankAccount("Space Station 14", 1000);
CreateOrderDatabase(0);
}
private void UpdateConsole(float frameTime)
{
_timer += frameTime;
while (_timer > Delay)
{
_timer -= Delay;
foreach (var account in BankAccounts)
{
account.Balance += PointIncrease;
}
}
}
/// <summary>

View File

@@ -0,0 +1,130 @@
using Content.Server.Cargo.Components;
using Content.Server.Labels.Components;
using Content.Server.Paper;
using Content.Server.Power.Components;
using Content.Shared.Cargo;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.Cargo;
public sealed partial class CargoSystem
{
private void InitializeTelepad()
{
SubscribeLocalEvent<CargoTelepadComponent, PowerChangedEvent>(OnTelepadPowerChange);
SubscribeLocalEvent<CargoTelepadComponent, AnchorStateChangedEvent>(OnTelepadAnchorChange);
}
private void UpdateTelepad(float frameTime)
{
foreach (var comp in EntityManager.EntityQuery<CargoTelepadComponent>())
{
// Don't EntityQuery for it as it's not required.
TryComp<AppearanceComponent>(comp.Owner, out var appearance);
if (comp.CurrentState == CargoTelepadState.Unpowered || comp.TeleportQueue.Count <= 0)
{
comp.CurrentState = CargoTelepadState.Idle;
appearance?.SetData(CargoTelepadVisuals.State, CargoTelepadState.Idle);
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;
appearance?.SetData(CargoTelepadVisuals.State, CargoTelepadState.Idle);
continue;
}
var product = comp.TeleportQueue.Pop();
SoundSystem.Play(Filter.Pvs(comp.Owner), comp.TeleportSound.GetSound(), comp.Owner, AudioParams.Default.WithVolume(-8f));
SpawnProduct(comp, product);
comp.CurrentState = CargoTelepadState.Teleporting;
appearance?.SetData(CargoTelepadVisuals.State, CargoTelepadState.Teleporting);
comp.Accumulator = comp.Delay;
}
}
private void SetEnabled(CargoTelepadComponent component, ApcPowerReceiverComponent? receiver = null,
TransformComponent? xform = null)
{
// False due to AllCompsOneEntity test where they may not have the powerreceiver.
if (!Resolve(component.Owner, ref receiver, ref xform, false)) return;
var disabled = !receiver.Powered || !xform.Anchored;
// Setting idle state should be handled by Update();
if (disabled) return;
TryComp<AppearanceComponent>(component.Owner, out var appearance);
component.CurrentState = CargoTelepadState.Unpowered;
appearance?.SetData(CargoTelepadVisuals.State, CargoTelepadState.Unpowered);
}
private void OnTelepadPowerChange(EntityUid uid, CargoTelepadComponent component, PowerChangedEvent args)
{
SetEnabled(component);
}
private void OnTelepadAnchorChange(EntityUid uid, CargoTelepadComponent component, ref AnchorStateChangedEvent args)
{
SetEnabled(component);
}
public void QueueTeleport(CargoTelepadComponent component, CargoOrderData order)
{
for (var i = 0; i < order.Amount; i++)
{
component.TeleportQueue.Push(order);
}
}
/// <summary>
/// Spawn the product and a piece of paper. Attempt to attach the paper to the product.
/// </summary>
private void SpawnProduct(CargoTelepadComponent component, CargoOrderData data)
{
// spawn the order
if (!_protoMan.TryIndex(data.ProductId, out CargoProductPrototype? prototype))
return;
var xform = Transform(component.Owner);
var product = EntityManager.SpawnEntity(prototype.Product, xform.Coordinates);
Transform(product).Anchored = false;
// spawn a piece of paper.
var printed = EntityManager.SpawnEntity(component.PrinterOutput, xform.Coordinates);
if (!TryComp<PaperComponent>(printed, out var paper))
return;
// fill in the order data
var val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", data.OrderNumber));
MetaData(printed).EntityName = val;
paper.SetContent(Loc.GetString(
"cargo-console-paper-print-text",
("orderNumber", data.OrderNumber),
("requester", data.Requester),
("reason", data.Reason),
("approver", data.Approver)));
// attempt to attach the label
if (TryComp<PaperLabelComponent>(product, out var label))
{
_slots.TryInsert(component.Owner, label.LabelSlot, printed, null);
}
}
}

View File

@@ -0,0 +1,27 @@
using Content.Shared.Cargo;
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.Cargo;
public sealed partial class CargoSystem : SharedCargoSystem
{
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly ItemSlotsSystem _slots = default!;
public override void Initialize()
{
base.Initialize();
InitializeConsole();
InitializeTelepad();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
UpdateConsole(frameTime);
UpdateTelepad(frameTime);
}
}

View File

@@ -60,7 +60,7 @@ namespace Content.Server.Cargo.Components
private SoundSpecifier _errorSound = new SoundPathSpecifier("/Audio/Effects/error.ogg");
private bool Powered => !_entMan.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
private CargoConsoleSystem _cargoConsoleSystem = default!;
private CargoSystem _cargoConsoleSystem = default!;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(CargoConsoleUiKey.Key);
@@ -76,7 +76,7 @@ namespace Content.Server.Cargo.Components
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
_cargoConsoleSystem = EntitySystem.Get<CargoConsoleSystem>();
_cargoConsoleSystem = EntitySystem.Get<CargoSystem>();
BankAccount = _cargoConsoleSystem.StationAccount;
}
@@ -188,7 +188,7 @@ namespace Content.Server.Cargo.Components
orders.Database.ClearOrderCapacity();
foreach (var order in approvedOrders)
{
telepadComponent.QueueTeleport(order);
_cargoConsoleSystem.QueueTeleport(telepadComponent, order);
}
}
}

View File

@@ -14,7 +14,7 @@ namespace Content.Server.Cargo.Components
{
base.Initialize();
Database = EntitySystem.Get<CargoConsoleSystem>().StationOrderDatabase;
Database = EntitySystem.Get<CargoSystem>().StationOrderDatabase;
}
public override ComponentState GetComponentState()

View File

@@ -1,150 +1,38 @@
using System;
using System.Collections.Generic;
using Content.Server.Labels.Components;
using Content.Server.Paper;
using Content.Server.Power.Components;
using Content.Shared.Cargo;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Cargo.Components;
using Content.Shared.Sound;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Cargo.Components
{
//This entire class is a PLACEHOLDER for the cargo shuttle.
//welp only need auto-docking now.
[RegisterComponent]
public class CargoTelepadComponent : Component
/// <summary>
/// Handles teleporting in requested cargo after the specified delay.
/// </summary>
[RegisterComponent, Friend(typeof(CargoSystem))]
public sealed class CargoTelepadComponent : SharedCargoTelepadComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[DataField("delay")]
public float Delay = 20f;
private const float TeleportDuration = 0.5f;
private const float TeleportDelay = 15f;
private List<CargoOrderData> _teleportQueue = new();
private CargoTelepadState _currentState = CargoTelepadState.Unpowered;
[DataField("teleportSound")] private SoundSpecifier _teleportSound = new SoundPathSpecifier("/Audio/Machines/phasein.ogg");
/// <summary>
/// How much time we've accumulated until next teleport.
/// </summary>
[ViewVariables]
public float Accumulator = 0f;
[ViewVariables]
public readonly Stack<CargoOrderData> TeleportQueue = new();
[ViewVariables]
public CargoTelepadState CurrentState = CargoTelepadState.Unpowered;
[DataField("teleportSound")] public SoundSpecifier TeleportSound = new SoundPathSpecifier("/Audio/Machines/phasein.ogg");
/// <summary>
/// The paper-type prototype to spawn with the order information.
/// </summary>
[DataField("printerOutput", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string PrinterOutput = "Paper";
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
#pragma warning disable 618
base.HandleMessage(message, component);
#pragma warning restore 618
switch (message)
{
case PowerChangedMessage powerChanged:
PowerUpdate(powerChanged);
break;
}
}
public void QueueTeleport(CargoOrderData order)
{
for (var i = 0; i < order.Amount; i++)
{
_teleportQueue.Add(order);
}
TeleportLoop();
}
private void PowerUpdate(PowerChangedMessage args)
{
if (args.Powered && _currentState == CargoTelepadState.Unpowered) {
_currentState = CargoTelepadState.Idle;
if(_entMan.TryGetComponent<SpriteComponent?>(Owner, out var spriteComponent) && spriteComponent.LayerCount > 0)
spriteComponent.LayerSetState(0, "idle");
TeleportLoop();
}
else if (!args.Powered)
{
_currentState = CargoTelepadState.Unpowered;
if (_entMan.TryGetComponent<SpriteComponent?>(Owner, out var spriteComponent) && spriteComponent.LayerCount > 0)
spriteComponent.LayerSetState(0, "offline");
}
}
private void TeleportLoop()
{
if (_currentState == CargoTelepadState.Idle && _teleportQueue.Count > 0)
{
_currentState = CargoTelepadState.Charging;
if (_entMan.TryGetComponent<SpriteComponent?>(Owner, out var spriteComponent) && spriteComponent.LayerCount > 0)
spriteComponent.LayerSetState(0, "idle");
Owner.SpawnTimer((int) (TeleportDelay * 1000), () =>
{
if (!Deleted && !_entMan.Deleted(Owner) && _currentState == CargoTelepadState.Charging && _teleportQueue.Count > 0)
{
_currentState = CargoTelepadState.Teleporting;
if (_entMan.TryGetComponent<SpriteComponent?>(Owner, out var spriteComponent) && spriteComponent.LayerCount > 0)
spriteComponent.LayerSetState(0, "beam");
Owner.SpawnTimer((int) (TeleportDuration * 1000), () =>
{
if (!Deleted && !((!_entMan.EntityExists(Owner) ? EntityLifeStage.Deleted : _entMan.GetComponent<MetaDataComponent>(Owner).EntityLifeStage) >= EntityLifeStage.Deleted) && _currentState == CargoTelepadState.Teleporting && _teleportQueue.Count > 0)
{
SoundSystem.Play(Filter.Pvs(Owner), _teleportSound.GetSound(), Owner, AudioParams.Default.WithVolume(-8f));
SpawnProduct(_teleportQueue[0]);
_teleportQueue.RemoveAt(0);
if (_entMan.TryGetComponent<SpriteComponent?>(Owner, out var spriteComponent) && spriteComponent.LayerCount > 0)
spriteComponent.LayerSetState(0, "idle");
_currentState = CargoTelepadState.Idle;
TeleportLoop();
}
});
}
});
}
}
/// <summary>
/// Spawn the product and a piece of paper. Attempt to attach the paper to the product.
/// </summary>
private void SpawnProduct(CargoOrderData data)
{
// spawn the order
if (!_prototypeManager.TryIndex(data.ProductId, out CargoProductPrototype? prototype))
return;
var product = _entMan.SpawnEntity(prototype.Product, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
_entMan.GetComponent<TransformComponent>(product).Anchored = false;
// spawn a piece of paper.
var printed = _entMan.SpawnEntity(PrinterOutput, _entMan.GetComponent<TransformComponent>(Owner).Coordinates);
if (!_entMan.TryGetComponent(printed, out PaperComponent paper))
return;
// fill in the order data
string val = Loc.GetString("cargo-console-paper-print-name", ("orderNumber", data.OrderNumber));
_entMan.GetComponent<MetaDataComponent>(printed).EntityName = val;
paper.SetContent(Loc.GetString(
"cargo-console-paper-print-text",
("orderNumber", data.OrderNumber),
("requester", data.Requester),
("reason", data.Reason),
("approver", data.Approver)));
// attempt to attach the label
if (_entMan.TryGetComponent(product, out PaperLabelComponent label))
{
EntitySystem.Get<ItemSlotsSystem>().TryInsert(Owner, label.LabelSlot, printed, null);
}
}
private enum CargoTelepadState { Unpowered, Idle, Charging, Teleporting };
}
}