using System.Collections; using UnityEngine; public class ActiveInventory : Singleton { private int activeSlotIndexNum = 0; // Tracks the currently selected inventory slot private PlayerControls playerControls; // Manages player input controls protected override void Awake() { base.Awake(); playerControls = new PlayerControls(); // Set up the player input controls } private void Start() { // Listen for keyboard input to switch between inventory slots playerControls.Inventory.Keyboard.performed += ctx => ToggleActiveSlot((int)ctx.ReadValue()); } private void OnEnable() { playerControls.Enable(); // Activate player controls when the script is enabled } public void EquipStartingWeapon() { ToggleActiveHighlight(0); // Equip the initial weapon by highlighting the first slot in the inventory } private void ToggleActiveSlot(int numValue) { ToggleActiveHighlight(numValue - 1); // Change the active slot based on the received input value } private void ToggleActiveHighlight(int indexNum) { activeSlotIndexNum = indexNum; // Update the active slot index // Turn off the highlight for all inventory slots foreach (Transform inventorySlot in transform) { inventorySlot.GetChild(0).gameObject.SetActive(false); // Typically, index 0 refers to the highlight or indicator } // Highlight the selected inventory slot transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true); // Switch the active weapon according to the selected slot ChangeActiveWeapon(); } private void ChangeActiveWeapon() { // If there's a currently active weapon, remove it if (ActiveWeapon.Instance.CurrentActiveWeapon != null) { Destroy(ActiveWeapon.Instance.CurrentActiveWeapon.gameObject); } // Access the selected inventory slot and fetch its weapon information Transform childTransform = transform.GetChild(activeSlotIndexNum); InventorySlot inventorySlot = childTransform.GetComponentInChildren(); WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo(); // If no weapon info is available, set the active weapon to null and exit if (weaponInfo == null) { ActiveWeapon.Instance.WeaponNull(); return; } // Spawn the new weapon and set it as the active one GameObject weaponToSpawn = weaponInfo.weaponPrefab; GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform); ActiveWeapon.Instance.NewWeapon(newWeapon.GetComponent()); } }