diff --git a/Scenes/Crafting.gd b/Scenes/Crafting.gd deleted file mode 100644 index a9f2075..0000000 --- a/Scenes/Crafting.gd +++ /dev/null @@ -1,2 +0,0 @@ -extends Control - diff --git a/Scenes/inventory.tscn b/Scenes/inventory.tscn index c888908..bf31428 100644 --- a/Scenes/inventory.tscn +++ b/Scenes/inventory.tscn @@ -3,8 +3,8 @@ [ext_resource type="Script" path="res://Scripts/Inventory.gd" id="1_hpfk4"] [ext_resource type="Texture2D" uid="uid://crurudbckpe4j" path="res://Assets/Art/Textures/inventory.png" id="1_qivim"] [ext_resource type="PackedScene" uid="uid://cccy3p17tx25c" path="res://Scenes/ui_item_stack.tscn" id="2_mmi5o"] +[ext_resource type="Script" path="res://Scripts/Crafting.gd" id="5_k523s"] [ext_resource type="PackedScene" uid="uid://d3jmireffqxgp" path="res://Scenes/ui_recipe.tscn" id="5_lxt81"] -[ext_resource type="Script" path="res://Scenes/Crafting.gd" id="5_osfj6"] [ext_resource type="Texture2D" uid="uid://b438nicufrs6d" path="res://Assets/Art/Textures/hotbar.png" id="5_rj0ue"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_anabp"] @@ -301,7 +301,7 @@ offset_top = 738.0 offset_right = -419.0 grow_horizontal = 2 grow_vertical = 2 -script = ExtResource("5_osfj6") +script = ExtResource("5_k523s") [node name="ScrollContainer" type="ScrollContainer" parent="Menu/Crafting"] layout_mode = 1 @@ -311,10 +311,11 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 -[node name="HBoxContainer" type="HBoxContainer" parent="Menu/Crafting/ScrollContainer"] +[node name="RecipeContainer" type="HBoxContainer" parent="Menu/Crafting/ScrollContainer"] +unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="UiRecipe" parent="Menu/Crafting/ScrollContainer/HBoxContainer" instance=ExtResource("5_lxt81")] +[node name="UiRecipe" parent="Menu/Crafting/ScrollContainer/RecipeContainer" instance=ExtResource("5_lxt81")] layout_mode = 2 diff --git a/Scenes/ui_recipe.tscn b/Scenes/ui_recipe.tscn index 43fa054..b0969c9 100644 --- a/Scenes/ui_recipe.tscn +++ b/Scenes/ui_recipe.tscn @@ -1,25 +1,15 @@ -[gd_scene load_steps=3 format=3 uid="uid://d3jmireffqxgp"] +[gd_scene load_steps=2 format=3 uid="uid://d3jmireffqxgp"] [ext_resource type="PackedScene" uid="uid://cccy3p17tx25c" path="res://Scenes/ui_item_stack.tscn" id="1_kr5gx"] -[ext_resource type="PackedScene" uid="uid://du08juske0mo1" path="res://Scenes/ui_ingredient_stack.tscn" id="2_kyq4o"] [node name="UiRecipe" type="VBoxContainer"] -[node name="hallo" parent="." instance=ExtResource("1_kr5gx")] +[node name="UiResult" parent="." instance=ExtResource("1_kr5gx")] custom_minimum_size = Vector2(100, 100) layout_mode = 2 size_flags_vertical = 1 -[node name="HFlowContainer" type="HFlowContainer" parent="."] +[node name="IngredientContainer" type="HFlowContainer" parent="."] layout_mode = 2 size_flags_vertical = 3 alignment = 1 - -[node name="hallo" parent="HFlowContainer" instance=ExtResource("2_kyq4o")] -layout_mode = 2 - -[node name="hallo3" parent="HFlowContainer" instance=ExtResource("2_kyq4o")] -layout_mode = 2 - -[node name="hallo2" parent="HFlowContainer" instance=ExtResource("2_kyq4o")] -layout_mode = 2 diff --git a/Scripts/Crafting.gd b/Scripts/Crafting.gd new file mode 100644 index 0000000..9af50a8 --- /dev/null +++ b/Scripts/Crafting.gd @@ -0,0 +1,46 @@ +extends Control + +@onready var inventory: Inventory = $"../.." +@onready var recipe_container: Control = %RecipeContainer + +var ui_recipe_res = preload("res://Scenes/ui_recipe.tscn") +var ui_ingredient_res = preload("res://Scenes/ui_ingredient_stack.tscn") + +func _ready() -> void: + inventory.inv_updated.connect(_inv_updated) + +func _inv_updated() -> void: + for child in recipe_container.get_children(): + child.queue_free() + + for recipe: Dictionary in Recipes.recipes: + if _has_items(recipe): + var ui_recipe: Control = ui_recipe_res.instantiate() + var ui_result = ui_recipe.get_node("UiResult") + inventory._update_ui_item(ui_result, recipe.result) + ui_result.get_node("Button").pressed.connect(_recipe_pressed.bind(recipe)) + + var ingredient_container = ui_recipe.get_node("IngredientContainer") + + for ingredient: ItemStack in recipe.ingredients: + var ui_ingredient: Control = ui_ingredient_res.instantiate() + inventory._update_ui_item(ui_ingredient, ingredient) + ingredient_container.add_child(ui_ingredient) + + add_child(ui_recipe) + +func _recipe_pressed(recipe: Dictionary) -> void: + if inventory.cursor_item != null: + return + + inventory.cursor_item = ItemStack.new(recipe.result.type, recipe.result.amount) + inventory._update_ui_item(inventory.ui_cursor_item, inventory.cursor_item) + for ingredient: ItemStack in recipe.ingredients: + inventory.remove_items(ingredient.type, ingredient.amount) + +func _has_items(recipe: Dictionary) -> bool: + for ingredient: ItemStack in recipe.ingredients: + if inventory.count_items(ingredient.type, ingredient.amount) < ingredient.amount: + return false + + return true diff --git a/Scripts/Inventory.gd b/Scripts/Inventory.gd index ee41c28..ecfa8b8 100644 --- a/Scripts/Inventory.gd +++ b/Scripts/Inventory.gd @@ -1,6 +1,8 @@ class_name Inventory extends Control +signal inv_updated + @onready var grid_container: GridContainer = %InventoryGrid @onready var grid_container_2: GridContainer = %InventoryHotbarGrid @onready var ui_cursor_item: MarginContainer = %CursorItem @@ -87,11 +89,13 @@ func _clicked_item(index: int) -> void: items[index] = null add_item(item) _update_ui_slot(index) + inv_updated.emit() return if add_item(item, 27): items[index] = null _update_ui_slot(index) + inv_updated.emit() return @@ -104,9 +108,11 @@ func _clicked_item(index: int) -> void: _update_ui_slot(index) _update_ui_item(ui_cursor_item, cursor_item) + inv_updated.emit() func _right_clicked_item(index: int) -> void: _try_split_stack(index) + inv_updated.emit() func _update_ui_slot(index: int) -> void: _update_ui_item(_get_ui_item(index), items[index]) @@ -116,17 +122,17 @@ func _update_ui_slot(index: int) -> void: _update_ui_item(hotbar_item, items[index]) func _update_ui_item(ui_item: Node, item: ItemStack) -> void: - var item_label: Label = ui_item.get_node("ItemName") + #var item_label: Label = ui_item.get_node("ItemName") var item_count: Label = ui_item.get_node("ItemCount") var texture_rect: TextureRect = ui_item.get_node("TextureRect") if item == null: - item_label.text = "" + #item_label.text = "" item_count.text = "" texture_rect.visible = false return - item_label.text = ItemStack.type_names[item.type] + #item_label.text = ItemStack.type_names[item.type] item_count.text = str(item.amount) texture_rect.visible = true texture_rect.texture = ItemStack.type_icons[item.type] @@ -151,6 +157,7 @@ func add_item(item: ItemStack, start_index: int = 0) -> bool: _update_ui_slot(slot) + inv_updated.emit() return true func _find_first_slot(type: ItemStack.ItemType, start_index: int = 0) -> int: @@ -191,3 +198,30 @@ func _swap_with_cursor(index: int) -> void: var temp = cursor_item cursor_item = items[index] items[index] = temp + +func count_items(type: ItemStack.ItemType, max_count: int) -> int: + var count: int = 0 + for item in items: + if item != null && item.type == type: + count += item.amount + if count >= max_count: + return count + return count + +func remove_items(type: ItemStack.ItemType, amount: int) -> void: + for i in range(inv_size): + if items[i] != null && items[i].type == type: + if items[i].amount < amount: + amount -= items[i].amount + items[i] = null + _update_ui_slot(i) + continue + + items[i].amount -= amount + if items[i].amount == 0: + items[i] = null + _update_ui_slot(i) + inv_updated.emit() + return + printerr("failed to remove items. how the fuck did you get here?") + inv_updated.emit() diff --git a/Scripts/Recipes.gd b/Scripts/Recipes.gd new file mode 100644 index 0000000..5da4907 --- /dev/null +++ b/Scripts/Recipes.gd @@ -0,0 +1,12 @@ +class_name Recipes +extends RefCounted + +static var recipes: Array = [ + {"result": ItemStack.new(ItemStack.ItemType.Pickaxe), "ingredients": [ + ItemStack.new(ItemStack.ItemType.Wood, 5), + ItemStack.new(ItemStack.ItemType.Rock, 3) + ]}, + {"result": ItemStack.new(ItemStack.ItemType.Rock), "ingredients": [ + ItemStack.new(ItemStack.ItemType.Wood, 1), + ]}, +]