Power cell slot QOL (#15373)
This commit is contained in:
25
Content.Server/PowerCell/PowerCellDrawComponent.cs
Normal file
25
Content.Server/PowerCell/PowerCellDrawComponent.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Content.Server.PowerCell;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the entity's ActivatableUI requires power or else it closes.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class PowerCellDrawComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("enabled")]
|
||||
public bool Enabled = false;
|
||||
|
||||
/// <summary>
|
||||
/// How much the entity draws while the UI is open.
|
||||
/// Set to 0 if you just wish to check for power upon opening the UI.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("drawRate")]
|
||||
public float DrawRate = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// How much power is used whenever the entity is "used".
|
||||
/// This is used to ensure the UI won't open again without a minimum use power.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("useRate")]
|
||||
public float UseRate = 0f;
|
||||
}
|
||||
7
Content.Server/PowerCell/PowerCellSlotEmptyEvent.cs
Normal file
7
Content.Server/PowerCell/PowerCellSlotEmptyEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Content.Server.PowerCell;
|
||||
|
||||
/// <summary>
|
||||
/// Raised directed on an entity when its active power cell has no more charge to supply.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public readonly record struct PowerCellSlotEmptyEvent;
|
||||
@@ -10,19 +10,25 @@ using Content.Shared.Rounding;
|
||||
using Robust.Shared.Containers;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Server.Kitchen.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Rejuvenate;
|
||||
using Content.Shared.UserInterface;
|
||||
using Robust.Server.GameObjects;
|
||||
|
||||
namespace Content.Server.PowerCell;
|
||||
|
||||
public sealed class PowerCellSystem : SharedPowerCellSystem
|
||||
{
|
||||
[Dependency] private readonly ActivatableUISystem _activatable = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
|
||||
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _sharedAppearanceSystem = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -39,6 +45,28 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
|
||||
SubscribeLocalEvent<BatteryComponent, BeingMicrowavedEvent>(OnMicrowaved);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
|
||||
|
||||
while (query.MoveNext(out var uid, out var comp, out var slot))
|
||||
{
|
||||
if (!comp.Enabled)
|
||||
continue;
|
||||
|
||||
if (!TryGetBatteryFromSlot(uid, out var battery, slot))
|
||||
continue;
|
||||
|
||||
if (battery.TryUseCharge(comp.DrawRate * frameTime))
|
||||
continue;
|
||||
|
||||
comp.Enabled = false;
|
||||
var ev = new PowerCellSlotEmptyEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRejuvenate(EntityUid uid, PowerCellComponent component, RejuvenateEvent args)
|
||||
{
|
||||
component.IsRigged = false;
|
||||
@@ -102,6 +130,89 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
|
||||
QueueDel(uid);
|
||||
}
|
||||
|
||||
#region Activatable
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the entity has a slotted battery and <see cref="PowerCellDrawComponent.UseRate"/> charge.
|
||||
/// </summary>
|
||||
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
|
||||
public bool HasActivatableCharge(EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null)
|
||||
{
|
||||
if (!Resolve(uid, ref battery, ref cell, false))
|
||||
return false;
|
||||
|
||||
return HasCharge(uid, battery.UseRate, cell, user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to use the <see cref="PowerCellDrawComponent.UseRate"/> for this entity.
|
||||
/// </summary>
|
||||
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
|
||||
public bool TryUseActivatableCharge(EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null)
|
||||
{
|
||||
if (!Resolve(uid, ref battery, ref cell, false))
|
||||
return false;
|
||||
|
||||
if (TryUseCharge(uid, battery.UseRate, cell, user))
|
||||
{
|
||||
_activatable.CheckUsage(uid);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the entity has a slotted battery and charge for the requested action.
|
||||
/// </summary>
|
||||
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
|
||||
public bool HasCharge(EntityUid uid, float charge, PowerCellSlotComponent? component = null, EntityUid? user = null)
|
||||
{
|
||||
if (!TryGetBatteryFromSlot(uid, out var battery, component))
|
||||
{
|
||||
if (user != null)
|
||||
_popup.PopupEntity(Loc.GetString("power-cell-no-battery"), uid, user.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (battery.CurrentCharge < charge)
|
||||
{
|
||||
if (user != null)
|
||||
_popup.PopupEntity(Loc.GetString("power-cell-insufficient"), uid, user.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to use charge from a slotted battery.
|
||||
/// </summary>
|
||||
public bool TryUseCharge(EntityUid uid, float charge, PowerCellSlotComponent? component = null, EntityUid? user = null)
|
||||
{
|
||||
if (!TryGetBatteryFromSlot(uid, out var battery, component))
|
||||
{
|
||||
if (user != null)
|
||||
_popup.PopupEntity(Loc.GetString("power-cell-no-battery"), uid, user.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!battery.TryUseCharge(charge))
|
||||
{
|
||||
if (user != null)
|
||||
_popup.PopupEntity(Loc.GetString("power-cell-insufficient"), uid, user.Value);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetBatteryFromSlot(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery, PowerCellSlotComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component, false))
|
||||
|
||||
Reference in New Issue
Block a user