using System.Collections; using UnityEngine; public class ActiveInventory : Singleton { private int activeSlotIndexNum = 0; // Index number of the currently active inventory slot private PlayerControls playerControls; // Player input controls protected override void Awake() { base.Awake(); playerControls = new PlayerControls(); // Initialize player controls } private void Start() { // Subscribe to the keyboard input event for toggling active slots playerControls.Inventory.Keyboard.performed += ctx => ToggleActiveSlot((int)ctx.ReadValue()); } private void OnEnable() { playerControls.Enable(); // Enable player controls when this script is enabled } public void EquipStartingWeapon() { ToggleActiveHighlight(0); // Equip the starting weapon by highlighting the first inventory slot } private void ToggleActiveSlot(int numValue) { ToggleActiveHighlight(numValue - 1); // Toggle active highlight based on the numeric value received from input } private void ToggleActiveHighlight(int indexNum) { activeSlotIndexNum = indexNum; // Set the active slot index number // Deactivate highlight for all inventory slots foreach (Transform inventorySlot in transform) { inventorySlot.GetChild(0).gameObject.SetActive(false); // Assuming index 0 is the highlight or indicator } // Activate highlight for the selected inventory slot transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true); // Change the active weapon based on the selected inventory slot ChangeActiveWeapon(); } private void ChangeActiveWeapon() { // Destroy the current active weapon if it exists if (ActiveWeapon.Instance.CurrentActiveWeapon != null) { Destroy(ActiveWeapon.Instance.CurrentActiveWeapon.gameObject); } // Get the selected inventory slot and retrieve the weapon info Transform childTransform = transform.GetChild(activeSlotIndexNum); InventorySlot inventorySlot = childTransform.GetComponentInChildren(); WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo(); // If no weapon info is found, set the active weapon to null and return if (weaponInfo == null) { ActiveWeapon.Instance.WeaponNull(); return; } // Instantiate the new weapon prefab and assign it as the active weapon GameObject weaponToSpawn = weaponInfo.weaponPrefab; GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform); ActiveWeapon.Instance.NewWeapon(newWeapon.GetComponent()); } }