cirnogodot/Scripts/Bullet.cs

89 lines
1.9 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;
2024-06-09 00:34:36 +02:00
[Export]
public float Damage = 1f;
2024-02-27 22:54:42 +01:00
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)
//{
2024-05-26 11:40:35 +02:00
// When a body is entered, invoke the event and pass the collided body
2024-05-15 15:48:48 +02:00
// BulletHit?.Invoke(body);
2024-05-26 11:40:35 +02:00
// Then remove the bullet
2024-05-15 15:48:48 +02:00
// 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-05-26 11:40:35 +02:00
this.Position += ((float)(Speed * delta) * _direction);
2024-02-27 22:54:42 +01:00
}
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-05-26 11:40:35 +02:00
private void _on_body_entered(Node2D body)
{
if (body.IsInGroup("Solid"))
{
2024-06-09 00:34:36 +02:00
//Debug.WriteLine("Collision");
QueueFree();
}
2024-08-18 17:38:32 +02:00
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
// {
// Debug.WriteLine("Collision with destroyable object body");
// QueueFree();
// }
2024-05-26 11:40:35 +02:00
2024-02-27 22:54:42 +01:00
2024-05-26 11:40:35 +02:00
}
2024-06-09 00:34:36 +02:00
private void _on_area_entered(Area2D area)
{
2024-08-18 11:33:17 +02:00
if (area.IsInGroup("Solid"))
{
QueueFree();
return;
}
2024-06-09 00:34:36 +02:00
if (area.IsInGroup("Destroyable") && area is IDestructible destructible)
{
//Debug.WriteLine("Collision with destroyable object area");
destructible.Hit(Damage);
2024-05-26 11:40:35 +02:00
2024-06-09 00:34:36 +02:00
QueueFree();
}
}
}