Skip to content

Commit 980be53

Browse files
committed
add pause menu
1 parent b324eae commit 980be53

9 files changed

+175
-11
lines changed

KirbVania/Levels/main.tscn

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
[gd_scene load_steps=12 format=3 uid="uid://bxpyucm48u158"]
1+
[gd_scene load_steps=13 format=3 uid="uid://bxpyucm48u158"]
22

33
[ext_resource type="PackedScene" uid="uid://ewtdtlls7g4l" path="res://Prefabs/hud.tscn" id="1_45dd7"]
44
[ext_resource type="Script" path="res://Scripts/GameManager.cs" id="1_mavl2"]
55
[ext_resource type="Texture2D" uid="uid://c6vahw4halja1" path="res://Assets/Castlevania_Level1.png" id="1_y2iyb"]
66
[ext_resource type="Texture2D" uid="uid://b7laf5rrynmj7" path="res://Assets/Castlevania_Tileset.png" id="2_8j0dx"]
77
[ext_resource type="PackedScene" uid="uid://br7qwturgnoix" path="res://Prefabs/player.tscn" id="3_hev6s"]
8+
[ext_resource type="PackedScene" uid="uid://d1000cpwd36l0" path="res://Prefabs/pause_menu.tscn" id="3_ipq38"]
89
[ext_resource type="PackedScene" uid="uid://o55thht06h1l" path="res://Prefabs/light.tscn" id="5_fb8rp"]
910
[ext_resource type="PackedScene" uid="uid://csav025q8eiw1" path="res://Prefabs/boundary.tscn" id="8_opuld"]
1011
[ext_resource type="PackedScene" uid="uid://b2wo84efmnnq" path="res://Prefabs/enemy_spawner.tscn" id="9_ofw52"]
@@ -281,15 +282,19 @@ physics_layer_0/collision_layer = 1
281282
physics_layer_0/collision_mask = 62
282283
sources/1 = SubResource("TileSetAtlasSource_r0vr1")
283284

284-
[node name="Main" type="Node2D" node_paths=PackedStringArray("_player", "_hud")]
285+
[node name="Main" type="Node2D" node_paths=PackedStringArray("_player", "_hud", "_pauseMenu")]
285286
script = ExtResource("1_mavl2")
286287
_player = NodePath("Player")
287288
_hud = NodePath("UI/HUD")
289+
_pauseMenu = NodePath("UI/PauseMenu")
288290

289291
[node name="UI" type="CanvasLayer" parent="."]
290292

291293
[node name="HUD" parent="UI" instance=ExtResource("1_45dd7")]
292294

295+
[node name="PauseMenu" parent="UI" instance=ExtResource("3_ipq38")]
296+
visible = false
297+
293298
[node name="CastlevaniaLevel1" type="Sprite2D" parent="."]
294299
visible = false
295300
modulate = Color(1, 0.701961, 0.701961, 0.501961)

KirbVania/Prefabs/main_menu.tscn

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ theme_override_constants/separation = 16
3636
layout_mode = 2
3737
text = "KirbVania"
3838
label_settings = SubResource("LabelSettings_d8sct")
39+
horizontal_alignment = 1
40+
vertical_alignment = 1
3941

4042
[node name="PlayBtn" type="Button" parent="MarginContainer/VBoxContainer"]
4143
unique_name_in_owner = true

KirbVania/Prefabs/options_menu.tscn

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ theme_override_constants/separation = 16
3636
layout_mode = 2
3737
text = "Options"
3838
label_settings = SubResource("LabelSettings_d8sct")
39+
horizontal_alignment = 1
40+
vertical_alignment = 1
3941

4042
[node name="BackButton" type="Button" parent="MarginContainer/VBoxContainer"]
4143
unique_name_in_owner = true

