Cleanup for ChargerComponent (#11907)
This commit is contained in:
@@ -5,6 +5,7 @@ using Content.Shared.Examine;
|
||||
using Content.Shared.PowerCell.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Containers;
|
||||
using Content.Shared.Power;
|
||||
|
||||
namespace Content.Server.Power.EntitySystems;
|
||||
|
||||
@@ -13,11 +14,11 @@ internal sealed class ChargerSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
[Dependency] private readonly PowerCellSystem _cellSystem = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _sharedAppearanceSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<ChargerComponent, ComponentInit>(OnChargerInit);
|
||||
SubscribeLocalEvent<ChargerComponent, ComponentRemove>(OnChargerRemove);
|
||||
SubscribeLocalEvent<ChargerComponent, ComponentStartup>(OnStartup);
|
||||
SubscribeLocalEvent<ChargerComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
SubscribeLocalEvent<ChargerComponent, EntInsertedIntoContainerMessage>(OnInserted);
|
||||
SubscribeLocalEvent<ChargerComponent, EntRemovedFromContainerMessage>(OnRemoved);
|
||||
@@ -25,6 +26,11 @@ internal sealed class ChargerSystem : EntitySystem
|
||||
SubscribeLocalEvent<ChargerComponent, ExaminedEvent>(OnChargerExamine);
|
||||
}
|
||||
|
||||
private void OnStartup(EntityUid uid, ChargerComponent component, ComponentStartup args)
|
||||
{
|
||||
UpdateStatus(uid, component);
|
||||
}
|
||||
|
||||
private void OnChargerExamine(EntityUid uid, ChargerComponent component, ExaminedEvent args)
|
||||
{
|
||||
args.PushMarkup(Loc.GetString("charger-examine", ("color", "yellow"), ("chargeRate", component.ChargeRate)));
|
||||
@@ -32,24 +38,21 @@ internal sealed class ChargerSystem : EntitySystem
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var comp in EntityManager.EntityQuery<ChargerComponent>())
|
||||
foreach (var (_, charger, slotComp) in EntityManager.EntityQuery<ActiveChargerComponent, ChargerComponent, ItemSlotsComponent>())
|
||||
{
|
||||
comp.OnUpdate(frameTime);
|
||||
if (!_itemSlotsSystem.TryGetSlot(charger.Owner, charger.SlotId, out ItemSlot? slot, slotComp))
|
||||
continue;
|
||||
|
||||
if (charger.Status == CellChargerStatus.Empty || charger.Status == CellChargerStatus.Charged || !slot.HasItem)
|
||||
continue;
|
||||
|
||||
TransferPower(charger.Owner, charger, frameTime);
|
||||
}
|
||||
}
|
||||
private void OnChargerInit(EntityUid uid, ChargerComponent component, ComponentInit args)
|
||||
{
|
||||
_itemSlotsSystem.AddItemSlot(uid, "charger-slot", component.ChargerSlot);
|
||||
}
|
||||
|
||||
private void OnChargerRemove(EntityUid uid, ChargerComponent component, ComponentRemove args)
|
||||
{
|
||||
_itemSlotsSystem.RemoveItemSlot(uid, component.ChargerSlot);
|
||||
}
|
||||
|
||||
|
||||
private void OnPowerChanged(EntityUid uid, ChargerComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
component.UpdateStatus();
|
||||
UpdateStatus(uid, component);
|
||||
}
|
||||
|
||||
private void OnInserted(EntityUid uid, ChargerComponent component, EntInsertedIntoContainerMessage args)
|
||||
@@ -57,25 +60,18 @@ internal sealed class ChargerSystem : EntitySystem
|
||||
if (!component.Initialized)
|
||||
return;
|
||||
|
||||
if (args.Container.ID != component.ChargerSlot.ID)
|
||||
if (args.Container.ID != component.SlotId)
|
||||
return;
|
||||
|
||||
// try get a battery directly on the inserted entity
|
||||
if (!TryComp(args.Entity, out component.HeldBattery))
|
||||
{
|
||||
// or by checking for a power cell slot on the inserted entity
|
||||
_cellSystem.TryGetBatteryFromSlot(args.Entity, out component.HeldBattery);
|
||||
}
|
||||
|
||||
component.UpdateStatus();
|
||||
|
||||
UpdateStatus(uid, component);
|
||||
}
|
||||
|
||||
private void OnRemoved(EntityUid uid, ChargerComponent component, EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (args.Container.ID != component.ChargerSlot.ID)
|
||||
if (args.Container.ID != component.SlotId)
|
||||
return;
|
||||
|
||||
component.UpdateStatus();
|
||||
UpdateStatus(uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -86,16 +82,113 @@ internal sealed class ChargerSystem : EntitySystem
|
||||
if (!component.Initialized)
|
||||
return;
|
||||
|
||||
if (args.Container.ID != component.ChargerSlot.ID)
|
||||
if (args.Container.ID != component.SlotId)
|
||||
return;
|
||||
|
||||
if (!TryComp(args.EntityUid, out PowerCellSlotComponent? cellSlot))
|
||||
return;
|
||||
|
||||
if (!_itemSlotsSystem.TryGetSlotById(args.EntityUid, cellSlot.CellSlotId, out ItemSlot? itemSlot))
|
||||
|
||||
if (!_itemSlotsSystem.TryGetSlot(args.EntityUid, cellSlot.CellSlotId, out ItemSlot? itemSlot))
|
||||
return;
|
||||
|
||||
if (!cellSlot.FitsInCharger || !itemSlot.HasItem)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void UpdateStatus(EntityUid uid, ChargerComponent component)
|
||||
{
|
||||
var status = GetStatus(uid, component);
|
||||
if (component.Status == status || !TryComp(uid, out ApcPowerReceiverComponent? receiver))
|
||||
return;
|
||||
|
||||
if (!_itemSlotsSystem.TryGetSlot(uid, component.SlotId, out ItemSlot? slot))
|
||||
return;
|
||||
|
||||
TryComp(uid, out AppearanceComponent? appearance);
|
||||
|
||||
component.Status = status;
|
||||
|
||||
if (component.Status == CellChargerStatus.Charging)
|
||||
{
|
||||
AddComp<ActiveChargerComponent>(uid);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemComp<ActiveChargerComponent>(uid);
|
||||
}
|
||||
|
||||
switch (component.Status)
|
||||
{
|
||||
case CellChargerStatus.Off:
|
||||
receiver.Load = 0;
|
||||
_sharedAppearanceSystem.SetData(uid, CellVisual.Light, CellChargerStatus.Off, appearance);
|
||||
break;
|
||||
case CellChargerStatus.Empty:
|
||||
receiver.Load = 0;
|
||||
_sharedAppearanceSystem.SetData(uid, CellVisual.Light, CellChargerStatus.Empty, appearance);
|
||||
break;
|
||||
case CellChargerStatus.Charging:
|
||||
receiver.Load = component.ChargeRate;
|
||||
_sharedAppearanceSystem.SetData(uid, CellVisual.Light, CellChargerStatus.Charging, appearance);
|
||||
break;
|
||||
case CellChargerStatus.Charged:
|
||||
receiver.Load = 0;
|
||||
_sharedAppearanceSystem.SetData(uid, CellVisual.Light, CellChargerStatus.Charged, appearance);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
_sharedAppearanceSystem.SetData(uid, CellVisual.Occupied, slot.HasItem, appearance);
|
||||
}
|
||||
|
||||
private CellChargerStatus GetStatus(EntityUid uid, ChargerComponent component)
|
||||
{
|
||||
if (!TryComp(uid, out TransformComponent? transformComponent))
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!transformComponent.Anchored)
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!TryComp(uid, out ApcPowerReceiverComponent? apcPowerReceiverComponent))
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!apcPowerReceiverComponent.Powered)
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!_itemSlotsSystem.TryGetSlot(uid, component.SlotId, out ItemSlot? slot))
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!_cellSystem.TryGetBatteryFromSlot(uid, out BatteryComponent? heldBattery))
|
||||
return CellChargerStatus.Off;
|
||||
|
||||
if (!slot.HasItem)
|
||||
return CellChargerStatus.Empty;
|
||||
|
||||
if (heldBattery != null && Math.Abs(heldBattery.MaxCharge - heldBattery.CurrentCharge) < 0.01)
|
||||
return CellChargerStatus.Charged;
|
||||
|
||||
return CellChargerStatus.Charging;
|
||||
}
|
||||
|
||||
private void TransferPower(EntityUid uid, ChargerComponent component, float frameTime)
|
||||
{
|
||||
if (!TryComp(uid, out ApcPowerReceiverComponent? receiverComponent))
|
||||
return;
|
||||
|
||||
if (!receiverComponent.Powered)
|
||||
return;
|
||||
|
||||
if (!_cellSystem.TryGetBatteryFromSlot(uid, out BatteryComponent? heldBattery))
|
||||
return;
|
||||
|
||||
heldBattery.CurrentCharge += component.ChargeRate * frameTime;
|
||||
// Just so the sprite won't be set to 99.99999% visibility
|
||||
if (heldBattery.MaxCharge - heldBattery.CurrentCharge < 0.01)
|
||||
{
|
||||
heldBattery.CurrentCharge = heldBattery.MaxCharge;
|
||||
}
|
||||
|
||||
UpdateStatus(uid, component);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user