70 lines
1.6 KiB
C#
70 lines
1.6 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class Laser : Area2D
|
|
{
|
|
[Export]
|
|
public int Speed { get; set;} = 2000;
|
|
|
|
public ship Shooter;
|
|
|
|
public 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 a = (asteroid)area;
|
|
a.Explode();
|
|
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,
|
|
_ => throw new ArgumentOutOfRangeException() // thank you rider very cool
|
|
};
|
|
}
|
|
|
|
private void OnBodyEntered(Node body)
|
|
{
|
|
GD.Print("Laser hit something: ", body.Name);
|
|
|
|
if (body is ship target)
|
|
{
|
|
if (Shooter != null && !ValidTargetFaction(target))
|
|
{
|
|
GD.Print("friendly fire");
|
|
QueueFree();
|
|
return; // prevent friendly fire
|
|
}
|
|
else
|
|
{
|
|
GD.Print("Hit type: ", body.GetType());
|
|
target.ShipDamage(Shooter.Damage);
|
|
QueueFree();
|
|
}
|
|
}
|
|
}
|
|
}
|