diff options
| author | alex <[email protected]> | 2026-07-24 23:33:46 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-24 23:33:46 +0200 |
| commit | 91fd5d80808fd177f97c4cc7e78b704efb5d6ce3 (patch) | |
| tree | fc0dee06eae293afb5e429fa0956455aee585f01 /Player.cs | |
| parent | 6b666c7f25121e61bf1c58ee65cb2cd0f254b887 (diff) | |
| download | godot-testing-91fd5d80808fd177f97c4cc7e78b704efb5d6ce3.tar.xz godot-testing-91fd5d80808fd177f97c4cc7e78b704efb5d6ce3.zip | |
improved stats resource, weapon now uses dmg from player stats
Diffstat (limited to 'Player.cs')
| -rw-r--r-- | Player.cs | 54 |
1 files changed, 28 insertions, 26 deletions
@@ -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 + } + } + } } |
