132 lines
3.5 KiB
GDScript
132 lines
3.5 KiB
GDScript
class_name GameUI
|
|
extends Control
|
|
|
|
@onready var tool_slots = %ToolSlots
|
|
@onready var tool_container:ItemGrid = %ToolContainer
|
|
@onready var game_menu_ui = %GameMenuUI
|
|
@onready var inventory_dialog = %InventoryDialog
|
|
@onready var build_dialog = %BuildDialog
|
|
|
|
var picked_up_level_items:Array[NodePath] = []
|
|
|
|
var _game_scene:Node3D
|
|
var _player:Player
|
|
var _all_recipes:Array[Recipe] = []
|
|
|
|
func _ready():
|
|
for file in DirAccess.get_files_at("res://data/recipes"):
|
|
var resource_file = "res://data/recipes/" + file
|
|
var recipe:Recipe = load(resource_file) as Recipe
|
|
_all_recipes.append(recipe)
|
|
|
|
tool_container.connect("item_selected", _on_tool_select)
|
|
|
|
|
|
func activate_game_scene(game_scene:Node3D) -> void:
|
|
_game_scene = game_scene
|
|
|
|
if _game_scene != null:
|
|
picked_up_level_items = []
|
|
|
|
if _player != null:
|
|
disconnect_player_signals()
|
|
|
|
_player = game_scene.find_child("Player", true, false)
|
|
assert(_player != null)
|
|
|
|
update_player_signals()
|
|
|
|
tool_slots.show()
|
|
tool_container.displayStacks(_player.inventory.get_tool_item_stacks())
|
|
|
|
# When saving a game we need to know which items were picked up.
|
|
for pickup_item in get_tree().get_nodes_in_group("pickup_item"):
|
|
pickup_item.connect("item_picked_up", on_item_pickup)
|
|
|
|
game_menu_ui.hide()
|
|
|
|
func _process(_delta):
|
|
if _player == null:
|
|
# TODO: make sure game ui is deactivated when no game running
|
|
return
|
|
|
|
assert(_player != null and tool_container.get_child_count() >= _player.selected_tool_slot_index)
|
|
|
|
for i in range(tool_container.get_child_count()):
|
|
var item_slot:ItemSlot = tool_container.get_child(i) as ItemSlot
|
|
|
|
assert(item_slot != null)
|
|
|
|
if i == _player.selected_tool_slot_index:
|
|
item_slot.selected = true
|
|
else:
|
|
item_slot.selected = false
|
|
|
|
|
|
func _on_message_timer_timeout():
|
|
%MessagesContainer.visible = false
|
|
|
|
|
|
func _on_player_trigger_message(message):
|
|
%MessagesContainer/MessageTextEdit.text = message
|
|
%MessagesContainer/MessageTimer.start(1)
|
|
%MessagesContainer.visible = true
|
|
print(message)
|
|
|
|
|
|
func on_item_pickup(node_path:NodePath):
|
|
picked_up_level_items.append(node_path)
|
|
|
|
|
|
func disconnect_player_signals():
|
|
_player.disconnect("trigger_message", _on_player_trigger_message)
|
|
|
|
|
|
func update_player_signals():
|
|
_player.connect("trigger_message", _on_player_trigger_message)
|
|
|
|
|
|
func _unhandled_key_input(event:InputEvent):
|
|
var key_event:InputEventKey = event as InputEventKey
|
|
|
|
if key_event and key_event.pressed and key_event.get_keycode_with_modifiers() == KEY_ESCAPE:
|
|
if %GameMenuUI.visible:
|
|
%GameMenuUI.hide()
|
|
else:
|
|
%GameMenuUI.show()
|
|
|
|
|
|
func _on_game_menu_ui_visibility_changed():
|
|
# Function gets triggered when scene is still in construction. In that
|
|
# case just return.
|
|
if tool_slots == null or _game_scene == null:
|
|
return
|
|
|
|
if %GameMenuUI.visible:
|
|
tool_slots.hide()
|
|
_game_scene.process_mode = Node.PROCESS_MODE_DISABLED
|
|
else:
|
|
tool_slots.show()
|
|
_game_scene.process_mode = Node.PROCESS_MODE_INHERIT
|
|
|
|
|
|
func _on_back_to_game_button_pressed():
|
|
%GameMenuUI.hide()
|
|
|
|
|
|
func _unhandled_input(event):
|
|
if event.is_action_released("toggle_inventory"):
|
|
print ("Showing inventory of player " + str(_player))
|
|
inventory_dialog.open(_all_recipes, _player.inventory)
|
|
|
|
if event.is_action_released("toggle_build_menu"):
|
|
if build_dialog.visible:
|
|
build_dialog.hide()
|
|
else:
|
|
build_dialog.open(_all_recipes, _player.inventory)
|
|
|
|
func _on_tool_select(item_slot:ItemSlot) -> void:
|
|
var tool_slot_index = tool_container.get_slot_index(item_slot)
|
|
print ("Selected tool index ", tool_slot_index)
|
|
|