2024-09-27 11:46:31 +02:00
|
|
|
extends MarginContainer
|
|
|
|
class_name BuildDialog
|
|
|
|
|
|
|
|
@onready var build_items_container:ItemGrid = %BuildItemsContainer
|
|
|
|
@onready var build_item_resources_container:ItemGrid = %BuildItemResourcesContainer
|
|
|
|
|
|
|
|
var _inventory:Inventory = null
|
2024-10-18 12:01:15 +02:00
|
|
|
var _build_item_slots:Array[ItemResource] = []
|
|
|
|
var _build_item_resources:Array[ItemResource] = []
|
|
|
|
var _recipes:Array[RecipeResource] = []
|
2024-09-27 11:46:31 +02:00
|
|
|
var _recipe_for_item:Dictionary = {}
|
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
func open(recipes:Array[RecipeResource], inventory:Inventory):
|
2024-09-27 11:46:31 +02:00
|
|
|
_inventory = inventory
|
|
|
|
_recipes = recipes
|
|
|
|
|
|
|
|
show()
|
|
|
|
|
|
|
|
if build_items_container.selected_slot == null:
|
|
|
|
build_items_container.select_slot(0)
|
|
|
|
|
|
|
|
_build_item_slots.clear()
|
2024-10-18 12:01:15 +02:00
|
|
|
for recipe:RecipeResource in recipes:
|
2024-09-27 11:46:31 +02:00
|
|
|
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)
|
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
func _update_build_item_resources_container(recipe:RecipeResource) -> void:
|
2024-09-27 11:46:31 +02:00
|
|
|
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()])
|