From 55cf630cb15c5fe1cdce0b03dab7f000cf7614d2 Mon Sep 17 00:00:00 2001 From: rsxri Date: Fri, 16 Aug 2024 11:18:44 +0100 Subject: [PATCH] reload scene on player health <=0, spawn in random asteroids when none left --- MB_FYP/scenes/game.tscn | 1 + MB_FYP/scenes/input_menu.tscn | 93 +++++++++++++++++++++++++++++++++++ MB_FYP/script/Laser.cs | 2 +- MB_FYP/script/game.cs | 14 ++++++ MB_FYP/script/player.cs | 7 ++- MB_FYP/script/ship.cs | 3 +- 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 MB_FYP/scenes/input_menu.tscn diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index 8f8d8fc..a95b119 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -41,6 +41,7 @@ position = Vector2(387, 230) [connection signal="HealthUpdate" from="Player" to="." method="OnPlayerHealthUpdate"] [connection signal="LaserShot" from="Player" to="." method="OnPlayerLaserShot"] +[connection signal="PlayerDeath" from="Player" to="." method="OnPlayerDeath"] [connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid3" to="." method="OnAsteroidExploded"] diff --git a/MB_FYP/scenes/input_menu.tscn b/MB_FYP/scenes/input_menu.tscn new file mode 100644 index 0000000..db90319 --- /dev/null +++ b/MB_FYP/scenes/input_menu.tscn @@ -0,0 +1,93 @@ +[gd_scene format=3 uid="uid://01tjlnox4crb"] + +[node name="InputMenu" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = 13.0 +offset_top = 13.0 +offset_right = 7.0 +offset_bottom = 3.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +layout_mode = 2 +offset_right = 351.0 +offset_bottom = 566.0 + +[node name="Forward" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpFwdLabel" type="Label" parent="VBoxContainer/Forward"] +layout_mode = 2 +text = "Forward" + +[node name="InpFwd" type="Button" parent="VBoxContainer/Forward"] +layout_mode = 2 +text = "W" + +[node name="Backward" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpBckLabel" type="Label" parent="VBoxContainer/Backward"] +layout_mode = 2 +text = "Backward" + +[node name="InpBck" type="Button" parent="VBoxContainer/Backward"] +layout_mode = 2 +text = "S" + +[node name="Left" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpLftLabel" type="Label" parent="VBoxContainer/Left"] +layout_mode = 2 +text = "Left" + +[node name="InpLft" type="Button" parent="VBoxContainer/Left"] +layout_mode = 2 +text = "A" + +[node name="Right" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpRhtLabel" type="Label" parent="VBoxContainer/Right"] +layout_mode = 2 +text = "Right" + +[node name="InpRht" type="Button" parent="VBoxContainer/Right"] +layout_mode = 2 +text = "D" + +[node name="Shoot" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpShootLabel" type="Label" parent="VBoxContainer/Shoot"] +layout_mode = 2 +text = "Shoot" + +[node name="InpShoot" type="Button" parent="VBoxContainer/Shoot"] +layout_mode = 2 +text = "SPACE" + +[node name="Flight Assist" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="InpFALabel" type="Label" parent="VBoxContainer/Flight Assist"] +layout_mode = 2 +text = "Flight Assist" + +[node name="InpFA" type="Button" parent="VBoxContainer/Flight Assist"] +layout_mode = 2 +text = "F" + +[node name="Back" type="Button" parent="VBoxContainer"] +layout_mode = 2 +text = "Back +" + +[node name="Save" type="Button" parent="VBoxContainer"] +layout_mode = 2 +text = "Save" diff --git a/MB_FYP/script/Laser.cs b/MB_FYP/script/Laser.cs index f592730..1f5b895 100644 --- a/MB_FYP/script/Laser.cs +++ b/MB_FYP/script/Laser.cs @@ -4,7 +4,7 @@ using System; public partial class Laser : Area2D { [Export] - public int Speed { get; set;} = 1000; + public int Speed { get; set;} = 2000; public Vector2 MovementVector { get; set; } = new Vector2(0, -1); diff --git a/MB_FYP/script/game.cs b/MB_FYP/script/game.cs index df8442f..b40cda4 100644 --- a/MB_FYP/script/game.cs +++ b/MB_FYP/script/game.cs @@ -53,6 +53,15 @@ public partial class game : Node2D { UpdateFALabel(); } + + if (Asteroids.GetChildCount() == 0) + { + Random rand = new Random(); + for (int i = 0; i < rand.Next(1, 5); i++) + { + SpawnAsteroid(new Vector2(rand.Next(10, 800), rand.Next(10, 800)), (int)asteroid.AsteroidSize.LARGE); + } + } } public void SpawnAsteroid(Vector2 position, int size) @@ -104,6 +113,11 @@ public partial class game : Node2D UpdateHealthLabel(health); } + public void OnPlayerDeath() + { + GetTree().ReloadCurrentScene(); //Reload scene to act as restart + } + public void OnAsteroidExploded(Vector2 pos, int size) { //score hardcoded for now, will make implementation of enemy scores easier later on diff --git a/MB_FYP/script/player.cs b/MB_FYP/script/player.cs index f5b42d1..05e1b3b 100644 --- a/MB_FYP/script/player.cs +++ b/MB_FYP/script/player.cs @@ -8,6 +8,9 @@ public partial class player : ship // Inherits from base ship class [Signal] public delegate void HealthUpdateEventHandler(int health); + [Signal] + public delegate void PlayerDeathEventHandler(); + [Export] public ShipColor Color; @@ -107,9 +110,9 @@ public partial class player : ship // Inherits from base ship class ShootLaser(); } - if (Health < 0) + if (Health <= 0) { - GD.Print("dead lol"); + EmitSignal(SignalName.PlayerDeath); } } diff --git a/MB_FYP/script/ship.cs b/MB_FYP/script/ship.cs index 810fddd..e762773 100644 --- a/MB_FYP/script/ship.cs +++ b/MB_FYP/script/ship.cs @@ -64,6 +64,7 @@ public partial class ship : CharacterBody2D public virtual void ShipDamage(int damage) { Health -= damage; + if (Health < 0){ Health = 0;} } public override void _Ready() @@ -80,4 +81,4 @@ public partial class ship : CharacterBody2D MoveAndSlide(); } -} \ No newline at end of file +}