diff options
| -rw-r--r-- | .idea/.idea.testing/.idea/.name | 1 | ||||
| -rw-r--r-- | .idea/.idea.testing/.idea/indexLayout.xml | 8 | ||||
| -rw-r--r-- | .idea/.idea.testing/.idea/libraries/GdSdk.xml | 9 | ||||
| -rw-r--r-- | .idea/.idea.testing/.idea/vcs.xml | 7 | ||||
| -rw-r--r-- | Bullet.cs | 12 | ||||
| -rw-r--r-- | Player.cs | 54 | ||||
| -rw-r--r-- | Weapon.cs | 39 | ||||
| -rw-r--r-- | Weapon.cs.uid | 1 | ||||
| -rw-r--r-- | player.tscn | 11 | ||||
| -rw-r--r-- | scripts/interfaces/IDamageable.cs | 6 | ||||
| -rw-r--r-- | scripts/interfaces/IDamageable.cs.uid | 1 | ||||
| -rw-r--r-- | scripts/resources/EntityStats.cs | 2 |
12 files changed, 122 insertions, 29 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="UserContentModel"> + <attachedFolders /> + <explicitIncludes /> + <explicitExcludes /> + </component> +</project>
\ 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 @@ +<component name="libraryTable"> + <library name="GdSdk" type="GdScript"> + <CLASSES /> + <JAVADOC /> + <SOURCES> + <root url="file://$APPLICATION_PLUGINS_DIR$/GdScript/extracted/Master" /> + </SOURCES> + </library> +</component>
\ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project>
\ No newline at end of file @@ -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); + } } } @@ -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 + } + } + } } 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>("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; + } |
