2023-11-20 09:35:59 +00:00
|
|
|
using Godot;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
public partial class game : Node2D
|
|
|
|
|
{
|
|
|
|
|
public Node Lasers = null;
|
|
|
|
|
public CharacterBody2D Player = null;
|
2023-12-08 14:21:45 +00:00
|
|
|
public Node Asteroids = null;
|
|
|
|
|
|
2023-12-09 07:49:33 +00:00
|
|
|
public Control HUD = null;
|
|
|
|
|
|
|
|
|
|
public Label ScoreLabel = null;
|
|
|
|
|
|
2024-08-15 17:12:48 +00:00
|
|
|
public Label HealthLabel = null;
|
|
|
|
|
|
2024-08-15 21:55:44 +00:00
|
|
|
public Label FlightAssistLabel = null;
|
|
|
|
|
|
2023-12-09 07:49:33 +00:00
|
|
|
public hud h;
|
|
|
|
|
|
|
|
|
|
|
2023-12-08 14:21:45 +00:00
|
|
|
private readonly PackedScene AsteroidScene = GD.Load<PackedScene>("res://scenes/asteroid.tscn");
|
2023-11-20 09:35:59 +00:00
|
|
|
|
2023-12-09 07:49:33 +00:00
|
|
|
public int Score = 0;
|
|
|
|
|
|
2023-11-20 09:35:59 +00:00
|
|
|
public override void _Ready()
|
|
|
|
|
{
|
2023-12-09 07:49:33 +00:00
|
|
|
HUD = GetNode<Control>("UI/HUD");
|
|
|
|
|
ScoreLabel = GetNode<Label>("UI/HUD/Score");
|
2024-08-15 21:55:44 +00:00
|
|
|
FlightAssistLabel = GetNode<Label>("UI/HUD/FlightAssist");
|
2024-08-15 17:12:48 +00:00
|
|
|
//HealthLabel = GetNode<Label>("UI/HUD/Health");
|
2023-12-09 07:49:33 +00:00
|
|
|
|
2023-12-08 14:21:45 +00:00
|
|
|
Asteroids = GetNode<Node>("Asteroids");
|
|
|
|
|
var a = new asteroid();
|
|
|
|
|
for (int i = 0; i == Asteroids.GetChildCount() - 1; i++){
|
|
|
|
|
a = (asteroid)Asteroids.GetChild(i);
|
|
|
|
|
a.Exploded += OnAsteroidExploded;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 13:40:50 +00:00
|
|
|
Lasers = GetNode<Node>("Lasers");
|
|
|
|
|
Player = GetNode<CharacterBody2D>("Player");
|
2024-08-15 17:12:48 +00:00
|
|
|
//var p = new player();
|
|
|
|
|
//p.LaserShot += OnPlayerLaserShot;
|
2023-12-08 14:21:45 +00:00
|
|
|
}
|
2023-11-20 09:35:59 +00:00
|
|
|
|
2024-08-12 18:47:47 +00:00
|
|
|
public override void _Process(double delta)
|
|
|
|
|
{
|
|
|
|
|
if (Input.IsActionJustPressed("reset"))
|
2023-12-09 07:49:33 +00:00
|
|
|
{
|
|
|
|
|
GetTree().ReloadCurrentScene();
|
|
|
|
|
}
|
2024-08-15 21:55:44 +00:00
|
|
|
|
|
|
|
|
if (Input.IsActionJustPressed("toggle_fa"))
|
|
|
|
|
{
|
|
|
|
|
UpdateFALabel();
|
|
|
|
|
}
|
2024-08-12 18:47:47 +00:00
|
|
|
}
|
2023-12-09 07:49:33 +00:00
|
|
|
|
2024-08-12 18:47:47 +00:00
|
|
|
public void SpawnAsteroid(Vector2 position, int size)
|
2023-12-08 14:21:45 +00:00
|
|
|
{
|
|
|
|
|
var a = new asteroid();
|
|
|
|
|
a = AsteroidScene.Instantiate<asteroid>();
|
|
|
|
|
a.GlobalPosition = position;
|
|
|
|
|
a.size = (asteroid.AsteroidSize)size;
|
|
|
|
|
a.Exploded += OnAsteroidExploded;
|
|
|
|
|
Asteroids.CallDeferred("add_child", a);
|
2023-11-20 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-09 07:49:33 +00:00
|
|
|
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();
|
2024-08-12 18:47:47 +00:00
|
|
|
}
|
2023-12-09 07:49:33 +00:00
|
|
|
|
2024-08-15 17:12:48 +00:00
|
|
|
public void UpdateHealthLabel(int health)
|
|
|
|
|
{
|
|
|
|
|
if (HealthLabel == null)
|
|
|
|
|
{
|
|
|
|
|
HealthLabel = GetNode<Label>("UI/HUD/Health");
|
|
|
|
|
}
|
|
|
|
|
HealthLabel.Text = "HEALTH: " + health.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 21:55:44 +00:00
|
|
|
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";}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 14:21:45 +00:00
|
|
|
//Signals and Connections
|
|
|
|
|
public void OnPlayerLaserShot(Area2D Laser)
|
2023-11-20 09:35:59 +00:00
|
|
|
{
|
|
|
|
|
Lasers.AddChild(Laser);
|
2023-11-25 13:40:50 +00:00
|
|
|
GD.Print(Laser.Position);
|
|
|
|
|
GD.Print(Player.Position);
|
2023-11-20 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-15 17:12:48 +00:00
|
|
|
public void OnPlayerHealthUpdate(int health)
|
|
|
|
|
{
|
|
|
|
|
UpdateHealthLabel(health);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 14:21:45 +00:00
|
|
|
public void OnAsteroidExploded(Vector2 pos, int size)
|
|
|
|
|
{
|
2023-12-09 07:49:33 +00:00
|
|
|
//score hardcoded for now, will make implementation of enemy scores easier later on
|
|
|
|
|
// score = score * 2 (for loop)
|
2023-12-08 14:21:45 +00:00
|
|
|
for(int i = 0; i < 2; i++){
|
|
|
|
|
if (size == 0){
|
|
|
|
|
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM);
|
2023-12-09 07:49:33 +00:00
|
|
|
Score += 60;
|
2023-12-08 14:21:45 +00:00
|
|
|
}
|
|
|
|
|
else if (size == 1){
|
|
|
|
|
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL);
|
2023-12-09 07:49:33 +00:00
|
|
|
Score += 40;
|
|
|
|
|
}
|
|
|
|
|
else if (size == 2)
|
|
|
|
|
{
|
|
|
|
|
Score += 20;
|
2023-12-08 14:21:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-09 07:49:33 +00:00
|
|
|
GD.Print(Score);
|
|
|
|
|
UpdateScoreLabel(Score);
|
2023-12-08 14:21:45 +00:00
|
|
|
}
|
2023-11-20 09:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|