mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-07 19:45:54 +00:00
Vending Machines
This commit is contained in:
parent
e25da0fe16
commit
d020b067af
16 changed files with 249 additions and 34 deletions
69
Scripts/UI/VendingMachineUi.cs
Normal file
69
Scripts/UI/VendingMachineUi.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Resources;
|
||||
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 LootItem[] Items { get; set; } = new LootItem[3];
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
FillItems();
|
||||
}
|
||||
|
||||
private void FillItems()
|
||||
{
|
||||
var buttons = ItemsContainer.GetChildren().Cast<Button>();
|
||||
|
||||
int i = 0;
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
var item = Items[i];
|
||||
if (item is null)
|
||||
{
|
||||
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}";
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
GameManager.Instance.ChangeState(GameState.Playing);
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
1
Scripts/UI/VendingMachineUi.cs.uid
Normal file
1
Scripts/UI/VendingMachineUi.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dwj7p7ydustm5
|
||||
Loading…
Add table
Add a link
Reference in a new issue