From 9362f66fdfdde318043e46df136247d5b0ccf8db Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 6 May 2025 10:53:49 +0200 Subject: [PATCH] Added weapon switching cooldown --- .../Components/Actors/PlayerWeaponProvider.cs | 34 ++++++++++++++++++- Scripts/Components/FSM/Player/Active.cs | 3 ++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Scripts/Components/Actors/PlayerWeaponProvider.cs b/Scripts/Components/Actors/PlayerWeaponProvider.cs index 87e5acc7..ebe40e53 100644 --- a/Scripts/Components/Actors/PlayerWeaponProvider.cs +++ b/Scripts/Components/Actors/PlayerWeaponProvider.cs @@ -12,9 +12,14 @@ public partial class PlayerWeaponProvider : Node2D { [Export] public PlayerStorageModule StorageModule { get; set; } [Export] public PackedScene WeaponTemplate { get; private set; } + + [Export] public double WeaponSwitchCooldown { get; private set; } = 0.5d; public Array EquippedWeapons { get; set; } = []; private int _currentWeaponIndex = 0; + + private double _switchCooldown = 0d; + private bool _switching = false; private int CurrentWeaponIndex { @@ -22,14 +27,16 @@ public partial class PlayerWeaponProvider : Node2D set { - if (value > EquippedWeapons.Count) + if (value > EquippedWeapons.Count - 1) { _currentWeaponIndex = 0; + return; } if (value < 0) { _currentWeaponIndex = EquippedWeapons.Count - 1; + return; } _currentWeaponIndex = value; @@ -55,6 +62,17 @@ public partial class PlayerWeaponProvider : Node2D EquipStartupWeapon(); } + public void Update(double delta) + { + if (!_switching) return; + _switchCooldown += delta; + if (_switchCooldown >= WeaponSwitchCooldown) + { + _switching = false; + _switchCooldown = 0d; + } + } + private void OnInventoryWeaponEquipped(string itemKey) { Equip(itemKey, true); @@ -94,9 +112,22 @@ public partial class PlayerWeaponProvider : Node2D // Triggered by event in inventorymanager private void EquipWeapon(Weapon weapon) { + if (EquippedWeapon == weapon) + { + return; + } + + // Need to start cooldown + EquippedWeapon?.Hide(); + EquippedWeapon = weapon; CurrentWeaponIndex = EquippedWeapons.IndexOf(weapon); GlobalState.Session.EquippedWeaponId = weapon.WeaponData.ItemKey; + + EquippedWeapon.Show(); + + _switching = true; + _switchCooldown = 0d; } public void NextWeapon() @@ -116,6 +147,7 @@ public partial class PlayerWeaponProvider : Node2D public void Shoot(Vector2 direction) { if (EquippedWeapon == null) return; + if (_switching) return; EquippedWeapon.ShootDirection = direction; EquippedWeapon.Shoot(); diff --git a/Scripts/Components/FSM/Player/Active.cs b/Scripts/Components/FSM/Player/Active.cs index bb5607c1..4d87a9af 100644 --- a/Scripts/Components/FSM/Player/Active.cs +++ b/Scripts/Components/FSM/Player/Active.cs @@ -166,6 +166,9 @@ public partial class Active : PlayerStateBase _animationProvider.SetAnimationSpeed(MainObject.Velocity); _animationProvider.SetAnimation(FacingDirection); + HandleWeaponSwitch(); + _weaponProvider.Update(delta); + //_crosshairProvider.UpdatePosition(FacingDirection); HandleShoot();