-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) if (area is asteroid asteroid)
{ {
asteroid.Explode(); asteroid.Explode(Shooter);
QueueFree(); QueueFree();
} }
} }
@ -63,5 +63,11 @@ public partial class Laser : Area2D
QueueFree(); 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 public partial class asteroid : Area2D
{ {
[Signal] [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}; 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 //DEBUG PRINT
GD.Print($"DEBUG: Explode() called for {Name} at {GlobalPosition}"); GD.Print($"DEBUG: Explode() called for {Name} at {GlobalPosition}");
GD.Print($"DEBUG: {Name} calling Explode()"); GD.Print($"DEBUG: {Name} calling Explode()");
EmitSignal(SignalName.Exploded, GlobalPosition, (int)Size); EmitSignal(SignalName.Exploded, GlobalPosition, (int)Size, shooter);
QueueFree(); QueueFree();
} }
private static void OnBodyEntered(CharacterBody2D body) private void OnBodyEntered(Node body)
{ {
/*if (body is player player) /*if (body is player player)
{ {
@ -85,11 +85,17 @@ public partial class asteroid : Area2D
GD.Print("player asteroid collide"); GD.Print("player asteroid collide");
}*/ }*/
if (body is not ship ship) return; if (body is ship ship)
{
ship.ShipDamage((30)); ship.ShipDamage((30));
GD.Print(("ship asteroid collide")); GD.Print(("ship asteroid collide"));
GD.Print(ship.Name, ship.Health); GD.Print(ship.Name, ship.Health);
} }
else if (body is StaticBody2D)
{
QueueFree();
}
}
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)

View file

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