GoodNoodle

Inventory

Jun 25th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7.  
  8. public enum ItemCatagory { Items, Capsules, Tms}
  9. public class Inventory : MonoBehaviour, ISavable
  10. {
  11.     [SerializeField] List<ItemSlot> slots;
  12.     [SerializeField] List<ItemSlot> capsuleSlots;
  13.     [SerializeField] List<ItemSlot> tmSlots;
  14.  
  15.     List<List<ItemSlot>> allSlots;
  16.  
  17.     public event Action OnUpdated;
  18.  
  19.  
  20.    
  21.  
  22.     private void Awake()
  23.     {
  24.         allSlots = new List<List<ItemSlot>>() { slots, capsuleSlots, tmSlots };
  25.     }
  26.     public static List<string> ItemCatagories { get; set; } = new List<string>()
  27.     {
  28.         "Items", "Capsules", "Tms & Hms"
  29.  
  30.     };
  31.  
  32.    
  33.  
  34.     public List<ItemSlot> GetSlotsByCatagory(int catagoryIndex)
  35.     {
  36.         return allSlots[catagoryIndex];
  37.     }
  38.  
  39.     public ItemBase GetItem(int itemIndex, int categoryIndex)
  40.     {
  41.         var currenSlots = GetSlotsByCatagory(categoryIndex);
  42.         return currenSlots[itemIndex].Item;
  43.     }
  44.  
  45.     public ItemBase UseItem(int itemIndex, Creature selectedCreature, int selectedCatagory)
  46.     {
  47.         var item = GetItem(itemIndex, selectedCatagory);
  48.         return UseItem(item, selectedCreature);
  49.     }
  50.  
  51.     public ItemBase UseItem(ItemBase item, Creature selectedCreature)
  52.     {
  53.        
  54.         bool itemused = item.Use(selectedCreature);
  55.  
  56.         if (itemused)
  57.         {
  58.             if (!item.IsReusable)
  59.                 RemoveItem(item);
  60.  
  61.             return item;
  62.         }
  63.         return null;
  64.     }
  65.  
  66.     public void AddItem(ItemBase item, int count = 1)
  67.     {
  68.        int catagory = (int)GetCatagoryFromItem(item);
  69.        var currSlots = GetSlotsByCatagory(catagory);
  70.  
  71.       var itemslot =  currSlots.FirstOrDefault(slot => slot.Item == item);
  72.         if (itemslot != null)
  73.         {
  74.             itemslot.Count += count;
  75.         }
  76.         else
  77.         {
  78.             currSlots.Add(new ItemSlot()
  79.             {
  80.                 Item = item,
  81.                 Count = count
  82.             });
  83.         }
  84.  
  85.  
  86.         OnUpdated?.Invoke();
  87.     }
  88.  
  89.     public int GetItemCount(ItemBase item)
  90.     {
  91.        
  92.             int catagory = (int)GetCatagoryFromItem(item);
  93.             var currSlots = GetSlotsByCatagory(catagory);
  94.  
  95.             var itemslot = currSlots.FirstOrDefault(slot => slot.Item == item);
  96.  
  97.         if (itemslot != null)
  98.            return itemslot.Count;
  99.         else
  100.             return 0;
  101.     }
  102.  
  103.     public bool HasItem( ItemBase item)
  104.     {
  105.         int catagory = (int)GetCatagoryFromItem(item);
  106.         var currSlots = GetSlotsByCatagory(catagory);
  107.  
  108.         return currSlots.Exists(slot => slot.Item == item);    
  109.     }
  110.            
  111.    
  112.  
  113.     public void RemoveItem(ItemBase item, int countToRemove = 1)
  114.     {
  115.         int catagory = (int)GetCatagoryFromItem(item);
  116.         var currSlots = GetSlotsByCatagory(catagory);
  117.  
  118.         var itemSlot = currSlots.First(slots => slots.Item == item);
  119.         itemSlot.Count -= countToRemove;
  120.  
  121.         if(itemSlot.Count == 0)
  122.             currSlots.Remove(itemSlot);
  123.  
  124.         OnUpdated?.Invoke();
  125.     }
  126.  
  127.     ItemCatagory GetCatagoryFromItem(ItemBase item)
  128.     {
  129.         if (item is RecoveryItem || item is EvolutionItem)
  130.             return ItemCatagory.Items;
  131.         else if (item is CapsuleItem)
  132.             return ItemCatagory.Capsules;
  133.         else
  134.             return ItemCatagory.Tms;
  135.     }
  136.     public static Inventory GetInventory()
  137.     {
  138.        return FindObjectOfType<PlayerController>().GetComponent<Inventory>();
  139.     }
  140.  
  141.     public object CaptureState()
  142.     {
  143.         var savedata = new InventorySaveData()
  144.         {
  145.             items = slots.Select(i => i.GetSaveData()).ToList(),
  146.             capsules = capsuleSlots.Select(i => i.GetSaveData()).ToList(),
  147.             tms = tmSlots.Select(i => i.GetSaveData()).ToList(),
  148.  
  149.            
  150.         };
  151.  
  152.         return savedata;
  153.     }
  154.  
  155.     public void RestoreState(object state)
  156.     {
  157.         var saveData = state as InventorySaveData;
  158.  
  159.         slots = saveData.items.Select(i => new ItemSlot(i)).ToList();
  160.         capsuleSlots = saveData.capsules.Select(i => new ItemSlot(i)).ToList();
  161.         tmSlots = saveData.tms.Select(i => new ItemSlot(i)).ToList();
  162.  
  163.        
  164.  
  165.         allSlots = new List<List<ItemSlot>>() { slots, capsuleSlots, tmSlots };
  166.  
  167.         OnUpdated?.Invoke();
  168.     }
  169. }
  170.  
  171. [Serializable]
  172. public class ItemSlot
  173. {
  174.     [SerializeField] ItemBase item;
  175.     [SerializeField] int count;
  176.  
  177.     public ItemSlot()
  178.     {
  179.  
  180.     }
  181.     public ItemSlot(ItemSaveData savedata)
  182.     {
  183.         item = ItemDB.GetObjectbyName(savedata.name);
  184.         count = savedata.count;
  185.     }
  186.     public ItemSaveData GetSaveData()
  187.     {
  188.         var saveData = new ItemSaveData()
  189.         {
  190.             name = item.name,
  191.             count = count
  192.         };
  193.  
  194.         return saveData;
  195.     }
  196.  
  197.     public ItemBase Item {
  198.          get => item;
  199.         set => item = value;
  200.  
  201.     }
  202.  
  203.     public int Count {
  204.         get => count;
  205.         set => count = value;
  206.     }
  207. }
  208.  
  209.  
  210. [Serializable]
  211. public class ItemSaveData
  212. {
  213.     public string name;
  214.     public int count;
  215. }
  216.  
  217. [Serializable]
  218. public class InventorySaveData
  219. {
  220.     public List<ItemSaveData> items;
  221.     public List<ItemSaveData> capsules;
  222.     public List<ItemSaveData> tms;
  223. }
  224.  
Add Comment
Please, Sign In to add comment