From b0475b16978eecef01baac9621576c3c08bab27b Mon Sep 17 00:00:00 2001 From: KingGurke Date: Mon, 2 Oct 2023 16:35:08 +0200 Subject: [PATCH] added pause menu in level and ability to load levels from main menu --- project.godot | 5 +++ src/game/levels/level.gd | 17 ++++++++ src/game/levels/level_1.tscn | 24 +++++++++- src/gui/menu/level.gd | 13 ++++++ src/gui/menu/level.tscn | 73 ++++++++++++++++++++++++++++++- src/gui/menu/main.gd | 5 ++- src/gui/menu/main.tscn | 85 +++++++++++++++++++++++------------- src/gui/menu/options.gd | 1 + src/gui/menu/options.tscn | 4 +- 9 files changed, 190 insertions(+), 37 deletions(-) create mode 100644 src/gui/menu/level.gd diff --git a/project.godot b/project.godot index f30f100..316c5fc 100644 --- a/project.godot +++ b/project.godot @@ -61,3 +61,8 @@ reverse={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +pause={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} diff --git a/src/game/levels/level.gd b/src/game/levels/level.gd index fc8bdad..ad7e092 100644 --- a/src/game/levels/level.gd +++ b/src/game/levels/level.gd @@ -21,6 +21,8 @@ extends Node2D ## reference to the line node used to draw the finish line @export var finish: Line2D +@export var menu: Control + signal distance_changed(new_distance) #variables @@ -31,6 +33,7 @@ var lap_distance = 0. var start var finish_right var finish_left +var paused = false # Called when the node enters the scene tree for the first time. func _ready(): @@ -53,6 +56,10 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): + + if Input.is_action_just_pressed("pause") && running: + pause() + if (running): # shrinking the track width -= shrink_factor * delta @@ -86,6 +93,16 @@ func _process(delta): distance_changed.emit(get_total_distance()) # print_debug("player distance driven: ", get_total_distance(), " distance since last check: ", off_diff) +func pause(): + if !paused: + Engine.time_scale = 0 + menu.show() + paused = true + else: + Engine.time_scale = 1 + menu.hide() + paused = false + func update_line(): line.set_width(width) diff --git a/src/game/levels/level_1.tscn b/src/game/levels/level_1.tscn index b5df79e..ae3c76a 100644 --- a/src/game/levels/level_1.tscn +++ b/src/game/levels/level_1.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=10 format=3 uid="uid://c77xk0mywwt5f"] +[gd_scene load_steps=11 format=3 uid="uid://c77xk0mywwt5f"] [ext_resource type="Script" path="res://src/game/levels/level.gd" id="1_s3rsn"] [ext_resource type="Texture2D" uid="uid://dio3rrptd6w7c" path="res://assets/bg2.png" id="2_57sr6"] [ext_resource type="Texture2D" uid="uid://ds0un04pocx6i" path="res://assets/track90.png" id="3_en5wc"] [ext_resource type="Texture2D" uid="uid://dmcrms0qu0mtt" path="res://assets/finish2.png" id="4_qeube"] [ext_resource type="PackedScene" uid="uid://ckau5s2tsb3oc" path="res://src/game/player.tscn" id="5_2n8nj"] +[ext_resource type="PackedScene" uid="uid://dyo5dblkfivwn" path="res://src/gui/menu/level.tscn" id="6_hs25h"] [ext_resource type="PackedScene" uid="uid://cdlbh3smpgg42" path="res://src/gui/hud/hud.tscn" id="6_r1i0q"] [ext_resource type="Texture2D" uid="uid://bq151lkkfdlf4" path="res://assets/sign.png" id="7_xmahe"] [ext_resource type="AudioStream" uid="uid://dh5jxxwim7ra8" path="res://assets/sounds/music/death_by_waveshaper.wav" id="8_qlkhp"] @@ -15,7 +16,7 @@ _data = { } point_count = 7 -[node name="level1" type="Node2D" node_paths=PackedStringArray("path", "line", "player", "finish")] +[node name="level1" type="Node2D" node_paths=PackedStringArray("path", "line", "player", "finish", "menu")] script = ExtResource("1_s3rsn") starting_width = 400.0 shrink_factor = 5.0 @@ -23,6 +24,7 @@ path = NodePath("TrackPath") line = NodePath("TrackLine") player = NodePath("player") finish = NodePath("FinishLine") +menu = NodePath("CanvasLayer/menu_box/level") [node name="TextureRect" type="TextureRect" parent="."] self_modulate = Color(0.827451, 0.521569, 0.313726, 1) @@ -63,6 +65,24 @@ brake_strength = 800.0 [node name="hud" parent="CanvasLayer" instance=ExtResource("6_r1i0q")] size_flags_horizontal = 3 +[node name="menu_box" type="Control" parent="CanvasLayer"] +layout_mode = 3 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -20.0 +offset_top = -20.0 +offset_right = 20.0 +offset_bottom = 20.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="level" parent="CanvasLayer/menu_box" instance=ExtResource("6_hs25h")] +visible = false +layout_mode = 1 + [node name="Node2D" type="Node2D" parent="."] [node name="curveSign3" type="PointLight2D" parent="Node2D"] diff --git a/src/gui/menu/level.gd b/src/gui/menu/level.gd new file mode 100644 index 0000000..ca3f655 --- /dev/null +++ b/src/gui/menu/level.gd @@ -0,0 +1,13 @@ +extends Control + +func _on_continue_button_pressed(): + get_tree().current_scene.pause() + +func _on_menu_button_pressed(): + get_tree().current_scene.pause() + get_tree().change_scene_to_file("res://src/gui/menu/main.tscn") + +func _on_options_button_pressed(): + var options = load("res://src/gui/menu/options.tscn").instantiate() + get_parent().add_child((options)) + hide() diff --git a/src/gui/menu/level.tscn b/src/gui/menu/level.tscn index 6ddeeab..b51e6c8 100644 --- a/src/gui/menu/level.tscn +++ b/src/gui/menu/level.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=2 format=3 uid="uid://dyo5dblkfivwn"] +[gd_scene load_steps=3 format=3 uid="uid://dyo5dblkfivwn"] [ext_resource type="Theme" uid="uid://dpktnl3r5wul1" path="res://src/gui/menu_theme.tres" id="1_62uke"] +[ext_resource type="Script" path="res://src/gui/menu/level.gd" id="2_6ldjx"] [node name="level" type="Control"] layout_mode = 3 @@ -10,3 +11,73 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 theme = ExtResource("1_62uke") +script = ExtResource("2_6ldjx") + +[node name="Pause" type="Panel" parent="."] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -131.0 +offset_top = -231.0 +offset_right = 132.0 +offset_bottom = 169.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="Label" type="Label" parent="Pause"] +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -52.0 +offset_top = 24.0 +offset_right = 52.0 +offset_bottom = 50.0 +grow_horizontal = 2 +text = "PAUSED" +horizontal_alignment = 1 + +[node name="VBoxContainer" type="VBoxContainer" parent="Pause"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -59.5 +offset_top = 3.0 +offset_right = 60.5 +offset_bottom = 131.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="ContinueButton" type="Button" parent="Pause/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) +layout_mode = 2 +size_flags_horizontal = 4 +size_flags_vertical = 8 +focus_neighbor_bottom = NodePath("../StartButton") +text = "CONTINUE" + +[node name="OptionsButton" type="Button" parent="Pause/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) +layout_mode = 2 +size_flags_horizontal = 4 +size_flags_vertical = 8 +focus_neighbor_bottom = NodePath("../StartButton") +text = "OPTIONS" + +[node name="MenuButton" type="Button" parent="Pause/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) +layout_mode = 2 +size_flags_horizontal = 4 +size_flags_vertical = 8 +focus_neighbor_bottom = NodePath("../StartButton") +text = "TO MENU" + +[connection signal="pressed" from="Pause/VBoxContainer/ContinueButton" to="." method="_on_continue_button_pressed"] +[connection signal="pressed" from="Pause/VBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"] +[connection signal="pressed" from="Pause/VBoxContainer/MenuButton" to="." method="_on_menu_button_pressed"] diff --git a/src/gui/menu/main.gd b/src/gui/menu/main.gd index 15a5ec1..f5bdd96 100644 --- a/src/gui/menu/main.gd +++ b/src/gui/menu/main.gd @@ -6,7 +6,7 @@ extends Control # Called when the node enters the scene tree for the first time. func _ready(): - $VBoxContainer/StartButton.grab_focus() + $Main/VBoxContainer/StartButton.grab_focus() # Called every frame. 'delta' is the elapsed time since the previous frame. @@ -22,7 +22,8 @@ func _on_start_button_pressed(): func _on_options_button_pressed(): var options = load("res://src/gui/menu/options.tscn").instantiate() - get_tree().current_scene.add_child((options)) + get_parent().add_child((options)) + hide() func _on_quit_button_pressed(): get_tree().quit() diff --git a/src/gui/menu/main.tscn b/src/gui/menu/main.tscn index 5c7e38b..6745e4a 100644 --- a/src/gui/menu/main.tscn +++ b/src/gui/menu/main.tscn @@ -65,39 +65,57 @@ grow_horizontal = 2 grow_vertical = 2 theme = SubResource("Theme_67fiq") script = ExtResource("1_p5r7q") -main_menu = NodePath("VBoxContainer") +main_menu = NodePath("Main") level_select = NodePath("LevelSelect") -[node name="VBoxContainer" type="VBoxContainer" parent="."] +[node name="Main" type="Panel" parent="."] layout_mode = 1 anchors_preset = 8 anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 -offset_left = -200.0 -offset_top = -44.0 -offset_right = 200.0 -offset_bottom = 156.0 +offset_left = -131.0 +offset_top = -231.0 +offset_right = 132.0 +offset_bottom = 169.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="Main"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -59.5 +offset_top = 3.0 +offset_right = 60.5 +offset_bottom = 131.0 grow_horizontal = 2 grow_vertical = 2 alignment = 1 -[node name="StartButton" type="Button" parent="VBoxContainer"] +[node name="StartButton" type="Button" parent="Main/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) layout_mode = 2 +size_flags_horizontal = 4 focus_neighbor_top = NodePath("../QuitButton") -text = "Start! -" +text = "PLAY" -[node name="OptionsButton" type="Button" parent="VBoxContainer"] +[node name="OptionsButton" type="Button" parent="Main/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) layout_mode = 2 -text = "Options -" +size_flags_horizontal = 4 +text = "OPTIONS" -[node name="QuitButton" type="Button" parent="VBoxContainer"] +[node name="QuitButton" type="Button" parent="Main/VBoxContainer"] +custom_minimum_size = Vector2(120, 40) layout_mode = 2 +size_flags_horizontal = 4 focus_neighbor_bottom = NodePath("../StartButton") -text = "Quit" +text = "QUIT" [node name="LevelSelect" type="Panel" parent="."] visible = false @@ -212,18 +230,6 @@ layout_mode = 2 size_flags_horizontal = 4 text = "12" -[node name="MarginContainer" type="MarginContainer" parent="LevelSelect/VBoxContainer"] -layout_mode = 2 -theme_override_constants/margin_top = 20 - -[node name="BackButton" type="Button" parent="LevelSelect/VBoxContainer/MarginContainer"] -custom_minimum_size = Vector2(120, 40) -layout_mode = 2 -size_flags_horizontal = 4 -size_flags_vertical = 8 -focus_neighbor_bottom = NodePath("../StartButton") -text = "Back" - [node name="Label" type="Label" parent="LevelSelect"] layout_mode = 1 anchors_preset = 5 @@ -237,15 +243,34 @@ grow_horizontal = 2 text = "LEVEL SELECT" horizontal_alignment = 1 +[node name="BackButton" type="Button" parent="LevelSelect"] +custom_minimum_size = Vector2(120, 40) +layout_mode = 1 +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -60.0 +offset_top = -109.0 +offset_right = 60.0 +offset_bottom = -69.0 +grow_horizontal = 2 +grow_vertical = 0 +size_flags_horizontal = 4 +size_flags_vertical = 8 +focus_neighbor_bottom = NodePath("../StartButton") +text = "BACK" + [node name="MenuMusic" type="AudioStreamPlayer" parent="."] stream = ExtResource("2_yc480") volume_db = -29.953 autoplay = true bus = &"Music" -[connection signal="pressed" from="VBoxContainer/StartButton" to="." method="_on_start_button_pressed"] -[connection signal="pressed" from="VBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"] -[connection signal="pressed" from="VBoxContainer/QuitButton" to="." method="_on_quit_button_pressed"] +[connection signal="pressed" from="Main/VBoxContainer/StartButton" to="." method="_on_start_button_pressed"] +[connection signal="pressed" from="Main/VBoxContainer/OptionsButton" to="." method="_on_options_button_pressed"] +[connection signal="pressed" from="Main/VBoxContainer/QuitButton" to="." method="_on_quit_button_pressed"] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/1" to="." method="_on_level_button_pressed" binds= [1]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/2" to="." method="_on_level_button_pressed" binds= [2]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/3" to="." method="_on_level_button_pressed" binds= [3]] @@ -258,4 +283,4 @@ bus = &"Music" [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row3/10" to="." method="_on_level_button_pressed" binds= [10]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row3/11" to="." method="_on_level_button_pressed" binds= [11]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row3/12" to="." method="_on_level_button_pressed" binds= [12]] -[connection signal="pressed" from="LevelSelect/VBoxContainer/MarginContainer/BackButton" to="." method="_on_back_button_pressed"] +[connection signal="pressed" from="LevelSelect/BackButton" to="." method="_on_back_button_pressed"] diff --git a/src/gui/menu/options.gd b/src/gui/menu/options.gd index bf87ee8..4aa028d 100644 --- a/src/gui/menu/options.gd +++ b/src/gui/menu/options.gd @@ -1,4 +1,5 @@ extends Control func _on_back_button_pressed(): + get_parent().get_child(0).show() queue_free() diff --git a/src/gui/menu/options.tscn b/src/gui/menu/options.tscn index b22f5fe..7e16c34 100644 --- a/src/gui/menu/options.tscn +++ b/src/gui/menu/options.tscn @@ -129,9 +129,9 @@ anchor_top = 1.0 anchor_right = 0.5 anchor_bottom = 1.0 offset_left = -60.0 -offset_top = -64.0 +offset_top = -109.0 offset_right = 60.0 -offset_bottom = -24.0 +offset_bottom = -69.0 grow_horizontal = 2 grow_vertical = 0 size_flags_horizontal = 4