mmorpg/Assets/InventoryManager.cs
2024-07-19 17:25:25 +05:30

71 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
using Assets.HeroEditor4D.InventorySystem.Scripts.Elements;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public InventorySlot[] inventorySlots;
public List<InventoryItemEntry> entries = new List<InventoryItemEntry>();
public GameObject ItemInventoryPrefab;
public List<item> startingItems;
void Start(){
foreach(item i in startingItems){
AddInvItem(i);
}
UpdateEntries();
}
void UpdateEntries(){
entries = new List<InventoryItemEntry>();
for(int i=0; i < inventorySlots.Length; i++){
ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
if(itemInSlot!= null){
entries.Add(new InventoryItemEntry(){slotIndex = i, lootType=itemInSlot.item.name ?? "null"});
}
}
}
public void AddInvItem (item item){
//find an empty slot
for(int i = 0 ; i < inventorySlots.Length ; i++){
InventorySlot slot = inventorySlots[i];
ItemInventory itemInSlot = slot.GetComponentInChildren<ItemInventory>();
if(itemInSlot == null){
SpawnNewItem(item , slot);
return;
}
//implement check for slot full
}
}
public void Clear(){
for(int i=0; i < inventorySlots.Length; i++){
if(inventorySlots[i].transform.childCount > 0){
Destroy(inventorySlots[i].transform.GetChild(0).gameObject);
}
}
}
void SpawnNewItem(item item , InventorySlot slot ){
GameObject newItemAdd = Instantiate(ItemInventoryPrefab , slot.transform);
ItemInventory inventoryItemm = newItemAdd.GetComponent<ItemInventory>();
inventoryItemm.InitializeNewItem(item);
}
}
[System.Serializable]
public class InventoryItemEntry{
public int slotIndex;
public string lootType;
}