function for toggling main menu visibility

This commit is contained in:
Antti Hakkarainen 2023-02-17 10:25:51 +02:00
parent 8b437f6598
commit 8189a16fee
3 changed files with 31 additions and 20 deletions

View file

@ -16,6 +16,7 @@ script = ExtResource("1_ysxum")
[node name="MainMenu" parent="." instance=ExtResource("2_wfpe2")] [node name="MainMenu" parent="." instance=ExtResource("2_wfpe2")]
[node name="Game" type="Node2D" parent="."] [node name="Game" type="Node2D" parent="."]
process_mode = 1
visible = false visible = false
[node name="ChunkHandler" type="Node2D" parent="Game"] [node name="ChunkHandler" type="Node2D" parent="Game"]

View file

@ -63,39 +63,49 @@ func start_new_game():
func _on_mainmenu_button_pressed(button:int): func _on_mainmenu_button_pressed(button:int):
match button: match button:
0: # new game Globals.MAINMENU_NEW_GAME:
start_new_game() start_new_game()
self.find_child("Menu_NewGame").disabled = true self.find_child("Menu_NewGame").disabled = true
self.find_child("Menu_ResumeGame").disabled = false self.find_child("Menu_ResumeGame").disabled = false
1: # load game: Globals.MAINMENU_LOAD_GAME:
pass pass
2: # resume game Globals.MAINMENU_RESUME_GAME:
# TODO save camera position before opening menu, restore camera position when closing menu # TODO save camera position before opening menu, restore camera position when closing menu
self.find_child("Game").process_mode = PROCESS_MODE_INHERIT self.find_child("Game").process_mode = PROCESS_MODE_INHERIT
self.find_child("Game").show() toggle_main_menu_visibility()
self.find_child("UILayer").show() Globals.MAINMENU_QUIT_GAME:
self.find_child("MainMenu").hide() quit_game()
_: _:
push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button) push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button)
func _unhandled_input(event) -> void: func _unhandled_input(event) -> void:
if event.is_action_pressed("open_main_menu"): if event.is_action_pressed("open_main_menu"):
emit_signal( # move mainmenu to current game camera position
"set_camera_position", var mainmenu_pos = Globals.CAMERA_POSITION
Vector2( mainmenu_pos.x -= DisplayServer.window_get_size(0).x/2
DisplayServer.window_get_size(0).x/2, mainmenu_pos.y -= DisplayServer.window_get_size(0).y/2
DisplayServer.window_get_size(0).y/2 self.find_child("MainMenu").position = mainmenu_pos
)
) # show the menu
self.find_child("Game").hide() toggle_main_menu_visibility()
self.find_child("UILayer").hide() #await get_tree().create_timer(0.2).timeout
self.find_child("MainMenu").show()
await get_tree().create_timer(0.2).timeout
self.find_child("Game").process_mode = PROCESS_MODE_DISABLED self.find_child("Game").process_mode = PROCESS_MODE_DISABLED
func toggle_main_menu_visibility():
var items = [find_child("Game"), find_child("UILayer"), find_child("MainMenu")]
for item in items:
if !item:
push_error("Error: While toggling main menu visibility. Missing UI element " + item)
if item.visible:
item.hide()
continue
item.show()
func quit_game(): func quit_game():
get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST) get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
get_tree().quit()

View file

@ -29,5 +29,5 @@ func _on_menu_credits_pressed():
func _on_menu_exit_game_pressed(): func _on_menu_exit_game_pressed():
#emit_signal("button_pressed", Globals.MAINMENU_QUIT_GAME) emit_signal("button_pressed", Globals.MAINMENU_QUIT_GAME)
get_tree().quit()