* clean up infinite researchsystem shitcode * fml some more shit * make syncing work logically * naming naming naming
98 lines
3.6 KiB
C#
98 lines
3.6 KiB
C#
using Content.Server.Research.Systems;
|
|
using Content.Server.Research.TechnologyDisk.Components;
|
|
using Content.Server.UserInterface;
|
|
using Content.Shared.Research;
|
|
using Content.Shared.Research.Components;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Server.Research.TechnologyDisk.Systems;
|
|
|
|
public sealed class DiskConsoleSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly IGameTiming _timing = default!;
|
|
[Dependency] private readonly AudioSystem _audio = default!;
|
|
[Dependency] private readonly ResearchSystem _research = default!;
|
|
[Dependency] private readonly UserInterfaceSystem _ui = default!;
|
|
|
|
/// <inheritdoc/>
|
|
public override void Initialize()
|
|
{
|
|
SubscribeLocalEvent<DiskConsoleComponent, DiskConsolePrintDiskMessage>(OnPrintDisk);
|
|
SubscribeLocalEvent<DiskConsoleComponent, ResearchServerPointsChangedEvent>(OnPointsChanged);
|
|
SubscribeLocalEvent<DiskConsoleComponent, ResearchRegistrationChangedEvent>(OnRegistrationChanged);
|
|
SubscribeLocalEvent<DiskConsoleComponent, BeforeActivatableUIOpenEvent>(OnBeforeUiOpen);
|
|
|
|
SubscribeLocalEvent<DiskConsolePrintingComponent, ComponentShutdown>(OnShutdown);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
|
|
foreach (var (printing, console, xform) in EntityQuery<DiskConsolePrintingComponent, DiskConsoleComponent, TransformComponent>())
|
|
{
|
|
if (printing.FinishTime > _timing.CurTime)
|
|
continue;
|
|
|
|
RemComp(printing.Owner, printing);
|
|
EntityManager.SpawnEntity(console.DiskPrototype, xform.Coordinates);
|
|
}
|
|
}
|
|
|
|
private void OnPrintDisk(EntityUid uid, DiskConsoleComponent component, DiskConsolePrintDiskMessage args)
|
|
{
|
|
if (HasComp<DiskConsolePrintingComponent>(uid))
|
|
return;
|
|
|
|
if (!_research.TryGetClientServer(uid, out var server, out var serverComp))
|
|
return;
|
|
|
|
if (serverComp.Points < component.PricePerDisk)
|
|
return;
|
|
|
|
_research.AddPointsToServer(server.Value, -component.PricePerDisk, serverComp);
|
|
_audio.PlayPvs(component.PrintSound, uid);
|
|
|
|
var printing = EnsureComp<DiskConsolePrintingComponent>(uid);
|
|
printing.FinishTime = _timing.CurTime + component.PrintDuration;
|
|
UpdateUserInterface(uid, component);
|
|
}
|
|
|
|
private void OnPointsChanged(EntityUid uid, DiskConsoleComponent component, ref ResearchServerPointsChangedEvent args)
|
|
{
|
|
UpdateUserInterface(uid, component);
|
|
}
|
|
|
|
private void OnRegistrationChanged(EntityUid uid, DiskConsoleComponent component, ref ResearchRegistrationChangedEvent args)
|
|
{
|
|
UpdateUserInterface(uid, component);
|
|
}
|
|
|
|
private void OnBeforeUiOpen(EntityUid uid, DiskConsoleComponent component, BeforeActivatableUIOpenEvent args)
|
|
{
|
|
UpdateUserInterface(uid, component);
|
|
}
|
|
|
|
public void UpdateUserInterface(EntityUid uid, DiskConsoleComponent? component = null)
|
|
{
|
|
if (!Resolve(uid, ref component, false))
|
|
return;
|
|
|
|
var totalPoints = 0;
|
|
if (_research.TryGetClientServer(uid, out _, out var server))
|
|
{
|
|
totalPoints = server.Points;
|
|
}
|
|
var canPrint = !HasComp<DiskConsolePrintingComponent>(uid) && totalPoints >= component.PricePerDisk;
|
|
|
|
var state = new DiskConsoleBoundUserInterfaceState(totalPoints, component.PricePerDisk, canPrint);
|
|
_ui.TrySetUiState(uid, DiskConsoleUiKey.Key, state);
|
|
}
|
|
|
|
private void OnShutdown(EntityUid uid, DiskConsolePrintingComponent component, ComponentShutdown args)
|
|
{
|
|
UpdateUserInterface(uid);
|
|
}
|
|
}
|