mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Remove items from vending machine once bought
This commit is contained in:
parent
d020b067af
commit
674f79f079
5 changed files with 58 additions and 17 deletions
|
|
@ -8,18 +8,30 @@ namespace Cirno.Scripts.Actors;
|
|||
|
||||
public partial class VendingMachine : Interactable
|
||||
{
|
||||
|
||||
[Export] public bool Infinite { get; private set; } = false;
|
||||
|
||||
[Export] public PackedScene UiScene { get; private set; }
|
||||
|
||||
[Export] public Array<LootItem> Items { get; set; }
|
||||
|
||||
|
||||
|
||||
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
GameManager.Instance.ChangeState(GameState.Shop);
|
||||
|
||||
var ui = UiScene.Instantiate<VendingMachineUi>();
|
||||
|
||||
ui.Items = Items.ToArray();
|
||||
ui.Machine = this;
|
||||
|
||||
ui.Items = new LootItem[3];
|
||||
int i = 0;
|
||||
foreach (var item in Items)
|
||||
{
|
||||
ui.Items[i] = item;
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
this.AddChild(ui);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Actors;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
|
@ -10,6 +11,8 @@ public partial class VendingMachineUi : CanvasLayer
|
|||
[Export] public Label MoneyLabel { get; private set; }
|
||||
[Export] public Container ItemsContainer { get; private set; }
|
||||
|
||||
public VendingMachine Machine { get; set; }
|
||||
|
||||
public LootItem[] Items { get; set; } = new LootItem[3];
|
||||
|
||||
public override void _Ready()
|
||||
|
|
@ -19,7 +22,7 @@ public partial class VendingMachineUi : CanvasLayer
|
|||
|
||||
private void FillItems()
|
||||
{
|
||||
var buttons = ItemsContainer.GetChildren().Cast<Button>();
|
||||
var buttons = ItemsContainer.GetChildren().Cast<Button>().ToList();
|
||||
|
||||
int i = 0;
|
||||
foreach (var button in buttons)
|
||||
|
|
@ -27,6 +30,7 @@ public partial class VendingMachineUi : CanvasLayer
|
|||
var item = Items[i];
|
||||
if (item is null)
|
||||
{
|
||||
button.Disabled = true;
|
||||
button.Icon = new PlaceholderTexture2D() { Size = new Vector2(16, 16) };
|
||||
button.Text = "Sold Out";
|
||||
i++;
|
||||
|
|
@ -44,6 +48,8 @@ public partial class VendingMachineUi : CanvasLayer
|
|||
var moneyAmount = InventoryManager.Instance.GetItemCount("CREDITS");
|
||||
|
||||
MoneyLabel.Text = $"{moneyAmount}";
|
||||
|
||||
buttons.First().GrabFocus();
|
||||
}
|
||||
|
||||
private void ButtonOnPressed(LootItem item)
|
||||
|
|
@ -54,15 +60,20 @@ public partial class VendingMachineUi : CanvasLayer
|
|||
{
|
||||
// Buy!
|
||||
InventoryManager.Instance.RemoveItem("CREDITS", item.Price);
|
||||
|
||||
InventoryManager.Instance.AddItem(item);
|
||||
|
||||
if (!Machine.Infinite)
|
||||
{
|
||||
Machine.Items.Remove(item);
|
||||
}
|
||||
|
||||
Close();
|
||||
Exit();
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
private void Exit()
|
||||
{
|
||||
GD.Print("Closing");
|
||||
GameManager.Instance.ChangeState(GameState.Playing);
|
||||
QueueFree();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue