diff --git a/scenes/main.tscn b/scenes/main.tscn index b9a208b..3b43b3e 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -1,14 +1,11 @@ -[gd_scene load_steps=4 format=3 uid="uid://c1ibyecb1dmow"] +[gd_scene load_steps=3 format=3 uid="uid://c1ibyecb1dmow"] -[ext_resource type="PackedScene" uid="uid://cc2n5bb6al7qg" path="res://scenes/game.tscn" id="1_0xm2m"] [ext_resource type="Script" uid="uid://c8qyieqgdrge2" path="res://scripts/main.gd" id="1_h2yge"] [ext_resource type="PackedScene" uid="uid://dax0hhxr4xyjj" path="res://ui/menu.tscn" id="2_h2yge"] [node name="Main" type="Node2D"] script = ExtResource("1_h2yge") -[node name="GameScene" parent="." instance=ExtResource("1_0xm2m")] - [node name="GameMenu" parent="." instance=ExtResource("2_h2yge")] anchors_preset = 0 anchor_right = 0.0 diff --git a/scripts/gamemanager.gd b/scripts/gamemanager.gd index c520ac5..fc76029 100644 --- a/scripts/gamemanager.gd +++ b/scripts/gamemanager.gd @@ -12,4 +12,12 @@ class Score extends Resource: self.score = score_ self.initials = initials_ -@export var high_scores: Array[Score] = [] \ No newline at end of file +@export var high_scores: Array[Score] = [] + +func reset_game() -> void: + # Reset game state - placeholder for actual game logic + pass + +func restart_level() -> void: + # Restart current level - placeholder for actual game logic + pass \ No newline at end of file diff --git a/scripts/main.gd b/scripts/main.gd index d141f5d..582a44c 100644 --- a/scripts/main.gd +++ b/scripts/main.gd @@ -1,9 +1,12 @@ extends Node2D @onready var game_menu := $GameMenu -@onready var game_scene := $GameScene @onready var music_player := $MusicAudioStreamPlayer +# Dynamic game scene management +var game_scene: Node2D = null +var game_scene_packed: PackedScene = preload("res://scenes/game.tscn") + func _ready() -> void: GameManager.game_over.connect(_on_game_manager_game_over) @@ -13,7 +16,8 @@ func _on_game_manager_game_over(player_won: bool) -> void: get_tree().paused = true # Load the game over screen game_menu.show() - game_scene.hide() + if game_scene: + game_scene.hide() func _input(event: InputEvent) -> void: if event.is_action_pressed("ui_cancel"): @@ -32,10 +36,21 @@ func is_game_paused() -> bool: func pause_game() -> void: - game_menu.pause() + # Only pause if there's an active game scene + if game_scene: + game_menu.pause() func resume_game() -> void: - game_menu.unpause() + # Only unpause if there's an active game scene + if game_scene: + game_menu.unpause() + + +func ensure_game_scene_loaded() -> void: + # Ensure the game scene is loaded and added to the scene tree + if not game_scene: + game_scene = game_scene_packed.instantiate() + add_child(game_scene) func _on_game_menu_exit_game() -> void: @@ -47,7 +62,11 @@ func _on_game_menu_return_main_menu() -> void: # Return to the main menu get_tree().paused = true game_menu.show() - game_scene.hide() + + # Unload the game scene completely + if game_scene: + game_scene.queue_free() + game_scene = null func _on_game_menu_start_game() -> void: @@ -56,7 +75,9 @@ func _on_game_menu_start_game() -> void: # Hide the game menu game_menu.hide() - # Start the game scene + # Load and instantiate the game scene + ensure_game_scene_loaded() + # Show the game scene game_scene.show() # start the music @@ -67,5 +88,10 @@ func _on_game_menu_start_game() -> void: func _on_game_menu_restart_game() -> void: print("Restarting game") + + # Ensure game scene is loaded before restarting + ensure_game_scene_loaded() + game_scene.show() + GameManager.restart_level() resume_game()