공부일지

백업 본문

UnityProjects/Inventory

백업

이하택 2023. 11. 9. 02:21

  • ItemManager.cs
    public class ItemManager : MonoBehaviour
    {
        public static ItemManager Instance;
        private ItemData itemData;
        [SerializeField]
        private TextAsset itemDBJson;
        public Dictionary<int, EquipData> equips = new Dictionary<int, EquipData>();
        public Dictionary<int, CropsData> crops = new Dictionary<int, CropsData>();
        public Dictionary<int, DishData> dishes = new Dictionary<int, DishData>();
        public Dictionary<int, FarmingItemData> farmingConsumes = new Dictionary<int, FarmingItemData>();
        public Dictionary<int, MedicineData> medicines = new Dictionary<int, MedicineData>();
        public Dictionary<int, SeedData> seeds = new Dictionary<int, SeedData>();
        public Dictionary<int, BlueprintData> blueprints = new Dictionary<int, BlueprintData>();
        private void Awake()
        {
            Instance = this;
            itemData = JsonConvert.DeserializeObject<ItemData>(itemDBJson.ToString());
            equips = itemData.equipData.ToDictionary(item => item.id, item => item);
            crops = itemData.cropData.ToDictionary(item => item.id, item => item);
            dishes = itemData.dishData.ToDictionary(item => item.id, item => item);
            farmingConsumes = itemData.farmingItemData.ToDictionary(item => item.id, item => item);
            medicines = itemData.medicineData.ToDictionary(item => item.id, item => item);
            seeds = itemData.seedData.ToDictionary(item => item.id, item => item);
            blueprints = itemData.blueprintData.ToDictionary(item => item.id, item => item);
        }
        //private void Start()
        //{
        //    foreach (var item in equips)
        //    {
        //        Debug.Log($"Item ID: {item.Key}, Name: {item.Value.name}");
        //    }
        //    //Debug.Log(equips[10001]);
        //}
    }
  • hatbar~
    public class Toolbar_UI : MonoBehaviour, IItemStorage
    {
        //Inventory For Blueprints And Seeds
        public static Toolbar_UI Instance;
    
    
        [Header("Slot Info")]
        [SerializeField] private Slot[] hotbarArray;
        [SerializeField] private Transform hotbar_parent;
    
    
        [Header("Selected Item")]
        public int selectedIndex;
    
        [Header("Interact Item")]
        public Transform dropPosition;
    
    
        private void Awake()
        {
            Instance = this;
            hotbarArray = new Slot[9]
            {
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0),
                new Slot(0, 0, 0)
            };
        }
        private void Start()
        {
            AddItem(16000);
            AddItem(15002);
            AddItem(15002);
            AddItem(15002);
            AddItem(15002);
            //시작할 때 핫바
            selectedIndex = 0;
            Debug.Log(UseItem());
            selectedIndex = 1;
            Debug.Log(UseItem());
        }
    
        #region 슬롯 선택
        private void GetInputNumber()
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
                selectedIndex = 0;
            else if (Input.GetKeyDown(KeyCode.Alpha2))
                selectedIndex = 1;
            else if (Input.GetKeyDown(KeyCode.Alpha3))
                selectedIndex = 2;
            else if (Input.GetKeyDown(KeyCode.Alpha4))
                selectedIndex = 3;
            else if (Input.GetKeyDown(KeyCode.Alpha5))
                selectedIndex = 4;
            else if (Input.GetKeyDown(KeyCode.Alpha6))
                selectedIndex = 5;
            else if (Input.GetKeyDown(KeyCode.Alpha7))
                selectedIndex = 6;
            else if (Input.GetKeyDown(KeyCode.Alpha8))
                selectedIndex = 7;
            else if (Input.GetKeyDown(KeyCode.Alpha9))
                selectedIndex = 8;
        }
        #endregion
    
        #region ModifySlot
        public void AddItem(int _id)
        {
            int index = GetItemSlot(_id);
    
            if (hotbarArray[index].curStack < hotbarArray[index].maxStack)
            {
                //add
                hotbarArray[index].curStack++;
            }
            else
            {
                //init slot, 
                index = GetEmptySlot();
                hotbarArray[index].maxStack = GetMaxStack(_id);
                hotbarArray[index].curStack++;
                hotbarArray[index].id = _id;
            }
        }
        public int UseItem()
        {
            //id를 보내줄 생각
            if (hotbarArray[selectedIndex].id == 0)
            {
                //null
                return 0;
            }
            else
            {
                //blueprints
                int temp = hotbarArray[selectedIndex].id;
                RemoveItem();
                return temp;
            }
        }
        public void RemoveItem()
        {
            if (hotbarArray[selectedIndex].id == 0)
            {
                return;
            }
    
            if (hotbarArray[selectedIndex].curStack > 1)
                hotbarArray[selectedIndex].curStack--;
            else if (hotbarArray[selectedIndex].curStack == 1)
            {
                hotbarArray[selectedIndex].curStack--;
                ResetSlot();
            }
        }
        public void ResetSlot()
        {
            hotbarArray[selectedIndex].id = 0;
            hotbarArray[selectedIndex].curStack = 0;
            hotbarArray[selectedIndex].maxStack = 0;
        }
        public int GetMaxStack(int _id)
        {
            if (_id >= 15000 && _id < 16000)
                return ItemManager.Instance.seeds[_id].maxStack;
            else
                return ItemManager.Instance.blueprints[_id].maxStack;
        }
        #endregion
    
        #region FindSlot
        public int GetItemSlot(int _id)    {
            for (int i = 0; i < hotbarArray.Length; i++)
            {
                if (hotbarArray[i].id == _id && hotbarArray[i].curStack < hotbarArray[i].maxStack)
                {
                    return i;
                }
            }
            //MustProcessOverflow
            return 0;
        }
        public int GetEmptySlot()
        {
            for (int i = 0; i < hotbarArray.Length; i++)
            {
                if (hotbarArray[i].id == 0)
                    return i;
            }
            return 0;
        }
        #endregion
    }

  • Slot.cs
    public class Slot
    {
        public int id;
        public int curStack;
        public int maxStack;
        public Slot(int _id, int _curStack, int _maxStack)
        {
            id = _id;
            curStack = _curStack;
            maxStack = _maxStack;
        }
    }
  • Json
    {
      "equipData": [
        {
          "id": 10000,
          "name": "BF_Sword",
          "atk": 10,
          "def": 0
        },
        {
          "id": 10001,
          "name": "BF_Sword",
          "atk": 10,
          "def": 0
        },
        {
          "id": 10002,
          "name": "BF_Sword",
          "atk": 10,
          "def": 0
        }
      ],
      "cropData": [
        {
          "id": 11000,
          "name": "Apple",
          "maxStack": 64
        },
        {
          "id": 11001,
          "name": "Tomato",
          "maxStack": 64
        },
        {
          "id": 11002,
          "name": "Banana",
          "maxStack": 64
        }
      ],
      "dishData": [
        {
          "id": 12000,
          "name": "soup",
          "protein": 30,
          "maxStack": 64
        },
        {
          "id": 12001,
          "name": "bulgogi",
          "protein": 30,
          "maxStack": 64
        },
        {
          "id": 12002,
          "name": "bibimbab",
          "protein": 30,
          "maxStack": 64
        }
      ],
      "farmingItemData": [
        {
          "id": 13000,
          "name": "weed",
          "dicrease": 30,
          "maxStack": 64
        },
        {
          "id": 13001,
          "name": "insect",
          "dicrease": 30,
          "maxStack": 64
        },
        {
          "id": 13002,
          "name": "fertility",
          "dicrease": 30,
          "maxStack": 64
        }
      ],
      "medicineData": [
        {
          "id": 14000,
          "name": "potion",
          "heal": 30,
          "maxStack": 64
        },
        {
          "id": 14001,
          "name": "middlepotion",
          "heal": 30,
          "maxStack": 64
        },
        {
          "id": 14002,
          "name": "highpotion",
          "heal": 30,
          "maxStack": 64
        }
      ],
      "seedData": [
        {
          "id": 15000,
          "name": "CarrotSeed",
          "maxStack": 64
        },
        {
          "id": 15001,
          "name": "AppleSeed",
          "maxStack": 64
        },
        {
          "id": 15002,
          "name": "BananaSeed",
          "maxStack": 64
        }
      ],
      "blueprintData": [
        {
          "id": 16000,
          "name": "potion",
          "maxStack": 64
        },
        {
          "id": 16001,
          "name": "middlepotion",
          "maxStack": 64
        },
        {
          "id": 16002,
          "name": "highpotion",
          "maxStack": 64
        }
      ]
    }
  • ItemData
    [Serializable]
    public class ItemData
    {
        public EquipData[] equipData;
        public CropsData[] cropData;
        public DishData[] dishData;
        public FarmingItemData[] farmingItemData;
        public MedicineData[] medicineData;
        public SeedData[] seedData;
        public BlueprintData[] blueprintData;
    }

  • 같은 종류 열매랑 씨앗을 -4000설정


Uploaded by N2T

'UnityProjects > Inventory' 카테고리의 다른 글

코드 리뷰  (2) 2023.11.20
상세 아이템 Data 작성  (1) 2023.11.16
질문록  (1) 2023.11.15
중간 발표와 피드백  (1) 2023.11.14
아이작 모작 팀 프로젝트 회고(작성중)  (0) 2023.10.18