summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/Enemy.cs40
-rw-r--r--scripts/Enemy.cs.uid1
2 files changed, 41 insertions, 0 deletions
diff --git a/scripts/Enemy.cs b/scripts/Enemy.cs
new file mode 100644
index 0000000..cdbcd38
--- /dev/null
+++ b/scripts/Enemy.cs
@@ -0,0 +1,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
+ }
+ }
+} \ No newline at end of file
diff --git a/scripts/Enemy.cs.uid b/scripts/Enemy.cs.uid
new file mode 100644
index 0000000..dd3ea0c
--- /dev/null
+++ b/scripts/Enemy.cs.uid
@@ -0,0 +1 @@
+uid://cml2xy7flhfn6