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 PackedScene WeaponTemplate { get; private set; }
[Export] public double WeaponSwitchCooldown { get; private set; } = 0.5d;
public Array<Weapon> 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();

View file

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