summaryrefslogtreecommitdiff
path: root/Player.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Player.cs')
-rw-r--r--Player.cs54
1 files changed, 28 insertions, 26 deletions
diff --git a/Player.cs b/Player.cs
index 5d4bb2a..11e3aa2 100644
--- a/Player.cs
+++ b/Player.cs
@@ -1,6 +1,6 @@
using Godot;
-
-public partial class Player : CharacterBody2D
+using testing.interfaces;
+public partial class Player : CharacterBody2D, IDamageable
{
[Export] public EntityStats BaseStats { get; set; }
[Export] public PackedScene BulletScene { get; set; }
@@ -30,31 +30,33 @@ public partial class Player : CharacterBody2D
// If the mouse is to the left of the player, flip the sprite.
// If it's to the right, keep it un-flipped.
_sprite.FlipH = mousePos.X < GlobalPosition.X;
-
- if (Input.IsActionJustPressed("shoot"))
+
+ }
+ public override void _UnhandledInput(InputEvent @event)
+ {
+ if (@event.IsActionPressed("shoot"))
{
- Shoot();
+ // Find the weapon node and tell it to fire at the mouse
+ var weapon = GetNodeOrNull<Weapon>("Weapon");
+ if (weapon != null)
+ {
+ weapon.Fire(GetGlobalMousePosition());
+ }
}
- }
- private void Shoot()
+ }
+ public void TakeDamage(float amount)
{
- // Failsafe in case you forgot to assign the scene in the editor
- if (BulletScene == null) return;
-
- // Create an instance of the bullet
- Bullet bullet = (Bullet)BulletScene.Instantiate();
-
- // Spawn it at the player's current position
- bullet.GlobalPosition = GlobalPosition;
-
- // Calculate the direction from the player to the mouse
- Vector2 mousePos = GetGlobalMousePosition();
- bullet.Direction = GlobalPosition.DirectionTo(mousePos);
-
- // Rotate the bullet to face the direction it's traveling
- bullet.Rotation = bullet.Direction.Angle();
-
- // Add the bullet to the current scene tree so it actually exists in the game
- GetTree().Root.AddChild(bullet);
- }
+ // For right now, just subtract it from the runtime stats we set up earlier!
+ if (_stats?.RuntimeStats != null)
+ {
+ _stats.RuntimeStats.MaxHealth -= amount;
+ GD.Print($"Ouch! Player took {amount} damage. Health remaining: {_stats.RuntimeStats.MaxHealth}");
+
+ if (_stats.RuntimeStats.MaxHealth <= 0)
+ {
+ GD.Print("Player Died!");
+ QueueFree(); // Deletes the player
+ }
+ }
+ }
}