using UnityEngine; using UnityEngine.InputSystem; public class WeaponsController : MonoBehaviour { [SerializeField] private Weapon[] weaponSlots; [SerializeField] private Camera playerCamera; [SerializeField] private LayerMask hitMask; private int activeWeaponIndex = 0; private bool isFiring; public Weapon ActiveWeapon => (weaponSlots != null && activeWeaponIndex >= 0 && activeWeaponIndex < weaponSlots.Length) ? weaponSlots[activeWeaponIndex] : null; public int ActiveWeaponIndex => activeWeaponIndex; private void Start() { if (playerCamera == null) { playerCamera = Camera.main; } EquipWeapon(0); } private void Update() { if (isFiring && ActiveWeapon != null && ActiveWeapon.CanShoot()) { ActiveWeapon.Shoot(playerCamera, hitMask); UpdateAmmoUI(); } } // Input System Handlers (supporting both "Fire" and default "Attack" action names) public void OnFire(InputValue value) => isFiring = value.isPressed; public void OnAttack(InputValue value) => isFiring = value.isPressed; public void OnReload(InputValue value) { if (value.isPressed && ActiveWeapon != null) { StartCoroutine(ActiveWeapon.ReloadRoutine()); } } public void OnNextWeapon(InputValue value) { if (value.isPressed) NextWeapon(); } public void OnNext(InputValue value) { if (value.isPressed) NextWeapon(); } public void OnPrevious(InputValue value) { if (value.isPressed) PreviousWeapon(); } public void NextWeapon() { if (weaponSlots == null || weaponSlots.Length <= 1) return; int next = (activeWeaponIndex + 1) % weaponSlots.Length; EquipWeapon(next); } public void PreviousWeapon() { if (weaponSlots == null || weaponSlots.Length <= 1) return; int prev = (activeWeaponIndex - 1 + weaponSlots.Length) % weaponSlots.Length; EquipWeapon(prev); } public void EquipWeapon(int index) { if (weaponSlots == null || weaponSlots.Length == 0) return; if (index < 0 || index >= weaponSlots.Length) return; for (int i = 0; i < weaponSlots.Length; i++) { if (weaponSlots[i] != null) { weaponSlots[i].gameObject.SetActive(i == index); } } activeWeaponIndex = index; UpdateAmmoUI(); } public bool AddAmmoToActiveWeapon(int amount) { if (ActiveWeapon != null) { ActiveWeapon.AddReserveAmmo(amount); UpdateAmmoUI(); return true; } return false; } public bool AddAmmoToMatchingWeapon(string weaponIdentifier, int amount) { if (weaponSlots == null) return false; foreach (var weapon in weaponSlots) { if (weapon != null && (string.IsNullOrEmpty(weaponIdentifier) || weapon.weaponName.ToLower().Contains(weaponIdentifier.ToLower()))) { weapon.AddReserveAmmo(amount); UpdateAmmoUI(); return true; } } return false; } public void UpdateAmmoUI() { if (ActiveWeapon != null && UIController.Instance != null) { UIController.Instance.UpdateAmmoDisplay(ActiveWeapon.currentAmmo, ActiveWeapon.maxAmmo); } } }