starfighter/Starfighter/script/entities/Laser.cs
2026-03-06 22:49:24 +00:00

69 lines
1.4 KiB
C#

using Godot;
using System;
public partial class Laser : Area2D
{
[Export]
public int Speed { get; set;} = 2000;
public ship Shooter;
private Vector2 MovementVector { get; set; } = new Vector2(0, -1);
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
QueueFree();
}
public override void _Ready()
{
Connect("body_entered", new Callable(this, nameof(OnBodyEntered)));
}
public override void _PhysicsProcess(double delta)
{
GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta;
}
private void OnAreaEntered(Area2D area)
{
if (area is asteroid asteroid)
{
asteroid.Explode(Shooter);
QueueFree();
}
}
private bool ValidTargetFaction(ship body)
{
return Shooter.Faction switch
{
ship.ShipFaction.PLAYER => body.Faction is ship.ShipFaction.ENEMY or ship.ShipFaction.ACE,
ship.ShipFaction.ENEMY => body.Faction is ship.ShipFaction.FRIENDLY or ship.ShipFaction.PLAYER,
ship.ShipFaction.FRIENDLY => body.Faction is ship.ShipFaction.ENEMY or ship.ShipFaction.ACE,
ship.ShipFaction.ACE => body.Faction is ship.ShipFaction.FRIENDLY or ship.ShipFaction.PLAYER,
};
}
private void OnBodyEntered(Node body)
{
if (body is ship target)
{
if (Shooter != null && !ValidTargetFaction(target))
{
QueueFree();
}
else
{
target.ShipDamage(Shooter.Damage);
QueueFree();
}
}
else if (body is StaticBody2D) // border detection
{
QueueFree();
}
}
}