Added weapon switching cooldown

This commit is contained in:
Marco 2025-05-06 10:53:49 +02:00
commit 9362f66fdf
2 changed files with 36 additions and 1 deletions

View file

@ -12,9 +12,14 @@ public partial class PlayerWeaponProvider : Node2D
{ {
[Export] public PlayerStorageModule StorageModule { get; set; } [Export] public PlayerStorageModule StorageModule { get; set; }
[Export] public PackedScene WeaponTemplate { get; private set; } [Export] public PackedScene WeaponTemplate { get; private set; }
[Export] public double WeaponSwitchCooldown { get; private set; } = 0.5d;
public Array<Weapon> EquippedWeapons { get; set; } = []; public Array<Weapon> EquippedWeapons { get; set; } = [];
private int _currentWeaponIndex = 0; private int _currentWeaponIndex = 0;
private double _switchCooldown = 0d;
private bool _switching = false;
private int CurrentWeaponIndex private int CurrentWeaponIndex
{ {
@ -22,14 +27,16 @@ public partial class PlayerWeaponProvider : Node2D
set set
{ {
if (value > EquippedWeapons.Count) if (value > EquippedWeapons.Count - 1)
{ {
_currentWeaponIndex = 0; _currentWeaponIndex = 0;
return;
} }
if (value < 0) if (value < 0)
{ {
_currentWeaponIndex = EquippedWeapons.Count - 1; _currentWeaponIndex = EquippedWeapons.Count - 1;
return;
} }
_currentWeaponIndex = value; _currentWeaponIndex = value;
@ -55,6 +62,17 @@ public partial class PlayerWeaponProvider : Node2D
EquipStartupWeapon(); EquipStartupWeapon();
} }
public void Update(double delta)
{
if (!_switching) return;
_switchCooldown += delta;
if (_switchCooldown >= WeaponSwitchCooldown)
{
_switching = false;
_switchCooldown = 0d;
}
}
private void OnInventoryWeaponEquipped(string itemKey) private void OnInventoryWeaponEquipped(string itemKey)
{ {
Equip(itemKey, true); Equip(itemKey, true);
@ -94,9 +112,22 @@ public partial class PlayerWeaponProvider : Node2D
// Triggered by event in inventorymanager // Triggered by event in inventorymanager
private void EquipWeapon(Weapon weapon) private void EquipWeapon(Weapon weapon)
{ {
if (EquippedWeapon == weapon)
{
return;
}
// Need to start cooldown
EquippedWeapon?.Hide();
EquippedWeapon = weapon; EquippedWeapon = weapon;
CurrentWeaponIndex = EquippedWeapons.IndexOf(weapon); CurrentWeaponIndex = EquippedWeapons.IndexOf(weapon);
GlobalState.Session.EquippedWeaponId = weapon.WeaponData.ItemKey; GlobalState.Session.EquippedWeaponId = weapon.WeaponData.ItemKey;
EquippedWeapon.Show();
_switching = true;
_switchCooldown = 0d;
} }
public void NextWeapon() public void NextWeapon()
@ -116,6 +147,7 @@ public partial class PlayerWeaponProvider : Node2D
public void Shoot(Vector2 direction) public void Shoot(Vector2 direction)
{ {
if (EquippedWeapon == null) return; if (EquippedWeapon == null) return;
if (_switching) return;
EquippedWeapon.ShootDirection = direction; EquippedWeapon.ShootDirection = direction;
EquippedWeapon.Shoot(); EquippedWeapon.Shoot();

View file

@ -166,6 +166,9 @@ public partial class Active : PlayerStateBase
_animationProvider.SetAnimationSpeed(MainObject.Velocity); _animationProvider.SetAnimationSpeed(MainObject.Velocity);
_animationProvider.SetAnimation(FacingDirection); _animationProvider.SetAnimation(FacingDirection);
HandleWeaponSwitch();
_weaponProvider.Update(delta);
//_crosshairProvider.UpdatePosition(FacingDirection); //_crosshairProvider.UpdatePosition(FacingDirection);
HandleShoot(); HandleShoot();