Raise powercellemptyevent on cell removed, fix powercelldraw (#15679)

This commit is contained in:
Nemanja
2023-04-23 01:27:56 -04:00
committed by GitHub
parent a381031909
commit ef28cfd55f
2 changed files with 22 additions and 3 deletions

View File

@@ -55,10 +55,10 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
if (!comp.Enabled) if (!comp.Enabled)
continue; continue;
if (!TryGetBatteryFromSlot(uid, out var battery, slot)) if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
continue; continue;
if (_battery.TryUseCharge(uid, comp.DrawRate * frameTime, battery)) if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate * frameTime, battery))
continue; continue;
comp.Enabled = false; comp.Enabled = false;
@@ -119,6 +119,14 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
} }
} }
protected override void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
{
base.OnCellRemoved(uid, component, args);
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
private void Explode(EntityUid uid, BatteryComponent? battery = null, EntityUid? cause = null) private void Explode(EntityUid uid, BatteryComponent? battery = null, EntityUid? cause = null)
{ {
if (!Resolve(uid, ref battery)) if (!Resolve(uid, ref battery))
@@ -216,18 +224,29 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
} }
public bool TryGetBatteryFromSlot(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery, PowerCellSlotComponent? component = null) public bool TryGetBatteryFromSlot(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery, PowerCellSlotComponent? component = null)
{
return TryGetBatteryFromSlot(uid, out _, out battery, component);
}
public bool TryGetBatteryFromSlot(EntityUid uid,
[NotNullWhen(true)] out EntityUid? batteryEnt,
[NotNullWhen(true)] out BatteryComponent? battery,
PowerCellSlotComponent? component = null)
{ {
if (!Resolve(uid, ref component, false)) if (!Resolve(uid, ref component, false))
{ {
batteryEnt = null;
battery = null; battery = null;
return false; return false;
} }
if (_itemSlotsSystem.TryGetSlot(uid, component.CellSlotId, out ItemSlot? slot)) if (_itemSlotsSystem.TryGetSlot(uid, component.CellSlotId, out ItemSlot? slot))
{ {
batteryEnt = slot.Item;
return TryComp(slot.Item, out battery); return TryComp(slot.Item, out battery);
} }
batteryEnt = null;
battery = null; battery = null;
return false; return false;
} }

View File

@@ -52,7 +52,7 @@ public abstract class SharedPowerCellSystem : EntitySystem
RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false); RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false);
} }
private void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args) protected virtual void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
{ {
if (args.Container.ID != component.CellSlotId) if (args.Container.ID != component.CellSlotId)
return; return;