TinyAdventure/ui/inventory_dialog.gd

69 lines
1.8 KiB
GDScript
Raw Normal View History

2024-07-09 22:33:38 +02:00
class_name InventoryDialog
extends MarginContainer
# Crafting
@onready var recipe_list:ItemList = %RecipeList
@onready var ingredients_container:ItemGrid = %IngredientsContainer
@onready var results_container:ItemGrid = %ResultsContainer
@onready var craft_button = %CraftButton
# Backpack
@onready var inventory_container:ItemGrid = %InventoryContainer
var _inventory:Inventory = null
var _selected_recipe:RecipeResource
2024-07-09 22:33:38 +02:00
func open(recipes:Array[RecipeResource], inventory:Inventory):
2024-07-09 22:33:38 +02:00
_inventory = inventory
show()
recipe_list.clear()
for recipe in recipes:
var index = recipe_list.add_item(recipe.name)
recipe_list.set_item_metadata(index, recipe)
inventory_container.displayStacks(inventory.get_item_stacks())
2024-07-09 22:33:38 +02:00
recipe_list.select(0)
_on_recipe_list_item_selected(0)
func _on_close_button_pressed():
hide()
func _unhandled_key_input(event:InputEvent):
var key_event:InputEventKey = event as InputEventKey
if visible and key_event.pressed and key_event.get_keycode_with_modifiers() == KEY_ESCAPE:
get_viewport().set_input_as_handled()
hide()
func update_crafting_widgets(recipe:RecipeResource):
2024-07-09 22:33:38 +02:00
ingredients_container.display(recipe.ingredients)
results_container.display(recipe.results)
craft_button.disabled = not _inventory.has_all(recipe.ingredients)
func _on_recipe_list_item_selected(index):
var recipe:RecipeResource = recipe_list.get_item_metadata(index)
2024-07-09 22:33:38 +02:00
_selected_recipe = recipe
print("Selected recipe is: " + str(recipe) + " with index " + str(index))
update_crafting_widgets(_selected_recipe)
func _on_craft_button_pressed():
for item in _selected_recipe.ingredients:
_inventory.remove_item(item)
for item in _selected_recipe.results:
_inventory.add_item(item)
inventory_container.displayStacks(_inventory.get_item_stacks())
2024-07-09 22:33:38 +02:00
update_crafting_widgets(_selected_recipe)