-feat: lasers and asteroids now despawn when collide with world border.

-fix: asteroids blown up by ai do not increase score.
This commit is contained in:
rsxri 2025-04-21 19:07:15 +01:00
parent 6a03a1d878
commit cf500dc06e
4 changed files with 55 additions and 29 deletions

File diff suppressed because one or more lines are too long

View file

@ -28,7 +28,7 @@ public partial class Laser : Area2D
{
if (area is asteroid asteroid)
{
asteroid.Explode();
asteroid.Explode(Shooter);
QueueFree();
}
}
@ -63,5 +63,11 @@ public partial class Laser : Area2D
QueueFree();
}
}
else if (body is StaticBody2D)
{
//GD.Print("laser hit border");
QueueFree();
}
}
}

View file

@ -4,7 +4,7 @@ using System;
public partial class asteroid : Area2D
{
[Signal]
public delegate void ExplodedEventHandler(Vector2 pos, int size);
public delegate void ExplodedEventHandler(Vector2 pos, int size, ship shooter);
public enum AsteroidSize {LARGE, MEDIUM, SMALL};
@ -68,16 +68,16 @@ public partial class asteroid : Area2D
}
}
public void Explode()
public void Explode(ship shooter)
{
//DEBUG PRINT
GD.Print($"DEBUG: Explode() called for {Name} at {GlobalPosition}");
GD.Print($"DEBUG: {Name} calling Explode()");
EmitSignal(SignalName.Exploded, GlobalPosition, (int)Size);
EmitSignal(SignalName.Exploded, GlobalPosition, (int)Size, shooter);
QueueFree();
}
private static void OnBodyEntered(CharacterBody2D body)
private void OnBodyEntered(Node body)
{
/*if (body is player player)
{
@ -85,10 +85,16 @@ public partial class asteroid : Area2D
GD.Print("player asteroid collide");
}*/
if (body is not ship ship) return;
ship.ShipDamage((30));
GD.Print(("ship asteroid collide"));
GD.Print(ship.Name, ship.Health);
if (body is ship ship)
{
ship.ShipDamage((30));
GD.Print(("ship asteroid collide"));
GD.Print(ship.Name, ship.Health);
}
else if (body is StaticBody2D)
{
QueueFree();
}
}

View file

@ -156,27 +156,35 @@ public partial class game : Node2D
GetTree().ReloadCurrentScene();
}
private void OnAsteroidExploded(Vector2 pos, int size)
private void OnAsteroidExploded(Vector2 pos, int size, ship shooter)
{
// DEBUG PRINT
GD.Print($"DEBUG: Asteroid exploded at {pos}, size: {size}");
if (size == 0)
switch (size)
{
for (int i = 0; i < 2; i++){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM);
case 0:
{
for (int i = 0; i < 2; i++){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM);
}
if (shooter is not player) return;
_score += 60;
break;
}
_score += 60;
}
else if (size == 1)
{
for (int i = 0; i < 2; i++){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL);
case 1:
{
for (int i = 0; i < 2; i++){
SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL);
}
if (shooter is not player) return;
_score += 40;
break;
}
_score += 40;
}
else if (size == 2)
{
_score += 20;
case 2:
if (shooter is not player) return;
_score += 20;
break;
}
GD.Print(_score);
UpdateScoreLabel(_score);