ChemMaster ECS (#11052)

This commit is contained in:
0x6273
2022-09-06 07:06:47 +02:00
committed by GitHub
parent 34b7e31e76
commit 0c8e52c163
17 changed files with 513 additions and 798 deletions

View File

@@ -1,134 +0,0 @@
using Content.Shared.Cloning;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.FixedPoint;
using Robust.Shared.Serialization;
namespace Content.Shared.Chemistry.Components
{
/// <summary>
/// Shared class for <c>ChemMasterComponent</c>. Provides a way for entities to split reagents from a beaker and produce pills and bottles via a user interface.
/// </summary>
[Virtual]
public class SharedChemMasterComponent : Component
{
public const int MaxEntitySpawns = 10;
public static string BeakerSlotId = "ChemMaster-beaker";
[DataField("beakerSlot")]
public ItemSlot BeakerSlot = new();
public const string SolutionName = "buffer";
[Serializable, NetSerializable]
public sealed class ChemMasterBoundUserInterfaceState : BoundUserInterfaceState
{
public readonly bool HasPower;
public readonly bool HasBeaker;
public readonly FixedPoint2 BeakerCurrentVolume;
public readonly FixedPoint2 BeakerMaxVolume;
public readonly string ContainerName;
public readonly string Label;
/// <summary>
/// A list of the reagents and their amounts within the beaker/reagent container, if applicable.
/// </summary>
public readonly IReadOnlyList<Solution.ReagentQuantity> ContainerReagents;
/// <summary>
/// A list of the reagents and their amounts within the buffer, if applicable.
/// </summary>
public readonly IReadOnlyList<Solution.ReagentQuantity> BufferReagents;
public readonly string DispenserName;
public readonly bool BufferModeTransfer;
public readonly FixedPoint2 BufferCurrentVolume;
public readonly uint SelectedPillType;
public ChemMasterBoundUserInterfaceState(bool hasPower, bool hasBeaker, FixedPoint2 beakerCurrentVolume, FixedPoint2 beakerMaxVolume, string containerName, string label,
string dispenserName, IReadOnlyList<Solution.ReagentQuantity> containerReagents, IReadOnlyList<Solution.ReagentQuantity> bufferReagents, bool bufferModeTransfer, FixedPoint2 bufferCurrentVolume, uint selectedPillType)
{
HasPower = hasPower;
HasBeaker = hasBeaker;
BeakerCurrentVolume = beakerCurrentVolume;
BeakerMaxVolume = beakerMaxVolume;
ContainerName = containerName;
Label = label;
DispenserName = dispenserName;
ContainerReagents = containerReagents;
BufferReagents = bufferReagents;
BufferModeTransfer = bufferModeTransfer;
BufferCurrentVolume = bufferCurrentVolume;
SelectedPillType = selectedPillType;
}
}
/// <summary>
/// Message data sent from client to server when a ChemMaster ui button is pressed.
/// </summary>
[Serializable, NetSerializable]
public sealed class UiActionMessage : BoundUserInterfaceMessage
{
public readonly UiAction Action;
public readonly FixedPoint2 Amount;
public readonly string Id = "";
public readonly bool IsBuffer;
public readonly string Label = "";
public readonly uint PillType;
public readonly int PillAmount;
public readonly int BottleAmount;
public UiActionMessage(UiAction action, FixedPoint2? amount, string? id, bool? isBuffer, string? label, uint? pillType, int? pillAmount, int? bottleAmount)
{
Action = action;
if (Action == UiAction.ChemButton)
{
Amount = amount.GetValueOrDefault();
if (id == null)
{
Id = "null";
}
else
{
Id = id;
}
IsBuffer = isBuffer.GetValueOrDefault();
}
else
{
PillAmount = pillAmount.GetValueOrDefault();
PillType = pillType.GetValueOrDefault();
BottleAmount = bottleAmount.GetValueOrDefault();
if (label == null)
{
Label = "null";
}
else
{
Label = label;
}
}
}
}
[Serializable, NetSerializable]
public enum ChemMasterUiKey
{
Key
}
/// <summary>
/// Used in <see cref="AcceptCloningChoiceMessage"/> to specify which button was pressed.
/// </summary>
public enum UiAction
{
Transfer,
Discard,
ChemButton,
CreatePills,
CreateBottles,
SetPillType
}
}
}

View File

@@ -1,30 +0,0 @@
using JetBrains.Annotations;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Chemistry.Components;
namespace Content.Shared.Chemistry.EntitySystems
{
[UsedImplicitly]
public abstract class SharedChemMasterSystem : EntitySystem
{
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedChemMasterComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<SharedChemMasterComponent, ComponentRemove>(OnComponentRemove);
}
private void OnComponentInit(EntityUid uid, SharedChemMasterComponent component, ComponentInit args)
{
_itemSlotsSystem.AddItemSlot(uid, SharedChemMasterComponent.BeakerSlotId, component.BeakerSlot);
}
private void OnComponentRemove(EntityUid uid, SharedChemMasterComponent component, ComponentRemove args)
{
_itemSlotsSystem.RemoveItemSlot(uid, component.BeakerSlot);
}
}
}

