Flatpacks and the Flatpacker 1001 (#23338)

* Flatpacker and flatpacks

* ok that's good enough

* convert solars/AME to flatpacks

* mats, mats, we are the mats

* basic mechanics are DONE

* thing

* final UI

* sloth

* rped jumpscare

* rename
This commit is contained in:
Nemanja
2024-01-03 01:16:02 -05:00
committed by GitHub
parent 04fc06e9d4
commit 1c11332fa4
40 changed files with 1066 additions and 101 deletions

View File

@@ -0,0 +1,48 @@
using Content.Shared.Construction;
using Content.Shared.Construction.Components;
using Robust.Client.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.Client.Construction;
/// <inheritdoc/>
public sealed class FlatpackSystem : SharedFlatpackSystem
{
[Dependency] private readonly AppearanceSystem _appearance = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FlatpackComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private void OnAppearanceChange(Entity<FlatpackComponent> ent, ref AppearanceChangeEvent args)
{
var (_, comp) = ent;
if (!_appearance.TryGetData<string>(ent, FlatpackVisuals.Machine, out var machineBoardId) || args.Sprite == null)
return;
if (!PrototypeManager.TryIndex<EntityPrototype>(machineBoardId, out var machineBoardPrototype))
return;
if (!machineBoardPrototype.TryGetComponent<SpriteComponent>(out var sprite))
return;
Color? color = null;
foreach (var layer in sprite.AllLayers)
{
if (layer.RsiState.Name is not { } spriteState)
continue;
if (!comp.BoardColors.TryGetValue(spriteState, out var c))
continue;
color = c;
break;
}
if (color != null)
args.Sprite.LayerSetColor(FlatpackVisualLayers.Overlay, color.Value);
}
}

View File

@@ -0,0 +1,40 @@
using Content.Shared.Construction.Components;
using JetBrains.Annotations;
namespace Content.Client.Construction.UI
{
[UsedImplicitly]
public sealed class FlatpackCreatorBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private FlatpackCreatorMenu? _menu;
public FlatpackCreatorBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_menu = new FlatpackCreatorMenu(Owner);
_menu.OnClose += Close;
_menu.PackButtonPressed += () =>
{
SendMessage(new FlatpackCreatorStartPackBuiMessage());
};
_menu.OpenCentered();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_menu?.Dispose();
}
}
}

View File

@@ -0,0 +1,43 @@
<controls:FancyWindow
xmlns="https://spacestation14.io"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:ui="clr-namespace:Content.Client.Materials.UI"
Title="{Loc 'flatpacker-ui-title'}"
MinSize="550 350">
<BoxContainer Orientation="Horizontal" HorizontalExpand="True" VerticalExpand="True" Margin="10">
<BoxContainer SizeFlagsStretchRatio="2" Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<BoxContainer Orientation="Vertical">
<SpriteView Name="MachineSprite" Scale="4 4" HorizontalAlignment="Center" MinSize="128 128"/>
<RichTextLabel Name="MachineNameLabel" HorizontalAlignment="Center" StyleClasses="LabelKeyText"/>
</BoxContainer>
<Control MinHeight="10"/>
<Button Name="PackButton" Text="{Loc 'flatpacker-ui-pack-button'}" MaxWidth="150" Margin="0 0 0 10"/>
<BoxContainer Orientation="Vertical" VerticalExpand="True">
<Label Name="CostHeaderLabel" Text="{Loc 'flatpacker-ui-cost-label'}" HorizontalAlignment="Left"/>
<PanelContainer VerticalExpand="True"
HorizontalExpand="True">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
<BoxContainer Orientation="Vertical" VerticalExpand="True">
<RichTextLabel Name="CostLabel" HorizontalAlignment="Center"/>
</BoxContainer>
</PanelContainer>
</BoxContainer>
</BoxContainer>
<Control MinWidth="10"/>
<BoxContainer SizeFlagsStretchRatio="3" Orientation="Vertical" HorizontalExpand="True" VerticalExpand="True">
<Label Text="{Loc 'flatpacker-ui-materials-label'}" HorizontalAlignment="Center" Margin="0 0 0 5"/>
<PanelContainer
VerticalExpand="True"
HorizontalExpand="True"
RectClipContent="True">
<PanelContainer.PanelOverride>
<gfx:StyleBoxFlat BackgroundColor="#1B1B1E" />
</PanelContainer.PanelOverride>
<ui:MaterialStorageControl Name="MaterialStorageControl"/>
</PanelContainer>
</BoxContainer>
</BoxContainer>
</controls:FancyWindow>

View File

@@ -0,0 +1,147 @@
using System.Linq;
using Content.Client.Materials;
using Content.Client.Message;
using Content.Client.UserInterface.Controls;
using Content.Shared.Construction.Components;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Materials;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Construction.UI;
[GenerateTypedNameReferences]
public sealed partial class FlatpackCreatorMenu : FancyWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly ItemSlotsSystem _itemSlots;
private readonly FlatpackSystem _flatpack;
private readonly MaterialStorageSystem _materialStorage;
private readonly SpriteSystem _spriteSystem;
private readonly EntityUid _owner;
private EntityUid? _currentBoard = EntityUid.Invalid;
private EntityUid? _machinePreview;
public event Action? PackButtonPressed;
public FlatpackCreatorMenu(EntityUid uid)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_itemSlots = _entityManager.System<ItemSlotsSystem>();
_flatpack = _entityManager.System<FlatpackSystem>();
_materialStorage = _entityManager.System<MaterialStorageSystem>();
_spriteSystem = _entityManager.System<SpriteSystem>();
_owner = uid;
PackButton.OnPressed += _ => PackButtonPressed?.Invoke();
MaterialStorageControl.SetOwner(uid);
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (_machinePreview is not { } && _entityManager.Deleted(_machinePreview))
{
_machinePreview = null;
MachineSprite.SetEntity(_machinePreview);
}
if (!_entityManager.TryGetComponent<FlatpackCreatorComponent>(_owner, out var flatpacker) ||
!_itemSlots.TryGetSlot(_owner, flatpacker.SlotId, out var itemSlot))
return;
if (flatpacker.Packing)
{
PackButton.Disabled = true;
}
else if (_currentBoard != null)
{
//todo double trycomp is kinda stinky.
if (_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var board) &&
board.Prototype != null)
{
var cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker),
(_currentBoard.Value, board));
PackButton.Disabled = !_materialStorage.CanChangeMaterialAmount(_owner, cost);
}
}
if (_currentBoard == itemSlot.Item)
return;
if (_machinePreview != null)
_entityManager.DeleteEntity(_machinePreview);
_currentBoard = itemSlot.Item;
CostHeaderLabel.Visible = _currentBoard != null;
if (_currentBoard != null &&
_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var machineBoard) &&
machineBoard.Prototype != null)
{
var proto = _prototypeManager.Index<EntityPrototype>(machineBoard.Prototype);
_machinePreview = _entityManager.Spawn(proto.ID);
_spriteSystem.ForceUpdate(_machinePreview.Value);
MachineNameLabel.SetMessage(proto.Name);
var cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker),
(_currentBoard.Value, machineBoard));
CostLabel.SetMarkup(GetCostString(cost));
}
else
{
_machinePreview = null;
MachineNameLabel.SetMessage(" ");
CostLabel.SetMessage(Loc.GetString("flatpacker-ui-no-board-label"));
PackButton.Disabled = true;
}
MachineSprite.SetEntity(_machinePreview);
}
//todo beautify
private string GetCostString(Dictionary<string, int> costs)
{
var orderedCosts = costs.OrderBy(p => p.Value);
var msg = new FormattedMessage();
foreach (var (mat, amount) in orderedCosts)
{
var matProto = _prototypeManager.Index<MaterialPrototype>(mat);
var sheetVolume = _materialStorage.GetSheetVolume(matProto);
var sheets = (float) -amount / sheetVolume;
var amountText = Loc.GetString("lathe-menu-material-amount",
("amount", sheets),
("unit", Loc.GetString(matProto.Unit)));
var text = Loc.GetString("lathe-menu-tooltip-display",
("amount", amountText),
("material", Loc.GetString(matProto.Name)));
msg.AddMarkup(text);
msg.PushNewline();
}
msg.Pop();
return msg.ToMarkup();
}
public override void Close()
{
base.Close();
_entityManager.DeleteEntity(_machinePreview);
_machinePreview = null;
}
}