KirbVania/Prefabs/pause_menu.tscn

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://d1000cpwd36l0"]
2+
3+
[ext_resource type="Script" path="res://Scripts/PauseMenu.cs" id="1_607jt"]
4+
5+
[sub_resource type="LabelSettings" id="LabelSettings_d8sct"]
6+
font_size = 36
7+
8+
[node name="PauseMenu" type="Control"]
9+
process_mode = 2
10+
layout_mode = 3
11+
anchors_preset = 15
12+
anchor_right = 1.0
13+
anchor_bottom = 1.0
14+
grow_horizontal = 2
15+
grow_vertical = 2
16+
script = ExtResource("1_607jt")
17+
18+
[node name="MarginContainer" type="MarginContainer" parent="."]
19+
layout_mode = 1
20+
anchors_preset = 8
21+
anchor_left = 0.5
22+
anchor_top = 0.5
23+
anchor_right = 0.5
24+
anchor_bottom = 0.5
25+
offset_left = -68.5
26+
offset_top = -48.5
27+
offset_right = 68.5
28+
offset_bottom = 48.5
29+
grow_horizontal = 2
30+
grow_vertical = 2
31+
32+
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
33+
layout_mode = 2
34+
theme_override_constants/separation = 16
35+
36+
[node name="Title" type="Label" parent="MarginContainer/VBoxContainer"]
37+
layout_mode = 2
38+
text = "Pause"
39+
label_settings = SubResource("LabelSettings_d8sct")
40+
horizontal_alignment = 1
41+
vertical_alignment = 1
42+
43+
[node name="ResumeBtn" type="Button" parent="MarginContainer/VBoxContainer"]
44+
layout_mode = 2
45+
focus_neighbor_bottom = NodePath("../OptionsBtn")
46+
text = "Resume"
47+
48+
[node name="OptionsBtn" type="Button" parent="MarginContainer/VBoxContainer"]
49+
layout_mode = 2
50+
focus_neighbor_top = NodePath("../ResumeBtn")
51+
focus_neighbor_bottom = NodePath("../ExitBtn")
52+
text = "Options"
53+
54+
[node name="ExitBtn" type="Button" parent="MarginContainer/VBoxContainer"]
55+
layout_mode = 2
56+
focus_neighbor_top = NodePath("../OptionsBtn")
57+
text = "Exit"

KirbVania/Scripts/GameManager.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using Godot;
22
using System;
3+
using KirbVania.Scripts;
34

45
public partial class GameManager : Node2D
56
{
67
[Export] private Player _player { get; set; }
78
[Export] private HUD _hud { get; set; }
9+
[Export] private PauseMenu _pauseMenu { get; set; }
10+
811
private int _score = 0;
12+
private bool _isPaused = false;
913
private AudioStreamPlayer2D _levelMusic;
1014

1115
public override void _Ready()
@@ -16,6 +20,14 @@ public override void _Ready()
1620
InitializeUI();
1721
}
1822

23+
public override void _Input(InputEvent @event)
24+
{
25+
if (@event.IsActionPressed(InputActions.TogglePause))
26+
{
27+
PauseGame();
28+
}
29+
}
30+
1931
private void OnPlayerScoreIncrease(int amount)
2032
{
2133
_score += amount;
@@ -37,4 +49,21 @@ private void InitializeUI()
3749
_hud.SetScore(0);
3850
_hud.SetHealth(6);
3951
}
52+
53+
private void PauseGame()
54+
{
55+
if (_isPaused) return;
56+
_isPaused = true;
57+
_pauseMenu.Show();
58+
_levelMusic.StreamPaused = true;
59+
GetTree().Paused = true;
60+
}
61+
62+
public void UnpauseGame()
63+
{
64+
if (!_isPaused) return;
65+
_isPaused = false;
66+
_levelMusic.StreamPaused = false;
67+
GetTree().Paused = false;
68+
}
4069
}

KirbVania/Scripts/InputActions.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace KirbVania.Scripts;
2+
3+
/// <summary>
4+
/// Constants for input actions.
5+
/// Be sure to update the Input Map in Project Settings
6+
/// such that the actions match the constants below.
7+
/// </summary>
8+
public static class InputActions
9+
{
10+
public static string MoveLeft => "move_left";
11+
public static string MoveRight => "move_right";
12+
public static string Jump => "jump";
13+
public static string Attack => "attack";
14+
public static string TogglePause => "toggle_pause";
15+
}

