cirnogodot/Scripts/UI/VendingMachineUi.cs

81 lines
2 KiB
C#
Raw Permalink Normal View History

2025-04-25 11:29:21 +02:00
using System.Linq;
using Cirno.Scripts.Actors;
2025-04-25 11:29:21 +02:00
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
2025-04-25 11:29:21 +02:00
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.UI;
public partial class VendingMachineUi : CanvasLayer
{
[Export] public Label MoneyLabel { get; private set; }
[Export] public Container ItemsContainer { get; private set; }
public VendingMachine Machine { get; set; }
2025-04-25 11:29:21 +02:00
public LootItem[] Items { get; set; } = new LootItem[3];
public override void _Ready()
{
FillItems();
}
private void FillItems()
{
var buttons = ItemsContainer.GetChildren().Cast<Button>().ToList();
2025-04-25 11:29:21 +02:00
int i = 0;
foreach (var button in buttons)
{
var item = Items[i];
if (item is null)
{
button.Disabled = true;
2025-04-25 11:29:21 +02:00
button.Icon = new PlaceholderTexture2D() { Size = new Vector2(16, 16) };
button.Text = "Sold Out";
i++;
continue;
}
button.Icon = item.InventorySprite;
button.Text = $"{item.ShortName} ({item.Price})";
button.Pressed += () => ButtonOnPressed(item);
i++;
}
var moneyAmount = InventoryManager.Instance.GetItemCount("CREDITS");
MoneyLabel.Text = $"{moneyAmount}";
buttons.First().GrabFocus();
2025-04-25 11:29:21 +02:00
}
private void ButtonOnPressed(LootItem item)
{
var moneyAmount = InventoryManager.Instance.GetItemCount("CREDITS");
if (moneyAmount >= item.Price)
{
// Buy!
InventoryManager.Instance.RemoveItem("CREDITS", item.Price);
InventoryManager.Instance.AddItem(item);
if (!Machine.Infinite)
{
Machine.Items.Remove(item);
}
2025-04-25 11:29:21 +02:00
Exit();
2025-04-25 11:29:21 +02:00
}
}
private void Exit()
2025-04-25 11:29:21 +02:00
{
GD.Print("Closing");
GameStateManager.Instance.ChangeState(GameState.Playing);
2025-04-25 11:29:21 +02:00
QueueFree();
}
}