mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Vending Machines
This commit is contained in:
parent
e25da0fe16
commit
d020b067af
16 changed files with 249 additions and 34 deletions
28
Scripts/Actors/VendingMachine.cs
Normal file
28
Scripts/Actors/VendingMachine.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.UI;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Actors;
|
||||
|
||||
public partial class VendingMachine : Interactable
|
||||
{
|
||||
[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();
|
||||
|
||||
this.AddChild(ui);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
1
Scripts/Actors/VendingMachine.cs.uid
Normal file
1
Scripts/Actors/VendingMachine.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bg20enqj6y6c8
|
||||
|
|
@ -344,6 +344,7 @@ public partial class GameManager : Node2D
|
|||
{
|
||||
case GameState.Paused:
|
||||
case GameState.Dialogue:
|
||||
case GameState.Shop:
|
||||
case GameState.Inventory:
|
||||
GetTree().SetPause(true);
|
||||
//Input.MouseMode = Input.MouseModeEnum.Visible;
|
||||
|
|
@ -404,5 +405,6 @@ public enum GameState
|
|||
Playing,
|
||||
Dialogue,
|
||||
Controlling,
|
||||
Inventory
|
||||
Inventory,
|
||||
Shop
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ public partial class LootItem : Resource
|
|||
[Export] public StringName ItemDescription { get; set; }
|
||||
[Export] public StringName ItemKey { get; set; }
|
||||
[Export] public ItemTypes Item;
|
||||
[Export] public int Price { get; set; }
|
||||
[Export] public ItemEffectResource ItemEffect { get; private set; }
|
||||
[Export] public WeaponResource WeaponData { get; set; }
|
||||
[Export] public int Amount;
|
||||
|
|
|
|||
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