summaryrefslogtreecommitdiff
path: root/Bullet.cs
blob: db44e2abfdc95beb927ad958a48f3eba736cd3a1 (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
32
33
34
35
36
37
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;
	[Export] public bool PierceEntity { get; set; } = false;
	[Export] public bool PierceTerrain { get; set; } = false;
	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);
			if(!PierceEntity){QueueFree();}
		}
		else{if(!PierceTerrain){QueueFree();}}
		

	}
}