Guest User

CustomInventory

a guest
Jan 31st, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using UnityEngine;
  2. using AC;
  3.  
  4. namespace AC.CombatExample
  5. {
  6.     public class CustomInventory : MonoBehaviour
  7.     {
  8.         #region Variables
  9.  
  10.         [SerializeField] public int currentlySelectedItemID = 0;
  11.         [SerializeField] private float inputDelay = 0.2f; // Delay before accepting input
  12.         [SerializeField] private int equipIconID = 3;
  13.         [SerializeField] private int unequipIconID = 6;
  14.         [SerializeField] private int combineIconID = 5;
  15.         [SerializeField] private int weaponCategoryID = 1;
  16.  
  17.         private Menu inventoryMenu;
  18.         private Menu interactionMenu;
  19.         private InventoryItemGO[] spawnedItems;
  20.         private bool isOn;
  21.         private float inputCooldown;
  22.  
  23.         #endregion
  24.  
  25.         #region UnityStandards
  26.  
  27.         private void OnEnable()
  28.         {
  29.             EventManager.OnMenuTurnOn += OnMenuTurnOn;
  30.             EventManager.OnMenuTurnOff += OnMenuTurnOff;
  31.             EventManager.OnInventoryInteract_Alt += OnInventoryInteract;
  32.         }
  33.  
  34.         private void OnDisable()
  35.         {
  36.             EventManager.OnMenuTurnOn -= OnMenuTurnOn;
  37.             EventManager.OnMenuTurnOff -= OnMenuTurnOff;
  38.             EventManager.OnInventoryInteract_Alt -= OnInventoryInteract;
  39.         }
  40.  
  41.         private void Start()
  42.         {
  43.             inventoryMenu = PlayerMenus.GetMenuWithName("Inventory");
  44.             interactionMenu = PlayerMenus.GetMenuWithName("Interaction");
  45.         }
  46.  
  47.         private void Update()
  48.         {
  49.             Menu interactionsMenu = PlayerMenus.GetMenuWithName("Interaction");
  50.             inputCooldown -= Time.unscaledDeltaTime; // Reduce cooldown over time
  51.             //bool inventoryOn = AC.PlayerMenus.GetMenuWithName("Inventory").IsOn();
  52.             //bool interactionOn = AC.PlayerMenus.GetMenuWithName("Interaction").IsOn();
  53.  
  54.             if (interactionsMenu != null)
  55.             {
  56.                 InvInstance foundInstance = GetInventoryInstanceByID(currentlySelectedItemID);
  57.                 if (foundInstance != null)
  58.                 {
  59.                     // Select the correct inventory item for interactions
  60.                     //inputCooldown <= 0f &&
  61.                     // && inventoryMenu.IsOn()
  62.  
  63.                     if (Input.GetButtonDown("Submit") && interactionMenu.IsOff())
  64.                     {
  65.                         KickStarter.runtimeInventory.SelectItem(foundInstance);
  66.                         interactionMenu.TurnOn();
  67.                         interactionMenu.MatchInteractions(foundInstance, true);
  68.                         Debug.Log("Interactions menu opened - setting item to: " + foundInstance.InvItem.label);
  69.                     }
  70.                 }
  71.                 else
  72.                 {
  73.                     Debug.Log("No InvInstance 'foundInstance' found");
  74.                 }
  75.             }
  76.         }
  77.  
  78.         private InvInstance GetInventoryInstanceByID(int id)
  79.         {
  80.             if (KickStarter.runtimeInventory == null)
  81.             {
  82.                 Debug.LogError("Runtime Inventory not initialized.");
  83.                 return null;
  84.             }
  85.  
  86.             // Search through player's inventory for an instance of the item
  87.             foreach (InvInstance instance in KickStarter.runtimeInventory.PlayerInvCollection.InvInstances)
  88.             {
  89.                 if (instance.InvItem.id == id)
  90.                 {
  91.                     return instance; // Return the instance, not the InvItem
  92.                 }
  93.             }
  94.  
  95.             return null; // Item not found
  96.         }
  97.  
  98.         private void OnMenuTurnOn(Menu menu, bool isInstant)
  99.         {
  100.             if (menu == inventoryMenu)
  101.             {
  102.                 //KickStarter.runtimeInventory.SetNull();
  103.                 isOn = true;
  104.                 KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursorAndHotspotLabel;
  105.                 SpawnItems();
  106.  
  107.                 inputCooldown = inputDelay; // Set delay when opening inventory
  108.             }
  109.         }
  110.  
  111.         private void OnMenuTurnOff(Menu menu, bool isInstant)
  112.         {
  113.             if (menu == inventoryMenu)
  114.             {
  115.                 isOn = false;
  116.                 KickStarter.cursorManager.inventoryHandling = InventoryHandling.ChangeCursor;
  117.  
  118.                 foreach (InventoryItemGO spawnedItem in spawnedItems)
  119.                 {
  120.                     Destroy(spawnedItem.gameObject);
  121.                 }
  122.             }
  123.         }
  124.  
  125.         private void OnInventoryInteract(InvInstance invInstance, int iconID)
  126.         {
  127.             if (iconID == combineIconID)
  128.             {
  129.                 // Combine the item
  130.                 invInstance.Select();
  131.             }
  132.             else if (invInstance.InvItem.binID == weaponCategoryID)
  133.             {
  134.                 // Is a weapon
  135.                 if (iconID == equipIconID)
  136.                 {
  137.                     // Equip the item
  138.                     inventoryMenu.TurnOff();
  139.                 }
  140.                 else if (iconID == unequipIconID)
  141.                 {
  142.                     // Unequip the item
  143.                     inventoryMenu.TurnOff();
  144.                 }
  145.             }
  146.         }
  147.  
  148.         #endregion
  149.  
  150.         #region PrivateFunctions
  151.  
  152.         private void SpawnItems()
  153.         {
  154.             spawnedItems = new InventoryItemGO[KickStarter.runtimeInventory.localItems.Count];
  155.  
  156.             for (int i = 0; i < spawnedItems.Length; i++)
  157.             {
  158.                 if (KickStarter.runtimeInventory.localItems[i].linkedPrefab)
  159.                 {
  160.                     GameObject spawnedItem = Instantiate(KickStarter.runtimeInventory.localItems[i].linkedPrefab, Vector3.zero, Quaternion.identity);
  161.                     spawnedItems[i] = spawnedItem.GetComponent<InventoryItemGO>();
  162.                 }
  163.                 else
  164.                 {
  165.                     GameObject emptyItem = new GameObject();
  166.                     emptyItem.transform.position = Vector3.zero;
  167.                     spawnedItems[i] = emptyItem.AddComponent<InventoryItemGO>();
  168.                 }
  169.             }
  170.         }
  171.  
  172.         #endregion
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment