189 lines
3.8 KiB
C#
189 lines
3.8 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class game : Node2D
|
|
{
|
|
public Node Lasers;
|
|
public CharacterBody2D Player;
|
|
public Node Asteroids;
|
|
|
|
public Node Friendlies;
|
|
|
|
public Node Enemies;
|
|
|
|
public Node Ships;
|
|
|
|
public Control HUD;
|
|
|
|
public Label ScoreLabel;
|
|
|
|
public Label HealthLabel;
|
|
|
|
public Label FlightAssistLabel;
|
|
|
|
public hud H;
|
|
|
|
|
|
private readonly PackedScene _asteroidScene = GD.Load<PackedScene>("res://scenes/asteroid.tscn");
|
|
|
|
public int Score = 0;
|
|
|
|
public override void _Ready()
|
|
{
|
|
HUD = GetNode<Control>("UI/HUD");
|
|
ScoreLabel = GetNode<Label>("UI/HUD/Score");
|
|
FlightAssistLabel = GetNode<Label>("UI/HUD/FlightAssist");
|
|
//HealthLabel = GetNode<Label>("UI/HUD/Health");
|
|
Asteroids = GetNode<Node>("Asteroids");
|
|
|
|
Lasers = GetNode<Node>("Lasers");
|
|
Ships = GetNode<Node>("Ships");
|
|
Enemies = GetNode<Node>("Ships/Enemy");
|
|
Friendlies = GetNode<Node>("Ships/Friendly");
|
|
Player = GetNode<CharacterBody2D>("Ships/Friendly/Player");
|
|
|
|
|
|
//AI fighter signals
|
|
foreach (Node node in Enemies.GetChildren())
|
|
{
|
|
if (node is ai_fighter ai)
|
|
{
|
|
GD.Print("Connected laser signal for: ", ai.Name);
|
|
|
|
ai.LaserShot += OnAILaserShot;
|
|
}
|
|
}
|
|
|
|
foreach (Node node in Friendlies.GetChildren())
|
|
{
|
|
if (node is player) continue;
|
|
|
|
if (node is ai_fighter ai)
|
|
{
|
|
GD.Print("Connected laser signal for: ", ai.Name);
|
|
|
|
ai.LaserShot += OnAILaserShot;
|
|
}
|
|
}
|
|
|
|
//var p = new player();
|
|
//p.LaserShot += OnPlayerLaserShot;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (Input.IsActionJustPressed("reset"))
|
|
{
|
|
GetTree().ReloadCurrentScene();
|
|
}
|
|
|
|
if (Input.IsActionJustPressed("toggle_fa"))
|
|
{
|
|
UpdateFALabel();
|
|
}
|
|
|
|
//AUTO SPAWN AFTER CLEAR
|
|
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)
|
|
{
|
|
var a = new asteroid();
|
|
a = _asteroidScene.Instantiate<asteroid>();
|
|
a.GlobalPosition = position;
|
|
a.Size = (asteroid.AsteroidSize)size;
|
|
a.Exploded += OnAsteroidExploded;
|
|
Asteroids.CallDeferred("add_child", a);
|
|
}
|
|
|
|
public void UpdateScoreLabel(int score)
|
|
{
|
|
/*var h = new hud();
|
|
GD.Print(h.Score);
|
|
h.Score.Text = "SCORE: " + score.ToString();
|
|
GD.Print(h.Score.Text);*/
|
|
|
|
ScoreLabel.Text = "SCORE: " + score.ToString();
|
|
}
|
|
|
|
public void UpdateHealthLabel(int health)
|
|
{
|
|
if (HealthLabel == null)
|
|
{
|
|
HealthLabel = GetNode<Label>("UI/HUD/Health");
|
|
}
|
|
HealthLabel.Text = "HEALTH: " + health.ToString();
|
|
}
|
|
|
|
public void UpdateFALabel()
|
|
{
|
|
// Bit of a hacky implementation I think, but it works.
|
|
if (FlightAssistLabel.Text == "FA: OFF"){FlightAssistLabel.Text = "FA: ON";}
|
|
else if (FlightAssistLabel.Text == "FA: ON"){FlightAssistLabel.Text = "FA: OFF";}
|
|
}
|
|
|
|
//Signals and Connections
|
|
public void OnAILaserShot(Area2D Laser)
|
|
{
|
|
Lasers.AddChild(Laser);
|
|
}
|
|
|
|
public void OnPlayerLaserShot(Area2D laser)
|
|
{
|
|
Lasers.AddChild(laser);
|
|
GD.Print(laser.Position);
|
|
GD.Print(Player.Position);
|
|
}
|
|
|
|
public void OnPlayerHealthUpdate(int health)
|
|
{
|
|
UpdateHealthLabel(health);
|
|
}
|
|
|
|
public void OnPlayerDeath()
|
|
{
|
|
CallDeferred(nameof(SafeReloadScene)); //Reload scene to act as restart
|
|
}
|
|
|
|
private void SafeReloadScene()
|
|
{
|
|
GD.Print("Reloading scene");
|
|
GetTree().ReloadCurrentScene();
|
|
}
|
|
|
|
public void OnAsteroidExploded(Vector2 pos, int size)
|
|
{
|
|
// DEBUG PRINT
|
|
GD.Print($"DEBUG: Asteroid exploded at {pos}, size: {size}");
|
|
if (size == 0)
|
|
{
|
|
for (int i = 0; i < 2; i++){
|
|
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM);
|
|
}
|
|
Score += 60;
|
|
}
|
|
else if (size == 1)
|
|
{
|
|
for (int i = 0; i < 2; i++){
|
|
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL);
|
|
}
|
|
Score += 40;
|
|
}
|
|
else if (size == 2)
|
|
{
|
|
Score += 20;
|
|
}
|
|
GD.Print(Score);
|
|
UpdateScoreLabel(Score);
|
|
}
|
|
}
|
|
|
|
|