Files
OldThink/Content.Server/AME/AntimatterEngineSystem.cs

118 lines
4.5 KiB
C#
Raw Normal View History

using System.Linq;
2023-01-20 10:17:57 -06:00
using Content.Server.Administration.Logs;
2022-04-15 17:20:20 -04:00
using Content.Server.AME.Components;
2022-03-29 03:58:51 +11:00
using Content.Server.Power.Components;
2022-04-15 17:20:20 -04:00
using Content.Server.Hands.Components;
using Content.Server.Popups;
using Content.Server.Tools;
2023-01-20 10:17:57 -06:00
using Content.Shared.Database;
2022-04-15 17:20:20 -04:00
using Content.Shared.Interaction;
using Content.Shared.Popups;
2022-04-15 17:20:20 -04:00
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Audio;
using JetBrains.Annotations;
2021-06-09 22:19:39 +02:00
namespace Content.Server.AME
{
[UsedImplicitly]
public sealed class AntimatterEngineSystem : EntitySystem
{
2022-04-15 17:20:20 -04:00
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly ToolSystem _toolSystem = default!;
2023-01-20 10:17:57 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
private float _accumulatedFrameTime;
2022-03-29 03:58:51 +11:00
private const float UpdateCooldown = 10f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AMEControllerComponent, PowerChangedEvent>(OnAMEPowerChange);
2022-04-15 17:20:20 -04:00
SubscribeLocalEvent<AMEControllerComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<AMEPartComponent, InteractUsingEvent>(OnPartInteractUsing);
2022-03-29 03:58:51 +11:00
}
public override void Update(float frameTime)
{
base.Update(frameTime);
2022-03-29 03:58:51 +11:00
// TODO: Won't exactly work with replays I guess?
_accumulatedFrameTime += frameTime;
2022-03-29 03:58:51 +11:00
if (_accumulatedFrameTime >= UpdateCooldown)
{
foreach (var comp in EntityManager.EntityQuery<AMEControllerComponent>())
{
comp.OnUpdate(frameTime);
}
2022-03-29 03:58:51 +11:00
_accumulatedFrameTime -= UpdateCooldown;
}
2022-03-29 03:58:51 +11:00
}
private static void OnAMEPowerChange(EntityUid uid, AMEControllerComponent component, ref PowerChangedEvent args)
2022-03-29 03:58:51 +11:00
{
component.UpdateUserInterface();
}
2022-04-15 17:20:20 -04:00
private void OnInteractUsing(EntityUid uid, AMEControllerComponent component, InteractUsingEvent args)
{
if (!TryComp(args.User, out HandsComponent? hands))
{
_popupSystem.PopupEntity(Loc.GetString("ame-controller-component-interact-using-no-hands-text"), uid, args.User);
2022-04-15 17:20:20 -04:00
return;
}
if (HasComp<AMEFuelContainerComponent?>(args.Used))
{
if (component.HasJar)
{
_popupSystem.PopupEntity(Loc.GetString("ame-controller-component-interact-using-already-has-jar"), uid, args.User);
2022-04-15 17:20:20 -04:00
}
else
{
component.JarSlot.Insert(args.Used);
_popupSystem.PopupEntity(Loc.GetString("ame-controller-component-interact-using-success"), uid,
args.User, PopupType.Medium);
2022-04-15 17:20:20 -04:00
component.UpdateUserInterface();
}
}
else
{
_popupSystem.PopupEntity(Loc.GetString("ame-controller-component-interact-using-fail"), uid, args.User);
2022-04-15 17:20:20 -04:00
}
}
private void OnPartInteractUsing(EntityUid uid, AMEPartComponent component, InteractUsingEvent args)
{
if (!HasComp<HandsComponent>(args.User))
{
_popupSystem.PopupEntity(Loc.GetString("ame-part-component-interact-using-no-hands"), uid, args.User);
2022-04-15 17:20:20 -04:00
return;
}
if (!_toolSystem.HasQuality(args.Used, component.QualityNeeded))
return;
2022-06-20 12:14:35 +12:00
if (!_mapManager.TryGetGrid(args.ClickLocation.GetGridUid(EntityManager), out var mapGrid))
2022-04-15 17:20:20 -04:00
return; // No AME in space.
var snapPos = mapGrid.TileIndicesFor(args.ClickLocation);
if (mapGrid.GetAnchoredEntities(snapPos).Any(sc => HasComp<AMEShieldComponent>(sc)))
{
_popupSystem.PopupEntity(Loc.GetString("ame-part-component-shielding-already-present"), uid, args.User);
2022-04-15 17:20:20 -04:00
return;
}
var ent = EntityManager.SpawnEntity("AMEShielding", mapGrid.GridTileToLocal(snapPos));
2023-01-20 10:17:57 -06:00
_adminLogger.Add(LogType.Construction, LogImpact.Low, $"{ToPrettyString(args.User):player} unpacked {ToPrettyString(ent)} at {Transform(ent).Coordinates} from {ToPrettyString(uid)}");
SoundSystem.Play(component.UnwrapSound.GetSound(), Filter.Pvs(uid), uid);
2022-04-15 17:20:20 -04:00
EntityManager.QueueDeleteEntity(uid);
}
}
}