starfighter/MB_FYP/script/game.cs

63 lines
1.4 KiB
C#

using Godot;
using System;
public partial class game : Node2D
{
public Node Lasers = null;
public CharacterBody2D Player = null;
public Node Asteroids = null;
private readonly PackedScene AsteroidScene = GD.Load<PackedScene>("res://scenes/asteroid.tscn");
public override void _Ready()
{
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;
}
Lasers = GetNode<Node>("Lasers");
Player = GetNode<CharacterBody2D>("Player");
var p = new player();
p.LaserShot += OnPlayerLaserShot;
}
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);
}
//Signals and Connections
public void OnPlayerLaserShot(Area2D Laser)
{
Lasers.AddChild(Laser);
GD.Print(Laser.Position);
GD.Print(Player.Position);
}
public void OnAsteroidExploded(Vector2 pos, int size)
{
GD.Print(size);
for(int i = 0; i < 2; i++){
if (size == 0){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM);
}
else if (size == 1){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL);
}
}
}
}