Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using AC;
- namespace AC.CombatExample
- {
- public class CustomInventory : MonoBehaviour
- {
- #region Variables
- [SerializeField] public int currentlySelectedItemID = 0;
- [SerializeField] private float inputDelay = 0.2f; // Delay before accepting input
- [SerializeField] private int equipIconID = 3;
- [SerializeField] private int unequipIconID = 6;
- [SerializeField] private int combineIconID = 5;
- [SerializeField] private int weaponCategoryID = 1;
- private Menu inventoryMenu;
- private Menu interactionMenu;
- private InventoryItemGO[] spawnedItems;
- private bool isOn;
- private float inputCooldown;
- #endregion
- #region UnityStandards
- private void OnEnable()
- {
- EventManager.OnMenuTurnOn += OnMenuTurnOn;
- EventManager.OnMenuTurnOff += OnMenuTurnOff;
- EventManager.OnInventoryInteract_Alt += OnInventoryInteract;
- }
- private void OnDisable()
- {
- EventManager.OnMenuTurnOn -= OnMenuTurnOn;
- EventManager.OnMenuTurnOff -= OnMenuTurnOff;
- EventManager.OnInventoryInteract_Alt -= OnInventoryInteract;
- }
- private void Start()
- {
- inventoryMenu = PlayerMenus.GetMenuWithName("Inventory");
- interactionMenu = PlayerMenus.GetMenuWithName("Interaction");
- }
- private void Update()
- {
- Menu interactionsMenu = PlayerMenus.GetMenuWithName("Interaction");
- inputCooldown -= Time.unscaledDeltaTime; // Reduce cooldown over time
- //bool inventoryOn = AC.PlayerMenus.GetMenuWithName("Inventory").IsOn();
- //bool interactionOn = AC.PlayerMenus.GetMenuWithName("Interaction").IsOn();
- if (interactionsMenu != null)
- {
- InvInstance foundInstance = GetInventoryInstanceByID(currentlySelectedItemID);
- if (foundInstance != null)
- {
- // Select the correct inventory item for interactions
- //inputCooldown <= 0f &&
- // && inventoryMenu.IsOn()
- if (Input.GetButtonDown("Submit") && interactionMenu.IsOff())
- {
- KickStarter.runtimeInventory.SelectItem(foundInstance);
- interactionMenu.TurnOn();
- interactionMenu.MatchInteractions(foundInstance, true);
- Debug.Log("Interactions menu opened - setting item to: " + foundInstance.InvItem.label);
- }
- }
- else
- {
- Debug.Log("No InvInstance 'foundInstance' found");
- }
- }
- }
- private InvInstance GetInventoryInstanceByID(int id)
- {
- if (KickStarter.runtimeInventory == null)
- {
- Debug.LogError("Runtime Inventory not initialized.");
- return null;
- }
- // Search through player's inventory for an instance of the item
- foreach (InvInstance instance in KickStarter.runtimeInventory.PlayerInvCollection.InvInstances)
- {
- if (instance.InvItem.id == id)
- {
- return instance; // Return the instance, not the InvItem
- }
- }
- return null; // Item not found
- }
- private void OnMenuTurnOn(Menu menu, bool isInstant)
- {
- if (menu == inventoryMenu)
- {
- //KickStarter.runtimeInventory.SetNull();
- isOn = true;
- KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursorAndHotspotLabel;
- SpawnItems();
- inputCooldown = inputDelay; // Set delay when opening inventory
- }
- }
- private void OnMenuTurnOff(Menu menu, bool isInstant)
- {
- if (menu == inventoryMenu)
- {
- isOn = false;
- KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursor;
- foreach (InventoryItemGO spawnedItem in spawnedItems)
- {
- Destroy(spawnedItem.gameObject);
- }
- }
- }
- private void OnInventoryInteract(InvInstance invInstance, int iconID)
- {
- if (iconID == combineIconID)
- {
- // Combine the item
- invInstance.Select();
- }
- else if (invInstance.InvItem.binID == weaponCategoryID)
- {
- // Is a weapon
- if (iconID == equipIconID)
- {
- // Equip the item
- inventoryMenu.TurnOff();
- }
- else if (iconID == unequipIconID)
- {
- // Unequip the item
- inventoryMenu.TurnOff();
- }
- }
- }
- #endregion
- #region PrivateFunctions
- private void SpawnItems()
- {
- spawnedItems = new InventoryItemGO[KickStarter.runtimeInventory.localItems.Count];
- for (int i = 0; i < spawnedItems.Length; i++)
- {
- if (KickStarter.runtimeInventory.localItems[i].linkedPrefab)
- {
- GameObject spawnedItem = Instantiate(KickStarter.runtimeInventory.localItems[i].linkedPrefab, Vector3.zero, Quaternion.identity);
- spawnedItems[i] = spawnedItem.GetComponent<InventoryItemGO>();
- }
- else
- {
- GameObject emptyItem = new GameObject();
- emptyItem.transform.position = Vector3.zero;
- spawnedItems[i] = emptyItem.AddComponent<InventoryItemGO>();
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment