reload scene on player health <=0, spawn in random asteroids when none left

This commit is contained in:
rsxri 2024-08-16 11:18:44 +01:00
parent b9c05c7ac3
commit 55cf630cb1
6 changed files with 116 additions and 4 deletions

View file

@ -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"]

View file

@ -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"

View file

@ -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);

View file

@ -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

View file

@ -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);
}
}

View file

@ -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()