move hot plate item placement stuff into its own system (#18923)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Construction;
|
||||
@@ -6,27 +5,26 @@ using Content.Server.Power.Components;
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.Placeable;
|
||||
using Robust.Shared.Physics.Events;
|
||||
using Robust.Shared.Physics.Systems;
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems;
|
||||
|
||||
public sealed class SolutionHeaterSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
||||
[Dependency] private readonly PlaceableSurfaceSystem _placeableSurface = default!;
|
||||
[Dependency] private readonly ItemPlacerSystem _itemPlacer = default!;
|
||||
[Dependency] private readonly PowerReceiverSystem _powerReceiver = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solution = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, RefreshPartsEvent>(OnRefreshParts);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, UpgradeExamineEvent>(OnUpgradeExamine);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, StartCollideEvent>(OnStartCollide);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, EndCollideEvent>(OnEndCollide);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, ItemPlacedEvent>(OnItemPlaced);
|
||||
SubscribeLocalEvent<SolutionHeaterComponent, ItemRemovedEvent>(OnItemRemoved);
|
||||
}
|
||||
|
||||
private void TurnOn(EntityUid uid)
|
||||
@@ -35,9 +33,12 @@ public sealed class SolutionHeaterSystem : EntitySystem
|
||||
EnsureComp<ActiveSolutionHeaterComponent>(uid);
|
||||
}
|
||||
|
||||
public bool TryTurnOn(EntityUid uid, SolutionHeaterComponent component)
|
||||
public bool TryTurnOn(EntityUid uid, ItemPlacerComponent? placer = null)
|
||||
{
|
||||
if (component.PlacedEntities.Count <= 0 || !_powerReceiver.IsPowered(uid))
|
||||
if (!Resolve(uid, ref placer))
|
||||
return false;
|
||||
|
||||
if (placer.PlacedEntities.Count <= 0 || !_powerReceiver.IsPowered(uid))
|
||||
return false;
|
||||
|
||||
TurnOn(uid);
|
||||
@@ -52,7 +53,8 @@ public sealed class SolutionHeaterSystem : EntitySystem
|
||||
|
||||
private void OnPowerChanged(EntityUid uid, SolutionHeaterComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
if (args.Powered && component.PlacedEntities.Count > 0)
|
||||
var placer = Comp<ItemPlacerComponent>(uid);
|
||||
if (args.Powered && placer.PlacedEntities.Count > 0)
|
||||
{
|
||||
TurnOn(uid);
|
||||
}
|
||||
@@ -74,43 +76,26 @@ public sealed class SolutionHeaterSystem : EntitySystem
|
||||
args.AddPercentageUpgrade("solution-heater-upgrade-heat", component.HeatPerSecond / component.BaseHeatPerSecond);
|
||||
}
|
||||
|
||||
private void OnStartCollide(EntityUid uid, SolutionHeaterComponent component, ref StartCollideEvent args)
|
||||
private void OnItemPlaced(EntityUid uid, SolutionHeaterComponent comp, ref ItemPlacedEvent args)
|
||||
{
|
||||
if (component.Whitelist is not null && !component.Whitelist.IsValid(args.OtherEntity))
|
||||
return;
|
||||
|
||||
// Disallow sleeping so we can detect when entity is removed from the heater.
|
||||
_physics.SetSleepingAllowed(args.OtherEntity, args.OtherBody, false);
|
||||
|
||||
component.PlacedEntities.Add(args.OtherEntity);
|
||||
|
||||
TryTurnOn(uid, component);
|
||||
|
||||
if (component.PlacedEntities.Count >= component.MaxEntities)
|
||||
_placeableSurface.SetPlaceable(uid, false);
|
||||
TryTurnOn(uid);
|
||||
}
|
||||
|
||||
private void OnEndCollide(EntityUid uid, SolutionHeaterComponent component, ref EndCollideEvent args)
|
||||
private void OnItemRemoved(EntityUid uid, SolutionHeaterComponent component, ref ItemRemovedEvent args)
|
||||
{
|
||||
// Re-allow sleeping.
|
||||
_physics.SetSleepingAllowed(args.OtherEntity, args.OtherBody, true);
|
||||
|
||||
component.PlacedEntities.Remove(args.OtherEntity);
|
||||
|
||||
if (component.PlacedEntities.Count == 0) // Last entity was removed
|
||||
var placer = Comp<ItemPlacerComponent>(uid);
|
||||
if (placer.PlacedEntities.Count == 0) // Last entity was removed
|
||||
TurnOff(uid);
|
||||
|
||||
_placeableSurface.SetPlaceable(uid, true);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var query = EntityQueryEnumerator<ActiveSolutionHeaterComponent, SolutionHeaterComponent>();
|
||||
while (query.MoveNext(out _, out _, out var heater))
|
||||
var query = EntityQueryEnumerator<ActiveSolutionHeaterComponent, SolutionHeaterComponent, ItemPlacerComponent>();
|
||||
while (query.MoveNext(out _, out _, out var heater, out var placer))
|
||||
{
|
||||
foreach (var heatingEntity in heater.PlacedEntities.Take((int) heater.MaxEntities))
|
||||
foreach (var heatingEntity in placer.PlacedEntities)
|
||||
{
|
||||
if (!TryComp<SolutionContainerManagerComponent>(heatingEntity, out var solution))
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user