132 lines
3.4 KiB
C#
132 lines
3.4 KiB
C#
using System;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace GodotspaceShooter.Scripts;
|
|
|
|
public partial class GameManager : Node {
|
|
private int asteroidMaxNumber = 3;
|
|
private int asteroidNumber;
|
|
|
|
private bool gameOver;
|
|
public bool GameOver {
|
|
get => gameOver;
|
|
private set {
|
|
gameOver = value;
|
|
GetTree().Paused = gameOver;
|
|
SoundManager.Instance.Stop(SoundManager.Sound.BackgroundMusic);
|
|
SoundManager.Instance.Play(SoundManager.Sound.GameOver);
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
[Export] private Array<PackedScene> asteroids;
|
|
[Export] private Node3D asteroidsContainer;
|
|
|
|
private int level = 1;
|
|
public int MaxLives { get; private set; } = 10;
|
|
private int lives = 10;
|
|
private int nextLevelPoints = 11;
|
|
private int points;
|
|
private float spawnRate = 1f;
|
|
private float spawnTimer;
|
|
|
|
public event EventHandler RefreshUi;
|
|
|
|
public int AsteroidNumber {
|
|
get => asteroidNumber;
|
|
set {
|
|
asteroidNumber = value;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public int Lives {
|
|
get => lives;
|
|
set {
|
|
lives = value;
|
|
if (lives <= 0) GameOver = true;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public int Points {
|
|
get => points;
|
|
set {
|
|
points = value;
|
|
if (points >= nextLevelPoints) NextLevel();
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public int Level {
|
|
get => level;
|
|
private set {
|
|
level = value;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public int NextLevelPoints {
|
|
get => nextLevelPoints;
|
|
private set {
|
|
nextLevelPoints = value;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public int AsteroidMaxNumber {
|
|
get => asteroidMaxNumber;
|
|
private set {
|
|
asteroidMaxNumber = value;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
private bool gamePaused;
|
|
public bool GamePaused {
|
|
get => gamePaused;
|
|
set {
|
|
gamePaused = value;
|
|
GetTree().Paused = gamePaused;
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
public static GameManager Instance { get; private set; }
|
|
[Export] public bool Debug { get; private set; } = true;
|
|
|
|
|
|
public override void _Ready() {
|
|
Instance = this;
|
|
Lives = MaxLives;
|
|
spawnTimer = spawnRate;
|
|
SoundManager.Instance.Play(SoundManager.Sound.BackgroundMusic);
|
|
RefreshUi?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public override void _Process(double delta) {
|
|
spawnTimer -= (float)delta;
|
|
if (spawnTimer < 0) {
|
|
if (AsteroidNumber < asteroidMaxNumber) SpawnNewAsteroid();
|
|
spawnTimer = spawnRate;
|
|
}
|
|
}
|
|
|
|
private void SpawnNewAsteroid() {
|
|
if (GameOver) return;
|
|
GD.Print("Spawn new asteroid");
|
|
AsteroidNumber++;
|
|
Node asteroid = asteroids.PickRandom().Instantiate();
|
|
asteroidsContainer.AddChild(asteroid);
|
|
asteroid.Name = $"Asteroid{AsteroidNumber}";
|
|
}
|
|
|
|
private void NextLevel() {
|
|
Ui.Instance.ShowMessage("Next Level!");
|
|
Level++;
|
|
NextLevelPoints = Level * 33;
|
|
AsteroidMaxNumber = 3 + Level;
|
|
spawnRate = 1f - Level * 0.1f;
|
|
}
|
|
}
|