cirnogodot/Scripts/Bullet.cs

55 lines
1.2 KiB
C#
Raw Normal View History

2024-02-27 17:16:55 +01:00
using Godot;
using System;
using System.Diagnostics;
2024-02-27 22:54:42 +01:00
public partial class Bullet : Area2D
2024-02-27 17:16:55 +01:00
{
2024-02-27 22:54:42 +01:00
[Export]
public float Speed = 1900f;
private Vector2 _direction = Vector2.Right;
2024-05-15 15:48:48 +02:00
//public delegate void BulletHitEventHandler(Node Body);
//public event BulletHitEventHandler BulletHit;
2024-02-27 17:16:55 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
2024-05-15 15:48:48 +02:00
//this.Connect("body_entered", new Callable(this, nameof(OnBodyEntered)));
2024-02-27 17:16:55 +01:00
}
2024-05-15 15:48:48 +02:00
//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();
//}
2024-05-01 11:48:04 +02:00
public void SetDirection(Vector2 direction)
{
var normalized = direction.Normalized();
_direction = normalized;
2024-05-02 12:50:08 +02:00
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
2024-05-01 11:48:04 +02:00
}
2024-02-27 17:16:55 +01:00
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
2024-02-27 22:54:42 +01:00
this.Position += ((float) (Speed * delta) * _direction);
}
2024-02-27 17:16:55 +01:00
2024-02-27 22:54:42 +01:00
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
2024-05-02 12:50:08 +02:00
//Debug.WriteLine("Destroy bullet out of screen");
2024-02-27 22:54:42 +01:00
QueueFree();
2024-02-27 17:16:55 +01:00
}
2024-02-27 22:54:42 +01:00
2024-02-27 17:16:55 +01:00
}
2024-02-27 22:54:42 +01:00