2024-09-08 14:00:02 +02:00
|
|
|
class_name GameUI
|
2024-07-09 22:33:38 +02:00
|
|
|
extends Control
|
|
|
|
|
2024-10-11 18:40:40 +02:00
|
|
|
signal quit_game_scene
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
@onready var tool_slots = %ToolSlots
|
2024-09-08 14:00:02 +02:00
|
|
|
@onready var tool_container:ItemGrid = %ToolContainer
|
2024-07-20 11:20:18 +02:00
|
|
|
@onready var game_menu_ui = %GameMenuUI
|
2024-09-08 14:00:02 +02:00
|
|
|
@onready var inventory_dialog = %InventoryDialog
|
2024-09-27 11:46:31 +02:00
|
|
|
@onready var build_dialog = %BuildDialog
|
2024-10-11 13:09:44 +02:00
|
|
|
@onready var debug_panel: Panel = %DebugPanel
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
var picked_up_level_items:Array[NodePath] = []
|
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
var _game_scene:Game
|
2024-09-08 14:00:02 +02:00
|
|
|
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)
|
2024-09-27 11:46:31 +02:00
|
|
|
|
|
|
|
tool_container.connect("item_selected", _on_tool_select)
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
func activate_game_scene(game_scene:Node3D) -> void:
|
2024-09-27 18:09:30 +02:00
|
|
|
_game_scene = game_scene as Game
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
if _game_scene != null:
|
2024-10-11 13:09:44 +02:00
|
|
|
debug_panel.build_system = game_scene.build_system
|
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
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()
|
2024-07-20 11:20:18 +02:00
|
|
|
|
2024-09-14 12:45:42 +02:00
|
|
|
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:
|
2024-09-27 11:46:31 +02:00
|
|
|
item_slot.selected = true
|
2024-09-14 12:45:42 +02:00
|
|
|
else:
|
2024-09-27 11:46:31 +02:00
|
|
|
item_slot.selected = false
|
2024-09-27 18:09:30 +02:00
|
|
|
|
|
|
|
if not _player.has_build_tool_active() and build_dialog.visible:
|
|
|
|
build_dialog.hide()
|
2024-09-14 12:45:42 +02:00
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
if _game_scene != null:
|
|
|
|
if _player.has_build_tool_active():
|
|
|
|
_game_scene.build_system.is_active = true
|
|
|
|
else:
|
|
|
|
_game_scene.build_system.is_active = false
|
2024-07-09 22:33:38 +02:00
|
|
|
|
|
|
|
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
|
2024-08-15 21:10:38 +02:00
|
|
|
print(message)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2024-07-20 11:20:18 +02:00
|
|
|
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.
|
2024-09-08 14:00:02 +02:00
|
|
|
if tool_slots == null or _game_scene == null:
|
2024-07-20 11:20:18 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
if %GameMenuUI.visible:
|
|
|
|
tool_slots.hide()
|
2024-09-08 14:00:02 +02:00
|
|
|
_game_scene.process_mode = Node.PROCESS_MODE_DISABLED
|
2024-07-20 11:20:18 +02:00
|
|
|
else:
|
|
|
|
tool_slots.show()
|
2024-09-08 14:00:02 +02:00
|
|
|
_game_scene.process_mode = Node.PROCESS_MODE_INHERIT
|
2024-08-15 22:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_back_to_game_button_pressed():
|
|
|
|
%GameMenuUI.hide()
|
2024-09-08 14:00:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _unhandled_input(event):
|
2024-10-11 18:40:40 +02:00
|
|
|
if not visible:
|
|
|
|
return
|
|
|
|
|
2024-09-08 14:00:02 +02:00
|
|
|
if event.is_action_released("toggle_inventory"):
|
2024-09-27 18:09:30 +02:00
|
|
|
if inventory_dialog.visible:
|
|
|
|
inventory_dialog.hide()
|
|
|
|
else:
|
|
|
|
inventory_dialog.open(_all_recipes, _player.inventory)
|
|
|
|
|
|
|
|
if _player == null or _player.is_queued_for_deletion():
|
|
|
|
return
|
2024-09-27 11:46:31 +02:00
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
if _player.has_build_tool_active() and event.is_action_released("toggle_build_menu"):
|
2024-09-27 11:46:31 +02:00
|
|
|
if build_dialog.visible:
|
|
|
|
build_dialog.hide()
|
|
|
|
else:
|
|
|
|
build_dialog.open(_all_recipes, _player.inventory)
|
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
if _game_scene.build_system.is_active and _game_scene.build_system.build_item != null:
|
|
|
|
return
|
|
|
|
elif _player.handle_tool_slot_input_events(event):
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
return
|
|
|
|
|
2024-09-27 11:46:31 +02:00
|
|
|
func _on_tool_select(item_slot:ItemSlot) -> void:
|
|
|
|
var tool_slot_index = tool_container.get_slot_index(item_slot)
|
2024-10-04 17:25:17 +02:00
|
|
|
_player.set_tool_slot_index(tool_slot_index)
|
2024-09-27 11:46:31 +02:00
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
|
|
|
|
func _on_build_items_container_item_selected(item_slot:ItemSlot) -> void:
|
|
|
|
if _game_scene:
|
|
|
|
_game_scene.build_system.build_item = item_slot.get_item()
|
|
|
|
build_dialog.hide()
|
2024-10-11 18:40:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_quit_to_main_menu_button_pressed() -> void:
|
|
|
|
quit_game_scene.emit()
|