diff options
Diffstat (limited to 'Bullet.cs')
| -rw-r--r-- | Bullet.cs | 31 |
1 files changed, 20 insertions, 11 deletions
@@ -2,15 +2,24 @@ using Godot; public partial class Bullet : Area2D { - [Export] public float Speed { get; set; } = 600.0f; - - // The player script will set this direction when the bullet spawns - public Vector2 Direction { get; set; } = Vector2.Zero; + [Export] public float Speed { get; set; } = 600.0f; + public Vector2 Direction { get; set; } = Vector2.Zero; - public override void _PhysicsProcess(double delta) - { - // Move the bullet over time. - // We multiply by delta to ensure movement is framerate-independent. - Position += Direction * Speed * (float)delta; - } -}
\ No newline at end of file + public override void _Ready() + { + // Subscribe to the built-in BodyEntered event when the bullet is spawned + BodyEntered += OnBodyEntered; + } + + 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) + { + // QueueFree safely deletes this node and all its children at the end of the current frame + QueueFree(); + } +} |
