cirnogodot/Scripts/Bullet.cs
2024-05-26 11:40:35 +02:00

67 lines
1.3 KiB
C#

using Godot;
using System;
using System.Diagnostics;
public partial class Bullet : Area2D
{
[Export]
public float Speed = 1900f;
private Vector2 _direction = Vector2.Right;
//public delegate void BulletHitEventHandler(Node Body);
//public event BulletHitEventHandler BulletHit;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
//this.Connect("body_entered", new Callable(this, nameof(OnBodyEntered)));
}
//private void OnBodyEntered(Node body)
//{
// When a body is entered, invoke the event and pass the collided body
// BulletHit?.Invoke(body);
// Then remove the bullet
// QueueFree();
//}
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
this.Position += ((float)(Speed * delta) * _direction);
}
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
//Debug.WriteLine("Destroy bullet out of screen");
QueueFree();
}
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
Debug.WriteLine("Collision");
QueueFree();
}
}
}