blob: cdbcd38a3fd31523dee2443f69e56c2c49a25062 (
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
38
39
40
|
using Godot;
using testing.interfaces; // Match your namespace
public partial class Enemy : CharacterBody2D, IDamageable
{
[Export] public float MoveSpeed { get; set; } = 150.0f;
[Export] public float MaxHealth { get; set; } = 30.0f;
private Node2D _player;
public override void _Ready()
{
// Find the player automatically using the group we just set up
_player = GetTree().GetFirstNodeInGroup("player") as Node2D;
}
public override void _PhysicsProcess(double delta)
{
if (_player == null) return;
// Calculate direction vector pointing from the enemy to the player
Vector2 direction = GlobalPosition.DirectionTo(_player.GlobalPosition);
// Move towards the player
Velocity = direction * MoveSpeed;
MoveAndSlide();
}
public void TakeDamage(float amount)
{
MaxHealth -= amount;
GD.Print($"Enemy took {amount} damage! Health remaining: {MaxHealth}");
if (MaxHealth <= 0)
{
GD.Print("Enemy defeated!");
QueueFree(); // Removes the enemy from the game
}
}
}
|