KirbVania/Scripts/PauseMenu.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Godot;
2+
using System;
3+
4+
public partial class PauseMenu : Control
5+
{
6+
private Button _resumeButton;
7+
private Button _optionsButton;
8+
private Button _exitButton;
9+
private GameManager _gameManager;
10+
11+
public override void _Ready()
12+
{
13+
_resumeButton = GetNode<Button>("MarginContainer/VBoxContainer/ResumeBtn");
14+
_resumeButton.Pressed += OnResumePressed;
15+
_resumeButton.GrabFocus();
16+
_optionsButton = GetNode<Button>("MarginContainer/VBoxContainer/OptionsBtn");
17+
_optionsButton.Pressed += OnOptionsPressed;
18+
_exitButton = GetNode<Button>("MarginContainer/VBoxContainer/ExitBtn");
19+
_exitButton.Pressed += OnExitPressed;
20+
_gameManager = GetNode<GameManager>("/root/Main");
21+
VisibilityChanged += OnVisibilityChanged;
22+
}
23+
24+
private void OnVisibilityChanged()
25+
{
26+
if (Visible)
27+
{
28+
_resumeButton.GrabFocus();
29+
}
30+
else
31+
{
32+
_resumeButton.ReleaseFocus();
33+
}
34+
}
35+
36+
private void OnResumePressed()
37+
{
38+
// Unpause the game
39+
_gameManager.UnpauseGame();
40+
}
41+
42+
private void OnOptionsPressed()
43+
{
44+
// Go to options menu
45+
// GetTree().ChangeSceneToFile("res://Prefabs/options_menu.tscn");
46+
GD.Print("TODO: Implement options menu that can come back to this thing");
47+
}
48+
49+
private void OnExitPressed()
50+
{
51+
// Exit to main menu
52+
GetTree().Paused = false;
53+
GetTree().ChangeSceneToFile("res://Prefabs/main_menu.tscn");
54+
}
55+
}

KirbVania/Scripts/Player.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ public override void _Process(double delta)
101101

102102
public override void _PhysicsProcess(double delta)
103103
{
104-
if (Input.IsActionJustPressed("quit"))
105-
GetTree().Quit();
106104
HandleMovement((float)delta);
107105
}
108106

109107
public override void _Input(InputEvent @event)
110108
{
111-
if (@event.IsActionPressed("attack"))
109+
if (@event.IsActionPressed(InputActions.Attack))
112110
{
113111
_isAttacking = true;
114112
TryAttack();
@@ -156,7 +154,7 @@ public void Heal(int amount)
156154
private void Die()
157155
{
158156
// TODO: Display game over screen
159-
GetTree().ReloadCurrentScene();
157+
GetTree().ChangeSceneToFile("res://Prefabs/main_menu.tscn");
160158
}
161159

162160
#endregion
@@ -205,7 +203,7 @@ private void HandleMovement(float delta)
205203
}
206204

207205
// Handle Jump.
208-
if (Input.IsActionJustPressed("jump") && IsOnFloor())
206+
if (Input.IsActionJustPressed(InputActions.Jump) && IsOnFloor())
209207
{
210208
velocity.Y = JumpVelocity;
211209
// Play jump sound
@@ -228,7 +226,7 @@ private void HandleMovement(float delta)
228226
else
229227
{
230228
// Handle horizontal movement
231-
var direction = Input.GetAxis("move_left", "move_right");
229+
var direction = Input.GetAxis(InputActions.MoveLeft, InputActions.MoveRight);
232230
velocity.X = _isAttacking && IsOnFloor() ? 0 : direction * Speed;
233231
}
234232

KirbVania/project.godot

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ attack={
6969
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":2,"pressure":0.0,"pressed":true,"script":null)
7070
]
7171
}
72-
quit={
72+
toggle_pause={
7373
"deadzone": 0.5,
74-
"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":81,"key_label":0,"unicode":113,"echo":false,"script":null)
74+
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":true,"script":null)
75+
, 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":4194306,"key_label":0,"unicode":0,"echo":false,"script":null)
7576
, 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)
76-
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":4,"pressure":0.0,"pressed":true,"script":null)
77+
, 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":80,"key_label":0,"unicode":112,"echo":false,"script":null)
7778
]
7879
}
7980

0 commit comments

Comments
 (0)