From 91fd5d80808fd177f97c4cc7e78b704efb5d6ce3 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 24 Jul 2026 23:33:46 +0200 Subject: improved stats resource, weapon now uses dmg from player stats --- .idea/.idea.testing/.idea/.name | 1 + .idea/.idea.testing/.idea/indexLayout.xml | 8 ++++ .idea/.idea.testing/.idea/libraries/GdSdk.xml | 9 +++++ .idea/.idea.testing/.idea/vcs.xml | 7 ++++ Bullet.cs | 12 ++++-- Player.cs | 54 ++++++++++++++------------- Weapon.cs | 39 +++++++++++++++++++ Weapon.cs.uid | 1 + player.tscn | 11 ++++++ scripts/interfaces/IDamageable.cs | 6 +++ scripts/interfaces/IDamageable.cs.uid | 1 + scripts/resources/EntityStats.cs | 2 + 12 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 .idea/.idea.testing/.idea/.name create mode 100644 .idea/.idea.testing/.idea/indexLayout.xml create mode 100644 .idea/.idea.testing/.idea/libraries/GdSdk.xml create mode 100644 .idea/.idea.testing/.idea/vcs.xml create mode 100644 Weapon.cs create mode 100644 Weapon.cs.uid create mode 100644 scripts/interfaces/IDamageable.cs create mode 100644 scripts/interfaces/IDamageable.cs.uid diff --git a/.idea/.idea.testing/.idea/.name b/.idea/.idea.testing/.idea/.name new file mode 100644 index 0000000..9a2c773 --- /dev/null +++ b/.idea/.idea.testing/.idea/.name @@ -0,0 +1 @@ +testing \ No newline at end of file diff --git a/.idea/.idea.testing/.idea/indexLayout.xml b/.idea/.idea.testing/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.testing/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.testing/.idea/libraries/GdSdk.xml b/.idea/.idea.testing/.idea/libraries/GdSdk.xml new file mode 100644 index 0000000..0059883 --- /dev/null +++ b/.idea/.idea.testing/.idea/libraries/GdSdk.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.testing/.idea/vcs.xml b/.idea/.idea.testing/.idea/vcs.xml new file mode 100644 index 0000000..8306744 --- /dev/null +++ b/.idea/.idea.testing/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Bullet.cs b/Bullet.cs index e613849..4ca9520 100644 --- a/Bullet.cs +++ b/Bullet.cs @@ -1,14 +1,17 @@ using Godot; - +using testing.interfaces; public partial class Bullet : Area2D { [Export] public float Speed { get; set; } = 600.0f; + [Export] public float Damage { get; set; } = 10.0f; public Vector2 Direction { get; set; } = Vector2.Zero; public override void _Ready() { // Subscribe to the built-in BodyEntered event when the bullet is spawned BodyEntered += OnBodyEntered; + + Rotation = Direction.Angle(); } public override void _PhysicsProcess(double delta) @@ -19,7 +22,10 @@ public partial class Bullet : Area2D // 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(); + if (body is IDamageable target) + { + // If it does, deal damage! + target.TakeDamage(Damage); + } } } 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"); + 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 + } + } + } } diff --git a/Weapon.cs b/Weapon.cs new file mode 100644 index 0000000..6050cc7 --- /dev/null +++ b/Weapon.cs @@ -0,0 +1,39 @@ +using Godot; + +public partial class Weapon : Node2D +{ + [Export] public PackedScene BulletScene { get; set; } + + // Future-proofing: Different weapons can have different damage multipliers! + [Export] public float DamageMultiplier { get; set; } = 1.0f; + public override void _Process(double delta) + { + LookAt(GetGlobalMousePosition()); + GD.Print("Weapon rotation is running!"); // Add this line + } + public void Fire(Vector2 targetPosition) + { + if (BulletScene == null) return; + + // 1. The weapon itself looks up to find the player's stats component + // (Assuming the weapon is a child of the Player, GetParent() gets the Player) + var statsComponent = GetParent().GetNodeOrNull("StatsComponent"); + + float finalDamage = 10.0f; // Fallback default + + if (statsComponent?.RuntimeStats != null) + { + // 2. The weapon reads the player's base damage and applies its own modifier + finalDamage = statsComponent.RuntimeStats.Damage * DamageMultiplier; + } + + // 3. Instantiate and configure the bullet + Bullet bullet = (Bullet)BulletScene.Instantiate(); + bullet.GlobalPosition = GlobalPosition; // Spawns right where the weapon node is! + bullet.Direction = Vector2.Right.Rotated(GlobalRotation); + bullet.Damage = finalDamage; + + // 4. Add it to the world + GetTree().Root.AddChild(bullet); + } +} \ No newline at end of file diff --git a/Weapon.cs.uid b/Weapon.cs.uid new file mode 100644 index 0000000..2e9a497 --- /dev/null +++ b/Weapon.cs.uid @@ -0,0 +1 @@ +uid://bhmdvsdxc4cnj diff --git a/player.tscn b/player.tscn index 37d6b01..3a99f06 100644 --- a/player.tscn +++ b/player.tscn @@ -5,6 +5,8 @@ [ext_resource type="Script" uid="uid://be81wxmdjuo7v" path="res://StatsComponent.cs" id="3_i3pqv"] [ext_resource type="Texture2D" uid="uid://bebwfbe8hsifr" path="res://2D Pixel Dungeon Asset Pack/Character_animation/priests_idle/priest1/v1/priest1_v1_1.png" id="4_hqtel"] [ext_resource type="Resource" uid="uid://pomj1pryili7" path="res://playerStats.tres" id="4_sweqy"] +[ext_resource type="Script" uid="uid://bhmdvsdxc4cnj" path="res://Weapon.cs" id="6_2hs0m"] +[ext_resource type="Texture2D" uid="uid://k44pw581tslx" path="res://2D Pixel Dungeon Asset Pack/items and trap_animation/torch/torch_1.png" id="7_1jxqw"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_0m7y3"] size = Vector2(15, 14) @@ -29,3 +31,12 @@ position_smoothing_enabled = true [node name="StatsComponent" type="Node" parent="." unique_id=2007335455] script = ExtResource("3_i3pqv") + +[node name="Weapon" type="Node2D" parent="." unique_id=722764434] +script = ExtResource("6_2hs0m") +BulletScene = ExtResource("2_sweqy") + +[node name="Sprite2D" type="Sprite2D" parent="Weapon" unique_id=782327985] +position = Vector2(8, 0) +rotation = 1.5707964 +texture = ExtResource("7_1jxqw") diff --git a/scripts/interfaces/IDamageable.cs b/scripts/interfaces/IDamageable.cs new file mode 100644 index 0000000..e7d6b99 --- /dev/null +++ b/scripts/interfaces/IDamageable.cs @@ -0,0 +1,6 @@ +namespace testing.interfaces; + +public interface IDamageable +{ + void TakeDamage(float amount); +} \ No newline at end of file diff --git a/scripts/interfaces/IDamageable.cs.uid b/scripts/interfaces/IDamageable.cs.uid new file mode 100644 index 0000000..eb1c706 --- /dev/null +++ b/scripts/interfaces/IDamageable.cs.uid @@ -0,0 +1 @@ +uid://behxnbuaav4s diff --git a/scripts/resources/EntityStats.cs b/scripts/resources/EntityStats.cs index 4eebcaf..b32c252 100644 --- a/scripts/resources/EntityStats.cs +++ b/scripts/resources/EntityStats.cs @@ -5,4 +5,6 @@ public partial class EntityStats : Resource { [Export] public float MaxHealth { get; set; } = 100.0f; [Export] public float MoveSpeed { get; set; } = 300.0f; + [Export] public float Damage { get; set; } = 10.0f; + } -- cgit v1.2.3