Godot-SpaceShooter/Scripts/GameManager.cs

50 lines
1.2 KiB
C#

using Godot;
using Godot.Collections;
namespace Scripts;
public partial class GameManager : Node
{
[Export] private Array<PackedScene> asteroids;
[Export] private float spawnRate = 1f;
private float spawnTimer;
[Export] private RichTextLabel labelAsteroids;
[Export] private RichTextLabel labelLifes;
private int asteroidNumber;
private const int asteroidMaxNumber = 100;
private int lives = 3;
public static GameManager Instance { get; private set; }
public override void _Ready() {
Instance = this;
spawnTimer = spawnRate;
labelLifes.Text = $"Lives: {lives}";
labelAsteroids.Text = $"Asteroids: {asteroidNumber}";
}
public override void _Process(double delta) {
spawnTimer -= (float)delta;
if (spawnTimer < 0) {
if (asteroidNumber < asteroidMaxNumber) SpawnNewAsteroid();
spawnTimer = spawnRate;
}
}
private void SpawnNewAsteroid() {
GD.Print("Spawn new asteroid");
asteroidNumber++;
Node asteroid = asteroids.PickRandom().Instantiate();
asteroid.Name = $"Asteroid{asteroidNumber}";
AddChild(asteroid);
labelAsteroids.Text = $"Asteroids: {asteroidNumber}";
}
public void RemoveLife() {
lives--;
labelLifes.Text = $"Lives: {lives}";
if (lives <= 0 ) { GD.Print("GAME OVER!");}
}
}