diff options
| author | alex <[email protected]> | 2026-07-25 17:14:11 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-25 17:14:11 +0200 |
| commit | a6e7c49a162ada8ec0883fccb1da7313b03ba2ec (patch) | |
| tree | 2e374c703ea3591e9f7053477bb1135fe608f58f | |
| parent | 023c4f003d58c27cd2677307abe2c65bb09c2681 (diff) | |
| download | godot-testing-main.tar.xz godot-testing-main.zip | |
| -rw-r--r-- | Player.cs | 12 | ||||
| -rw-r--r-- | Weapon.cs | 2 | ||||
| -rw-r--r-- | player.tscn | 16 | ||||
| -rw-r--r-- | project.godot | 10 | ||||
| -rw-r--r-- | scripts/WeaponHolder.cs | 43 | ||||
| -rw-r--r-- | scripts/WeaponHolder.cs.uid | 1 | ||||
| -rw-r--r-- | weapons/something.tscn | 14 | ||||
| -rw-r--r-- | weapons/torch.tscn | 14 |
8 files changed, 89 insertions, 23 deletions
@@ -33,18 +33,6 @@ public partial class Player : CharacterBody2D, IDamageable _sprite.FlipH = mousePos.X < GlobalPosition.X; } - public override void _UnhandledInput(InputEvent @event) - { - if (@event.IsActionPressed("shoot")) - { - // Find the weapon node and tell it to fire at the mouse - var weapon = GetNodeOrNull<Weapon>("Weapon"); - if (weapon != null) - { - weapon.Fire(GetGlobalMousePosition()); - } - } - } public void TakeDamage(float amount) { // For right now, just subtract it from the runtime stats we set up earlier! @@ -17,7 +17,7 @@ public partial class Weapon : Node2D // 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"); + var statsComponent = GetParent().GetParent().GetNodeOrNull<StatsComponent>("StatsComponent"); float finalDamage = 10.0f; // Fallback default diff --git a/player.tscn b/player.tscn index 3a99f06..33e05c5 100644 --- a/player.tscn +++ b/player.tscn @@ -5,8 +5,9 @@ [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"] +[ext_resource type="PackedScene" uid="uid://ctmfmhg8v8msi" path="res://weapons/torch.tscn" id="6_1jxqw"] +[ext_resource type="Script" uid="uid://bfqeoell73w34" path="res://scripts/WeaponHolder.cs" id="7_dw050"] +[ext_resource type="PackedScene" uid="uid://bywl6wp6gtekw" path="res://weapons/something.tscn" id="8_dw050"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_0m7y3"] size = Vector2(15, 14) @@ -32,11 +33,6 @@ 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") +[node name="Node2D" type="Node2D" parent="." unique_id=1010399635] +script = ExtResource("7_dw050") +WeaponScenes = Array[PackedScene]([ExtResource("8_dw050"), ExtResource("6_1jxqw")]) diff --git a/project.godot b/project.godot index 425feb4..2d4f4c8 100644 --- a/project.godot +++ b/project.godot @@ -34,6 +34,16 @@ shoot={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) ] } +weapon_1={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":16,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":49,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} +weapon_2={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":16,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":50,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} [layer_names] diff --git a/scripts/WeaponHolder.cs b/scripts/WeaponHolder.cs new file mode 100644 index 0000000..210be60 --- /dev/null +++ b/scripts/WeaponHolder.cs @@ -0,0 +1,43 @@ +using Godot; + +public partial class WeaponHolder : Node2D +{ + [Export] public PackedScene[] WeaponScenes { get; set; } + + private Weapon _currentWeapon; + + public override void _Ready() + { + // Equip the first weapon automatically + EquipWeapon(0); + } + + public void EquipWeapon(int index) + { + if (WeaponScenes == null || WeaponScenes.Length <= index || WeaponScenes[index] == null) + return; + + if (_currentWeapon != null) + { + _currentWeapon.QueueFree(); + } + + _currentWeapon = WeaponScenes[index].Instantiate<Weapon>(); + AddChild(_currentWeapon); + + GD.Print($"WeaponHolder equipped weapon index: {index}"); + } + + public override void _UnhandledInput(InputEvent @event) + { + // Handle shooting + if (@event.IsActionPressed("shoot") && _currentWeapon != null) + { + _currentWeapon.Fire(GetGlobalMousePosition()); + } + + // Handle swapping + if (@event.IsActionPressed("weapon_1")) EquipWeapon(0); + else if (@event.IsActionPressed("weapon_2")) EquipWeapon(1); + } +}
\ No newline at end of file diff --git a/scripts/WeaponHolder.cs.uid b/scripts/WeaponHolder.cs.uid new file mode 100644 index 0000000..089d4a7 --- /dev/null +++ b/scripts/WeaponHolder.cs.uid @@ -0,0 +1 @@ +uid://bfqeoell73w34 diff --git a/weapons/something.tscn b/weapons/something.tscn new file mode 100644 index 0000000..93ea0cf --- /dev/null +++ b/weapons/something.tscn @@ -0,0 +1,14 @@ +[gd_scene format=3 uid="uid://bywl6wp6gtekw"] + +[ext_resource type="Script" uid="uid://bhmdvsdxc4cnj" path="res://Weapon.cs" id="1_1d5wo"] +[ext_resource type="PackedScene" uid="uid://cchdwhewroga8" path="res://Bullet.tscn" id="2_faifk"] +[ext_resource type="Texture2D" uid="uid://631urrob2cwi" path="res://2D Pixel Dungeon Asset Pack/items and trap_animation/keys/keys_1_3.png" id="3_1d5wo"] + +[node name="Weapon" type="Node2D" unique_id=1330410444] +script = ExtResource("1_1d5wo") +BulletScene = ExtResource("2_faifk") +DamageMultiplier = 100.0 + +[node name="Sprite2D" type="Sprite2D" parent="." unique_id=962000068] +position = Vector2(8, 0) +texture = ExtResource("3_1d5wo") diff --git a/weapons/torch.tscn b/weapons/torch.tscn new file mode 100644 index 0000000..39bb963 --- /dev/null +++ b/weapons/torch.tscn @@ -0,0 +1,14 @@ +[gd_scene format=3 uid="uid://ctmfmhg8v8msi"] + +[ext_resource type="Script" uid="uid://bhmdvsdxc4cnj" path="res://Weapon.cs" id="1_brm8f"] +[ext_resource type="PackedScene" uid="uid://cchdwhewroga8" path="res://Bullet.tscn" id="2_e1en3"] +[ext_resource type="Texture2D" uid="uid://k44pw581tslx" path="res://2D Pixel Dungeon Asset Pack/items and trap_animation/torch/torch_1.png" id="3_objab"] + +[node name="Weapon" type="Node2D" unique_id=1330410444] +script = ExtResource("1_brm8f") +BulletScene = ExtResource("2_e1en3") + +[node name="Sprite2D" type="Sprite2D" parent="." unique_id=962000068] +position = Vector2(8, 0) +rotation = 1.5707964 +texture = ExtResource("3_objab") |
