144 lines
3.1 KiB
C#
144 lines
3.1 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class game : Node2D
|
|
{
|
|
public Node Lasers = null;
|
|
public CharacterBody2D Player = null;
|
|
public Node Asteroids = null;
|
|
|
|
public Control HUD = null;
|
|
|
|
public Label ScoreLabel = null;
|
|
|
|
public Label HealthLabel = null;
|
|
|
|
public Label FlightAssistLabel = null;
|
|
|
|
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");
|
|
Player = GetNode<CharacterBody2D>("Player");
|
|
//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 OnPlayerLaserShot(Area2D Laser)
|
|
{
|
|
Lasers.AddChild(Laser);
|
|
GD.Print(Laser.Position);
|
|
GD.Print(Player.Position);
|
|
}
|
|
|
|
public void OnPlayerHealthUpdate(int health)
|
|
{
|
|
UpdateHealthLabel(health);
|
|
}
|
|
|
|
public void OnPlayerDeath()
|
|
{
|
|
GetTree().ReloadCurrentScene(); //Reload scene to act as restart
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|