summaryrefslogtreecommitdiff
path: root/Bullet.cs
blob: 4ca952038b87e25783b55f4b1df7d605eb5df3d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Godot;
using testing.interfaces;
public partial class Bullet : Area2D
{
	[Export] public float Speed { get; set; } = 600.0f;
	[Export] public float Damage { get; set; } = 10.0f;
	public Vector2 Direction { get; set; } = Vector2.Zero;

	public override void _Ready()
	{
		// Subscribe to the built-in BodyEntered event when the bullet is spawned
		BodyEntered += OnBodyEntered;
		
		Rotation = Direction.Angle();
	}

	public override void _PhysicsProcess(double delta)
	{
		Position += Direction * Speed * (float)delta;
	}

	// This method is triggered automatically when the bullet hits a physics body
	private void OnBodyEntered(Node2D body)
	{
		if (body is IDamageable target)
		{
			// If it does, deal damage!
			target.TakeDamage(Damage);
		}
	}
}