From a6e7c49a162ada8ec0883fccb1da7313b03ba2ec Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 25 Jul 2026 17:14:11 +0200 Subject: weapons are swappable now --- scripts/WeaponHolder.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ scripts/WeaponHolder.cs.uid | 1 + 2 files changed, 44 insertions(+) create mode 100644 scripts/WeaponHolder.cs create mode 100644 scripts/WeaponHolder.cs.uid (limited to 'scripts') 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(); + 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 -- cgit v1.2.3