* small patch * fix * remove garbage * fix * moved to shared events * fix * LocId --------- Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using Content.Server.Research.Systems;
|
|
using Content.Server.Research.TechnologyDisk.Components;
|
|
using Content.Shared.UserInterface;
|
|
using Content.Shared.Research;
|
|
using Content.Shared.Research.Components;
|
|
using Robust.Server.Audio;
|
|
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);
|
|
|
|
var query = EntityQueryEnumerator<DiskConsolePrintingComponent, DiskConsoleComponent, TransformComponent>();
|
|
while (query.MoveNext(out var uid, out var printing, out var console, out var xform))
|
|
{
|
|
if (printing.FinishTime > _timing.CurTime)
|
|
continue;
|
|
|
|
RemComp(uid, printing);
|
|
Spawn(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.ModifyServerPoints(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 = !(TryComp<DiskConsolePrintingComponent>(uid, out var printing) && printing.FinishTime >= _timing.CurTime) &&
|
|
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);
|
|
}
|
|
}
|