Power Cell Refactor (#5943)
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
41
Content.Shared/PowerCell/Components/PowerCellComponent.cs
Normal file
41
Content.Shared/PowerCell/Components/PowerCellComponent.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.PowerCell;
|
||||
|
||||
/// <summary>
|
||||
/// This component enables power-cell related interactions (e.g., entity white-lists, cell sizes, examine, rigging).
|
||||
/// The actual power functionality is provided by the server-side BatteryComponent.
|
||||
/// </summary>
|
||||
[NetworkedComponent]
|
||||
[RegisterComponent]
|
||||
[ComponentProtoName("PowerCell")]
|
||||
public sealed class PowerCellComponent : Component
|
||||
{
|
||||
public const string SolutionName = "powerCell";
|
||||
public const int PowerCellVisualsLevels = 4;
|
||||
|
||||
[DataField("cellSize")]
|
||||
public PowerCellSize CellSize = PowerCellSize.Small;
|
||||
|
||||
// Not networked to clients
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool IsRigged { get; set; }
|
||||
}
|
||||
|
||||
public enum PowerCellSize
|
||||
{
|
||||
Small = 0,
|
||||
Medium = 1,
|
||||
Large = 2
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum PowerCellVisuals
|
||||
{
|
||||
ChargeLevel
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.PowerCell.Components;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class PowerCellSlotComponent : Component
|
||||
{
|
||||
public override string Name => "PowerCellSlot";
|
||||
|
||||
/// <summary>
|
||||
/// What size of cell fits into this component.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("slotSize")]
|
||||
public PowerCellSize SlotSize { get; set; } = PowerCellSize.Small;
|
||||
|
||||
/// <summary>
|
||||
/// The actual item-slot that contains the cell. Allows all the interaction logic to be handled by <see cref="ItemSlotsSystem"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Given that <see cref="PowerCellSystem"/> needs to verify that a given cell has the correct cell-size before
|
||||
/// inserting anyways, there is no need to specify a separate entity whitelist. In this slot's yaml definition.
|
||||
/// </remarks>
|
||||
[DataField("cellSlot")]
|
||||
public ItemSlot CellSlot = new();
|
||||
|
||||
/// <summary>
|
||||
/// Name of the item-slot used to store cells. Determines the eject/insert verb text. E.g., "Eject > Power cell".
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is simply used provide a default value for <see cref="CellSlot.Name"/>. If this string is empty or
|
||||
/// whitespace, the verb will instead use the full name of any cell (e.g., "eject > small super-capacity power
|
||||
/// cell").
|
||||
/// </remarks>
|
||||
[DataField("slotName")]
|
||||
public readonly string SlotName = "power-cell-slot-component-slot-name-default"; // gets Loc.GetString()-ed by ItemSlotsSystem
|
||||
|
||||
/// <summary>
|
||||
/// True if we don't want a cell inserted during map init. If a starting item is defined
|
||||
/// in the <see cref="CellSlot"/> yaml definition, that always takes precedence.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If false, the cell will start with a standard cell with a matching cell-size.
|
||||
/// </remarks>
|
||||
[DataField("startEmpty")]
|
||||
public bool StartEmpty = false;
|
||||
|
||||
/// <summary>
|
||||
/// Descriptive text to add to add when examining an entity with a cell slot. If empty or whitespace, will not add
|
||||
/// any text.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("descFormatString")]
|
||||
public string? DescFormatString { get; set; } = "power-cell-slot-component-description-default";
|
||||
|
||||
/// <summary>
|
||||
/// Can this entity be inserted directly into a charging station? If false, you need to manually remove the power
|
||||
/// cell and recharge it separately.
|
||||
/// </summary>
|
||||
[DataField("fitsInCharger")]
|
||||
public bool FitsInCharger = true;
|
||||
}
|
||||
|
||||
public class PowerCellChangedEvent : EntityEventArgs
|
||||
{
|
||||
public readonly bool Ejected;
|
||||
|
||||
public PowerCellChangedEvent(bool ejected)
|
||||
{
|
||||
Ejected = ejected;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.PowerCell
|
||||
{
|
||||
public static class SharedPowerCell
|
||||
{
|
||||
public const int PowerCellVisualsLevels = 4;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum PowerCellVisuals
|
||||
{
|
||||
ChargeLevel
|
||||
}
|
||||
}
|
||||
109
Content.Shared/PowerCell/SharedPowerCellSystem.cs
Normal file
109
Content.Shared/PowerCell/SharedPowerCellSystem.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.PowerCell.Components;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using System;
|
||||
|
||||
namespace Content.Shared.PowerCell;
|
||||
|
||||
public abstract class SharedPowerCellSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, ComponentInit>(OnCellSlotInit);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, ComponentRemove>(OnCellSlotRemove);
|
||||
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, ExaminedEvent>(OnSlotExamined);
|
||||
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
|
||||
SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
|
||||
}
|
||||
|
||||
private void OnCellInsertAttempt(EntityUid uid, PowerCellSlotComponent component, ContainerIsInsertingAttemptEvent args)
|
||||
{
|
||||
if (!component.Initialized)
|
||||
return;
|
||||
|
||||
if (args.Container.ID != component.CellSlot.ID)
|
||||
return;
|
||||
|
||||
if (!TryComp(args.EntityUid, out PowerCellComponent? cell) || cell.CellSize != component.SlotSize)
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCellInserted(EntityUid uid, PowerCellSlotComponent component, EntInsertedIntoContainerMessage args)
|
||||
{
|
||||
if (!component.Initialized)
|
||||
return;
|
||||
|
||||
if (args.Container.ID != component.CellSlot.ID)
|
||||
return;
|
||||
|
||||
RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false);
|
||||
}
|
||||
|
||||
private void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
|
||||
{
|
||||
if (args.Container.ID != component.CellSlot.ID)
|
||||
return;
|
||||
|
||||
RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
|
||||
}
|
||||
|
||||
private void OnCellSlotInit(EntityUid uid, PowerCellSlotComponent component, ComponentInit args)
|
||||
{
|
||||
_itemSlotsSystem.AddItemSlot(uid, "cellslot_cell_container", component.CellSlot);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(component.CellSlot.Name) &&
|
||||
!string.IsNullOrWhiteSpace(component.SlotName))
|
||||
{
|
||||
component.CellSlot.Name = component.SlotName;
|
||||
}
|
||||
|
||||
if (component.StartEmpty)
|
||||
return;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(component.CellSlot.StartingItem))
|
||||
return;
|
||||
|
||||
// set default starting cell based on cell-type
|
||||
component.CellSlot.StartingItem = component.SlotSize switch
|
||||
{
|
||||
PowerCellSize.Small => "PowerCellSmallStandard",
|
||||
PowerCellSize.Medium => "PowerCellMediumStandard",
|
||||
PowerCellSize.Large => "PowerCellLargeStandard",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
private void OnCellSlotRemove(EntityUid uid, PowerCellSlotComponent component, ComponentRemove args)
|
||||
{
|
||||
_itemSlotsSystem.RemoveItemSlot(uid, component.CellSlot);
|
||||
}
|
||||
|
||||
private void OnSlotExamined(EntityUid uid, PowerCellSlotComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange || string.IsNullOrWhiteSpace(component.DescFormatString))
|
||||
return;
|
||||
|
||||
var sizeText = Loc.GetString(component.SlotSize switch
|
||||
{
|
||||
PowerCellSize.Small => "power-cell-slot-component-description-size-small",
|
||||
PowerCellSize.Medium => "power-cell-slot-component-description-size-medium",
|
||||
PowerCellSize.Large => "power-cell-slot-component-description-size-large",
|
||||
_ => "???"
|
||||
});
|
||||
|
||||
args.PushMarkup(Loc.GetString(component.DescFormatString, ("size", sizeText)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user