TinyAdventure/ui/build_dialog.gd

55 lines
1.7 KiB
GDScript

extends MarginContainer
class_name BuildDialog
@onready var build_items_container:ItemGrid = %BuildItemsContainer
@onready var build_item_resources_container:ItemGrid = %BuildItemResourcesContainer
var _inventory:Inventory = null
var _build_item_slots:Array[Item] = []
var _build_item_resources:Array[Item] = []
var _recipes:Array[Recipe] = []
var _recipe_for_item:Dictionary = {}
func open(recipes:Array[Recipe], inventory:Inventory):
_inventory = inventory
_recipes = recipes
show()
if build_items_container.selected_slot == null:
build_items_container.select_slot(0)
_build_item_slots.clear()
for recipe:Recipe in recipes:
if not recipe.results[0].is_buildable:
continue
if recipe.results.size() > 1:
push_warning("Invalid recipe '%s': results must only contain single item!", recipe.name)
continue
_build_item_slots.append(recipe.results[0])
_recipe_for_item[recipe.results[0]] = recipe
build_items_container.display(_build_item_slots)
func _update_build_item_resources_container(recipe:Recipe) -> void:
build_item_resources_container.display(recipe.ingredients)
func _unhandled_key_input(event:InputEvent) -> void:
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 _on_build_items_container_item_selected(item_slot:ItemSlot) -> void:
_build_item_resources.clear()
build_item_resources_container.clear_slots()
if item_slot.get_item() == null or _recipe_for_item.has(item_slot.get_item()) == null:
return
_update_build_item_resources_container(_recipe_for_item[item_slot.get_item()])