Remove items from vending machine once bought

This commit is contained in:
Marco 2025-04-25 12:21:33 +02:00
commit 674f79f079
5 changed files with 58 additions and 17 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3 uid="uid://ca0g1jnl6n0pl"]
[gd_scene load_steps=10 format=3 uid="uid://ca0g1jnl6n0pl"]
[ext_resource type="Theme" uid="uid://dnsadvmunm76k" path="res://Resources/Styles/MainMenuButtons.tres" id="1_2iadx"]
[ext_resource type="Script" uid="uid://dwj7p7ydustm5" path="res://Scripts/UI/VendingMachineUi.cs" id="1_dy2e4"]
@ -8,7 +8,16 @@
[ext_resource type="Texture2D" uid="uid://4x3ouxyxjqjc" path="res://Sprites/Items/Credits_Pickup.png" id="4_dy2e4"]
[ext_resource type="Texture2D" uid="uid://bjdsi4x8m42ky" path="res://Sprites/Items/Power_Pickup.png" id="5_dy2e4"]
[sub_resource type="InputEventKey" id="InputEventKey_dy2e4"]
device = -1
pressed = true
keycode = 4194305
[sub_resource type="Shortcut" id="Shortcut_16ivy"]
events = [SubResource("InputEventKey_dy2e4")]
[node name="VendingMachineUi" type="CanvasLayer" node_paths=PackedStringArray("MoneyLabel", "ItemsContainer")]
process_mode = 3
layer = 2
script = ExtResource("1_dy2e4")
MoneyLabel = NodePath("PanelContainer/VBoxContainer/HBoxContainer3/Label")
@ -41,10 +50,13 @@ layout_mode = 2
size_flags_horizontal = 10
text = "Buy"
[node name="Button" type="Button" parent="PanelContainer/VBoxContainer/HBoxContainer"]
[node name="Exit" type="Button" parent="PanelContainer/VBoxContainer/HBoxContainer"]
z_index = 1
layout_mode = 2
size_flags_horizontal = 10
text = "X"
size_flags_stretch_ratio = 0.8
shortcut = SubResource("Shortcut_16ivy")
text = "Exit"
[node name="HBoxContainer2" type="HBoxContainer" parent="PanelContainer/VBoxContainer"]
process_mode = 3
@ -99,4 +111,4 @@ texture = ExtResource("4_dy2e4")
layout_mode = 2
text = "123456"
[connection signal="pressed" from="PanelContainer/VBoxContainer/HBoxContainer/Button" to="." method="Close"]
[connection signal="pressed" from="PanelContainer/VBoxContainer/HBoxContainer/Exit" to="." method="Exit"]

View file

@ -70,5 +70,6 @@ autoplay = true
[node name="Minimap" type="CanvasLayer" parent="." node_paths=PackedStringArray("RoomManager")]
script = ExtResource("16_pfafs")
RoomManager = NodePath("../Maps")
CellSize = 4
[connection signal="ManagerReady" from="." to="Maps" method="InitSpawning"]

View file

@ -1,13 +1,14 @@
[gd_scene load_steps=10 format=4 uid="uid://24wh7h2dbljf"]
[gd_scene load_steps=11 format=4 uid="uid://24wh7h2dbljf"]
[ext_resource type="Script" uid="uid://b2j00riayxkit" path="res://Scripts/Controllers/RogueliteRoom.cs" id="1_vhsym"]
[ext_resource type="Resource" uid="uid://dn3ai56rrxfnk" path="res://Resources/RogueliteMaps/Beginner1.tres" id="2_vhsym"]
[ext_resource type="PackedScene" uid="uid://l84on3kv2s52" path="res://Scenes/Door_Horizontal.tscn" id="3_pys6w"]
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="3_u3c1h"]
[ext_resource type="PackedScene" uid="uid://b0k2grrc8xp1l" path="res://Scenes/Props/BigTank.tscn" id="4_70a8f"]
[ext_resource type="Script" uid="uid://krean0uywtms" path="res://Scripts/TilemapAvoidance.cs" id="4_pys6w"]
[ext_resource type="PackedScene" uid="uid://bc64lr3vlwchq" path="res://Scenes/Door_Vertical.tscn" id="4_s67gl"]
[ext_resource type="PackedScene" uid="uid://cl7s06sk106uo" path="res://Scenes/Props/VendingMachine.tscn" id="6_70a8f"]
[ext_resource type="PackedScene" uid="uid://4q82gggqax87" path="res://Scenes/Items/Cheat_Gun_Pickup.tscn" id="6_pys6w"]
[ext_resource type="PackedScene" uid="uid://djf0y08ix66fn" path="res://Scenes/Interactable/Chest.tscn" id="7_kublc"]
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="8_dbn5e"]
[ext_resource type="Resource" uid="uid://cltxhkrqp055v" path="res://Resources/Items/Money_Pickup.tres" id="9_5yebg"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_u3c1h"]
size = Vector2(272, 85)
@ -16,9 +17,6 @@ size = Vector2(272, 85)
process_mode = 1
script = ExtResource("1_vhsym")
RoomResource = ExtResource("2_vhsym")
DoorPrefab = ExtResource("3_pys6w")
VerticalDoorPrefab = ExtResource("4_s67gl")
WallPrefab = ExtResource("4_70a8f")
metadata/test_scene = "res://Scenes/Maps/Roguelike.tscn"
[node name="Floor" type="TileMapLayer" parent="." node_paths=PackedStringArray("_solidLayers") groups=["Solid"]]
@ -46,6 +44,13 @@ metadata/_edit_lock_ = true
[node name="CheaetGun" parent="Actors" instance=ExtResource("6_pys6w")]
position = Vector2(119, 127)
[node name="VendingMachine" parent="Actors" instance=ExtResource("6_70a8f")]
position = Vector2(248, 56)
[node name="Chest" parent="Actors" instance=ExtResource("7_kublc")]
position = Vector2(216, 55)
LootTable = Array[ExtResource("8_dbn5e")]([ExtResource("9_5yebg"), ExtResource("9_5yebg"), ExtResource("9_5yebg"), ExtResource("9_5yebg"), ExtResource("9_5yebg"), ExtResource("9_5yebg")])
[node name="EnemySpawners" type="Node2D" parent="."]
[node name="PlayerEnterDetector" type="Area2D" parent="."]

View file

@ -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);

View file

@ -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();
}