From aa8b56a72c7e95e317596c399449054e847a5b5f Mon Sep 17 00:00:00 2001 From: Sascha Date: Wed, 24 Jan 2024 12:34:10 +0100 Subject: [PATCH] Pause --- Godot-space Shooter.csproj | 4 ++ Godot-space Shooter.csproj.old.5 | 1 + Godot-space Shooter.sln | 14 ++--- Scenes/SpaceShooter.tscn | 99 +++++++++++++++++++++++--------- Scripts/Asteroid.cs | 2 +- Scripts/Background.cs | 2 +- Scripts/Explosion.cs | 2 + Scripts/GameArea.cs | 2 + Scripts/GameManager.cs | 19 ++++-- Scripts/InputManager.cs | 26 +++++++++ Scripts/LaserBeam.cs | 2 + Scripts/PlayerShip.cs | 2 +- Scripts/SoundManager.cs | 2 +- Scripts/Ui.cs | 11 ++-- project.godot | 7 +++ 15 files changed, 146 insertions(+), 49 deletions(-) create mode 100644 Scripts/InputManager.cs diff --git a/Godot-space Shooter.csproj b/Godot-space Shooter.csproj index 218871a..9d3fa14 100644 --- a/Godot-space Shooter.csproj +++ b/Godot-space Shooter.csproj @@ -2,5 +2,9 @@ net8.0 true + GodotspaceShooter + + + \ No newline at end of file diff --git a/Godot-space Shooter.csproj.old.5 b/Godot-space Shooter.csproj.old.5 index 218871a..cc1116d 100644 --- a/Godot-space Shooter.csproj.old.5 +++ b/Godot-space Shooter.csproj.old.5 @@ -2,5 +2,6 @@ net8.0 true + GodotspaceShooter \ No newline at end of file diff --git a/Godot-space Shooter.sln b/Godot-space Shooter.sln index 896aa9b..5b844cf 100644 --- a/Godot-space Shooter.sln +++ b/Godot-space Shooter.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Godot-space Shooter", "Godot-space Shooter.csproj", "{240DDE4D-DBD7-403E-B52D-F5962748425A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Godot-space Shooter", "Godot-space Shooter.csproj", "{EDAECF90-1BC3-4D10-849B-57165567D656}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -9,11 +9,11 @@ Global ExportRelease|Any CPU = ExportRelease|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {240DDE4D-DBD7-403E-B52D-F5962748425A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {240DDE4D-DBD7-403E-B52D-F5962748425A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {240DDE4D-DBD7-403E-B52D-F5962748425A}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU - {240DDE4D-DBD7-403E-B52D-F5962748425A}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU - {240DDE4D-DBD7-403E-B52D-F5962748425A}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU - {240DDE4D-DBD7-403E-B52D-F5962748425A}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {EDAECF90-1BC3-4D10-849B-57165567D656}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU EndGlobalSection EndGlobal diff --git a/Scenes/SpaceShooter.tscn b/Scenes/SpaceShooter.tscn index f32f87f..3d0b25a 100644 --- a/Scenes/SpaceShooter.tscn +++ b/Scenes/SpaceShooter.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=20 format=3 uid="uid://det8556rpxhbv"] +[gd_scene load_steps=22 format=3 uid="uid://det8556rpxhbv"] [ext_resource type="PackedScene" uid="uid://tlr55u0gn20l" path="res://Packed-Scenes/PlayerShip.tscn" id="1_njb5h"] [ext_resource type="Texture2D" uid="uid://cg6n1hh3lj7rn" path="res://Textures/tile_nebula_green_dff.png" id="2_43vix"] @@ -14,11 +14,19 @@ [ext_resource type="AudioStream" uid="uid://bspwjmgsoxkbn" path="res://Sounds/gameover2.wav" id="9_rtdps"] [ext_resource type="AudioStream" uid="uid://dlml6o3xscxqf" path="res://Sounds/weapon_player.wav" id="11_bbedd"] [ext_resource type="Script" path="res://Scripts/GameArea.cs" id="11_p0rh7"] -[ext_resource type="Texture2D" uid="uid://b8gy1u2i0jkyd" path="res://Images/restart.svg" id="15_nxptg"] +[ext_resource type="Script" path="res://Scripts/InputManager.cs" id="15_uovst"] [ext_resource type="Script" path="res://Scripts/Ui.cs" id="15_xl7yx"] [sub_resource type="BoxShape3D" id="BoxShape3D_bw288"] +[sub_resource type="LabelSettings" id="LabelSettings_dqsad"] +font_size = 50 +font_color = Color(1, 0, 0, 1) +outline_size = 3 +outline_color = Color(1, 0.999986, 0.00293482, 1) +shadow_size = 10 +shadow_offset = Vector2(5, 5) + [sub_resource type="InputEventKey" id="InputEventKey_gjrl7"] device = -1 keycode = 4194309 @@ -26,21 +34,34 @@ keycode = 4194309 [sub_resource type="Shortcut" id="Shortcut_soxs4"] events = [SubResource("InputEventKey_gjrl7")] +[sub_resource type="LabelSettings" id="LabelSettings_vf3s8"] +font_size = 50 +font_color = Color(0.233592, 0.506913, 1, 1) +outline_size = 3 +outline_color = Color(0, 1, 0.00392157, 1) +shadow_size = 10 +shadow_offset = Vector2(5, 5) + [node name="SpaceShooter" type="Node3D"] +process_mode = 3 [node name="PlayerShip" parent="." node_paths=PackedStringArray("jet", "PlayerRb", "shots") instance=ExtResource("1_njb5h")] +process_mode = 1 transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74227e-08, 0, -1, 0, 0, 5) jet = NodePath("RigidBody3D/Jet") PlayerRb = NodePath("RigidBody3D") shots = NodePath("Shots") [node name="Camera3D" type="Camera3D" parent="."] +process_mode = 1 transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 8.8, 0) [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +process_mode = 1 transform = Transform3D(1, 0, 0, 0, 0.707106, 0.707106, 0, -0.707106, 0.707106, 10, 10, 10) [node name="Background" type="Sprite3D" parent="."] +process_mode = 1 transform = Transform3D(1.1, 0, 0, 0, -4.80825e-08, -1, 0, 1.1, -4.37114e-08, 0, -0.5, 0) billboard = 1 shaded = true @@ -51,8 +72,10 @@ region_rect = Rect2(0, 0, 810, 1380) script = ExtResource("3_imgbw") [node name="Asteroids" type="Node3D" parent="."] +process_mode = 1 [node name="GameArea" type="Node3D" parent="."] +process_mode = 1 script = ExtResource("11_p0rh7") [node name="Bottom" type="RigidBody3D" parent="GameArea"] @@ -86,6 +109,7 @@ transform = Transform3D(15, 0, 0, 0, 15, 0, 0, 0, 15, 0, 0, 0) shape = SubResource("BoxShape3D_bw288") [node name="SoundManager" type="Node" parent="." node_paths=PackedStringArray("backgroundMusic", "gameOverMusic", "asteroidExplosionSound", "playerExplosionSound", "laserSound")] +process_mode = 1 script = ExtResource("8_4ksf0") backgroundMusic = NodePath("BackgroundMusic") gameOverMusic = NodePath("GameOverMusic") @@ -110,13 +134,19 @@ stream = ExtResource("9_rtdps") stream = ExtResource("11_bbedd") [node name="GameManager" type="Node" parent="." node_paths=PackedStringArray("asteroidsContainer")] +process_mode = 1 script = ExtResource("9_rsrr5") asteroids = Array[PackedScene]([ExtResource("5_do6ba"), ExtResource("6_tqoe7"), ExtResource("7_v6ul2")]) asteroidsContainer = NodePath("../Asteroids") -[node name="Ui" type="Node2D" parent="." node_paths=PackedStringArray("labelAsteroids", "labelLevel", "labelLifes", "labelNextLevel", "labelPoints", "labelGameOver", "restartButton", "reachedPointsLabel", "healthBar")] +[node name="InputManager" type="Node" parent="."] +process_mode = 3 +script = ExtResource("15_uovst") + +[node name="Ui" type="Node2D" parent="." node_paths=PackedStringArray("rtlAsteroids", "labelLevel", "labelLifes", "labelNextLevel", "labelPoints", "labelGameOver", "restartButton", "reachedPointsLabel", "healthBar", "vBoxPause")] +process_mode = 3 script = ExtResource("15_xl7yx") -labelAsteroids = NodePath("VBoxLabels/LabelAsteroids") +rtlAsteroids = NodePath("VBoxLabels/rtlAsteroids") labelLevel = NodePath("VBoxLabels/LabelLevel") labelLifes = NodePath("VBoxLabels/LabelLives") labelNextLevel = NodePath("VBoxLabels/LabelNextLevel") @@ -125,6 +155,7 @@ labelGameOver = NodePath("VBoxGameOver/LabelGameOver") restartButton = NodePath("VBoxGameOver/RestartButton") reachedPointsLabel = NodePath("VBoxGameOver/ReachedPointsLabel") healthBar = NodePath("VBoxHealth/HealthBar") +vBoxPause = NodePath("VBoxPause") [node name="VBoxLabels" type="VBoxContainer" parent="Ui"] anchors_preset = 2 @@ -135,7 +166,7 @@ offset_right = 152.0 offset_bottom = 1278.0 grow_vertical = 0 -[node name="LabelAsteroids" type="RichTextLabel" parent="Ui/VBoxLabels"] +[node name="rtlAsteroids" type="RichTextLabel" parent="Ui/VBoxLabels"] layout_mode = 2 size_flags_vertical = 3 text = "Asteroids: 0" @@ -171,31 +202,25 @@ scroll_active = false shortcut_keys_enabled = false [node name="VBoxGameOver" type="VBoxContainer" parent="Ui"] +visible = false anchors_preset = 8 anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 -offset_left = 195.5 -offset_top = 490.0 -offset_right = 603.5 -offset_bottom = 788.0 +offset_left = 197.0 +offset_top = 556.0 +offset_right = 605.0 +offset_bottom = 854.0 grow_horizontal = 2 grow_vertical = 2 -[node name="LabelGameOver" type="RichTextLabel" parent="Ui/VBoxGameOver"] +[node name="LabelGameOver" type="Label" parent="Ui/VBoxGameOver"] layout_mode = 2 -size_flags_vertical = 3 -theme_override_colors/default_color = Color(1, 0, 0, 1) -theme_override_colors/font_shadow_color = Color(0, 0, 0, 1) -theme_override_colors/font_outline_color = Color(1, 1, 1, 1) -theme_override_constants/shadow_offset_x = 1 -theme_override_constants/shadow_offset_y = -1 -theme_override_constants/shadow_outline_size = 0 -theme_override_font_sizes/normal_font_size = 20 text = "Game Over" -scroll_active = false -shortcut_keys_enabled = false +label_settings = SubResource("LabelSettings_dqsad") +horizontal_alignment = 1 +vertical_alignment = 1 [node name="ReachedPointsLabel" type="RichTextLabel" parent="Ui/VBoxGameOver"] layout_mode = 2 @@ -208,16 +233,34 @@ layout_mode = 2 size_flags_horizontal = 4 size_flags_vertical = 6 shortcut = SubResource("Shortcut_soxs4") -icon = ExtResource("15_nxptg") -icon_alignment = 1 +text = "Restart" + +[node name="VBoxPause" type="VBoxContainer" parent="Ui"] +offset_left = 195.5 +offset_top = 647.0 +offset_right = 603.5 +offset_bottom = 788.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="LabelPause" type="Label" parent="Ui/VBoxPause"] +layout_mode = 2 +text = "Pause" +label_settings = SubResource("LabelSettings_vf3s8") +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="ButtonResume" type="Button" parent="Ui/VBoxPause"] +layout_mode = 2 +size_flags_horizontal = 4 +size_flags_vertical = 6 +text = "Resume +" [node name="VBoxHealth" type="VBoxContainer" parent="Ui"] -anchors_preset = 5 -anchor_left = 0.5 -anchor_right = 0.5 -offset_left = 45.0 -offset_right = 754.0 -offset_bottom = 22.0 +offset_top = 1274.0 +offset_right = 152.0 +offset_bottom = 1278.0 grow_horizontal = 2 [node name="HealthBar" type="ProgressBar" parent="Ui/VBoxHealth"] diff --git a/Scripts/Asteroid.cs b/Scripts/Asteroid.cs index 8491e36..acdeec3 100644 --- a/Scripts/Asteroid.cs +++ b/Scripts/Asteroid.cs @@ -1,6 +1,6 @@ using Godot; -namespace Scripts; +namespace GodotspaceShooter.Scripts; [GlobalClass] public partial class Asteroid : Node3D { diff --git a/Scripts/Background.cs b/Scripts/Background.cs index f5d00c3..132bb13 100644 --- a/Scripts/Background.cs +++ b/Scripts/Background.cs @@ -1,6 +1,6 @@ using Godot; -namespace Scripts; +namespace GodotspaceShooter.Scripts; public partial class Background : Sprite3D { [Export] private float moveVelocity = -250f; diff --git a/Scripts/Explosion.cs b/Scripts/Explosion.cs index e6fe0b6..b707be4 100644 --- a/Scripts/Explosion.cs +++ b/Scripts/Explosion.cs @@ -1,5 +1,7 @@ using Godot; +namespace GodotspaceShooter.Scripts; + public partial class Explosion : GpuParticles3D { public override void _Process(double delta) { if (!Emitting) QueueFree(); diff --git a/Scripts/GameArea.cs b/Scripts/GameArea.cs index a89a95e..88aa1fa 100644 --- a/Scripts/GameArea.cs +++ b/Scripts/GameArea.cs @@ -1,3 +1,5 @@ using Godot; +namespace GodotspaceShooter.Scripts; + public partial class GameArea : Node3D { } \ No newline at end of file diff --git a/Scripts/GameManager.cs b/Scripts/GameManager.cs index 2b58d2f..165cc3d 100644 --- a/Scripts/GameManager.cs +++ b/Scripts/GameManager.cs @@ -2,7 +2,7 @@ using System; using Godot; using Godot.Collections; -namespace Scripts; +namespace GodotspaceShooter.Scripts; public partial class GameManager : Node { private int asteroidMaxNumber = 3; @@ -19,7 +19,7 @@ public partial class GameManager : Node { private int points; private float spawnRate = 1f; private float spawnTimer; - + public event EventHandler RefreshUi; public int AsteroidNumber { @@ -30,8 +30,6 @@ public partial class GameManager : Node { } } - public bool GamePaused { get; private set; } = true; - public int Lives { get => lives; set { @@ -60,7 +58,7 @@ public partial class GameManager : Node { public int NextLevelPoints { get => nextLevelPoints; - set { + private set { nextLevelPoints = value; RefreshUi?.Invoke(this, EventArgs.Empty); } @@ -68,12 +66,20 @@ public partial class GameManager : Node { public int AsteroidMaxNumber { get => asteroidMaxNumber; - set { + private set { asteroidMaxNumber = value; RefreshUi?.Invoke(this, EventArgs.Empty); } } + private bool gamePaused; + public bool GamePaused { + get => gamePaused; + set { + gamePaused = value; + RefreshUi?.Invoke(this, EventArgs.Empty); + } + } public static GameManager Instance { get; private set; } [Export] public bool Debug { get; private set; } = true; @@ -95,6 +101,7 @@ public partial class GameManager : Node { spawnTimer = spawnRate; } } + private void SpawnNewAsteroid() { if (GameOver) return; GD.Print("Spawn new asteroid"); diff --git a/Scripts/InputManager.cs b/Scripts/InputManager.cs new file mode 100644 index 0000000..259755a --- /dev/null +++ b/Scripts/InputManager.cs @@ -0,0 +1,26 @@ +using Godot; + +namespace GodotspaceShooter.Scripts; + +public partial class InputManager : Node +{ + private const string GAME_PAUSE = "Game_Pause"; + + public override void _Process(double delta) { + if (Input.IsActionJustReleased(GAME_PAUSE)) { + switch (GameManager.Instance.GamePaused) { + case true: + GameManager.Instance.GamePaused = false; + GD.Print(GameManager.Instance.GamePaused); + GetTree().Paused = GameManager.Instance.GamePaused; + break; + case false: + GameManager.Instance.GamePaused = true; + GD.Print(GameManager.Instance.GamePaused); + GetTree().Paused = GameManager.Instance.GamePaused; + break; + } + } + } + +} \ No newline at end of file diff --git a/Scripts/LaserBeam.cs b/Scripts/LaserBeam.cs index 5a50553..76ef446 100644 --- a/Scripts/LaserBeam.cs +++ b/Scripts/LaserBeam.cs @@ -1,5 +1,7 @@ using Godot; +namespace GodotspaceShooter.Scripts; + public partial class LaserBeam : Node3D { [Export] private RigidBody3D rigidBody; private float speed = 10f; diff --git a/Scripts/PlayerShip.cs b/Scripts/PlayerShip.cs index 2dd8926..5656aa8 100644 --- a/Scripts/PlayerShip.cs +++ b/Scripts/PlayerShip.cs @@ -1,6 +1,6 @@ using Godot; -namespace Scripts; +namespace GodotspaceShooter.Scripts; public partial class PlayerShip : Node3D { private const float jetTimerMax = 0.5f; diff --git a/Scripts/SoundManager.cs b/Scripts/SoundManager.cs index 2b49346..84c2889 100644 --- a/Scripts/SoundManager.cs +++ b/Scripts/SoundManager.cs @@ -2,7 +2,7 @@ using System; using System.Runtime.InteropServices; using Godot; -namespace Scripts; +namespace GodotspaceShooter.Scripts; public partial class SoundManager : Node { [Export] private AudioStreamPlayer backgroundMusic; diff --git a/Scripts/Ui.cs b/Scripts/Ui.cs index 8f35dec..2385950 100644 --- a/Scripts/Ui.cs +++ b/Scripts/Ui.cs @@ -1,18 +1,19 @@ using System; using Godot; -namespace Scripts; +namespace GodotspaceShooter.Scripts; public partial class Ui : Node2D { - [Export] private RichTextLabel labelAsteroids; + [Export] private RichTextLabel rtlAsteroids; [Export] private RichTextLabel labelLevel; [Export] private RichTextLabel labelLifes; [Export] private RichTextLabel labelNextLevel; [Export] private RichTextLabel labelPoints; - [Export] private RichTextLabel labelGameOver; + [Export] private Label labelGameOver; [Export] private Button restartButton; [Export] private RichTextLabel reachedPointsLabel; [Export] private ProgressBar healthBar; + [Export] private VBoxContainer vBoxPause; public static Ui Instance { get; private set; } @@ -32,7 +33,7 @@ public partial class Ui : Node2D private void RefreshUi(object sender, EventArgs e) { labelLifes.Text = $"Lives: {GameManager.Instance.Lives}"; - labelAsteroids.Text = $"Asteroids: {GameManager.Instance.AsteroidNumber} / {GameManager.Instance.AsteroidMaxNumber}"; + rtlAsteroids.Text = $"Asteroids: {GameManager.Instance.AsteroidNumber} / {GameManager.Instance.AsteroidMaxNumber}"; labelPoints.Text = $"Points: {GameManager.Instance.Points}"; labelNextLevel.Text = $"Next Level at: {GameManager.Instance.NextLevelPoints}"; labelLevel.Text = $"Actual Level: {GameManager.Instance.Level}"; @@ -45,6 +46,8 @@ public partial class Ui : Node2D restartButton.Visible = GameManager.Instance.GameOver; reachedPointsLabel.Visible = GameManager.Instance.GameOver; } + + vBoxPause.Visible = GameManager.Instance.GamePaused; } private void RestartButtonOnPressed() => GetTree().ReloadCurrentScene(); diff --git a/project.godot b/project.godot index bdceb04..49f035e 100644 --- a/project.godot +++ b/project.godot @@ -63,3 +63,10 @@ Player_Fire={ , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":1,"pressure":0.0,"pressed":false,"script":null) ] } +Game_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) +, 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":4194313,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":false,"script":null) +] +}