View File

@@ -0,0 +1,164 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using Robust.Shared.Serialization;
namespace Content.Shared.Chemistry
{
/// <summary>
/// This class holds constants that are shared between client and server.
/// </summary>
public sealed class SharedChemMaster
{
public const uint PillTypes = 20;
public const string BufferSolutionName = "buffer";
public const string ContainerSlotName = "beakerSlot";
public const string PillSolutionName = "food";
public const string BottleSolutionName = "drink";
}
[Serializable, NetSerializable]
public sealed class ChemMasterSetModeMessage : BoundUserInterfaceMessage
{
public readonly ChemMasterMode ChemMasterMode;
public ChemMasterSetModeMessage(ChemMasterMode mode)
{
ChemMasterMode = mode;
}
}
[Serializable, NetSerializable]
public sealed class ChemMasterSetPillTypeMessage : BoundUserInterfaceMessage
{
public readonly uint PillType;
public ChemMasterSetPillTypeMessage(uint pillType)
{
PillType = pillType;
}
}
[Serializable, NetSerializable]
public sealed class ChemMasterReagentAmountButtonMessage : BoundUserInterfaceMessage
{
public readonly string ReagentId;
public readonly ChemMasterReagentAmount Amount;
public readonly bool FromBuffer;
public ChemMasterReagentAmountButtonMessage(string reagentId, ChemMasterReagentAmount amount, bool fromBuffer)
{
ReagentId = reagentId;
Amount = amount;
FromBuffer = fromBuffer;
}
}
[Serializable, NetSerializable]
public sealed class ChemMasterCreatePillsMessage : BoundUserInterfaceMessage
{
public readonly uint Amount;
public readonly string Label;
public ChemMasterCreatePillsMessage(uint amount, string label)
{
Amount = amount;
Label = label;
}
}
[Serializable, NetSerializable]
public sealed class ChemMasterCreateBottlesMessage : BoundUserInterfaceMessage
{
public readonly uint Amount;
public readonly string Label;
public ChemMasterCreateBottlesMessage(uint amount, string label)
{
Amount = amount;
Label = label;
}
}
public enum ChemMasterMode
{
Transfer,
Discard,
}
public enum ChemMasterReagentAmount
{
U1 = 1,
U5 = 5,
U10 = 10,
U25 = 25,
All,
}
public static class ChemMasterReagentAmountToFixedPoint
{
public static FixedPoint2 GetFixedPoint(this ChemMasterReagentAmount amount)
{
if (amount == ChemMasterReagentAmount.All)
return FixedPoint2.MaxValue;
else
return FixedPoint2.New((int)amount);
}
}
[Serializable, NetSerializable]
public sealed class ChemMasterBoundUserInterfaceState : BoundUserInterfaceState
{
public readonly FixedPoint2? ContainerCurrentVolume;
public readonly FixedPoint2? ContainerMaxVolume;
public readonly string? ContainerName;
/// <summary>
/// A list of the reagents and their amounts within the beaker/reagent container, if applicable.
/// </summary>
public readonly IReadOnlyList<Solution.ReagentQuantity>? ContainerReagents;
/// <summary>
/// A list of the reagents and their amounts within the buffer, if applicable.
/// </summary>
public readonly IReadOnlyList<Solution.ReagentQuantity> BufferReagents;
public readonly string DispenserName;
public readonly ChemMasterMode Mode;
public readonly FixedPoint2? BufferCurrentVolume;
public readonly uint SelectedPillType;
public readonly uint PillProductionLimit;
public readonly uint BottleProductionLimit;
public readonly bool UpdateLabel;
public ChemMasterBoundUserInterfaceState(FixedPoint2? containerCurrentVolume, FixedPoint2? containerMaxVolume, string? containerName,
string dispenserName, IReadOnlyList<Solution.ReagentQuantity>? containerReagents, IReadOnlyList<Solution.ReagentQuantity> bufferReagents, ChemMasterMode mode,
FixedPoint2 bufferCurrentVolume, uint selectedPillType, uint pillProdictionLimit, uint bottleProdictionLimit, bool updateLabel)
{
ContainerCurrentVolume = containerCurrentVolume;
ContainerMaxVolume = containerMaxVolume;
ContainerName = containerName;
DispenserName = dispenserName;
ContainerReagents = containerReagents;
BufferReagents = bufferReagents;
Mode = mode;
BufferCurrentVolume = bufferCurrentVolume;
SelectedPillType = selectedPillType;
PillProductionLimit = pillProdictionLimit;
BottleProductionLimit = bottleProdictionLimit;
UpdateLabel = updateLabel;
}
public bool HasContainer()
{
return ContainerCurrentVolume is not null;
}
}
[Serializable, NetSerializable]
public enum ChemMasterUiKey
{
Key
}
}