Project/Alice Project

Inventory make

Red_Horse 2022. 2. 13. 02:02

인벤토리 키고 끄기 Flow

인벤토리 Flow

 
void Update()
{
check_input();
}
void check_input()
{
if (Button.AButton)
{
if (inventory.activeSelf == false)
inventory.SetActive(true);
else
inventory.SetActive(false);
}​

 

1. Update 메서드를 활용하여 Controller의 입력값의 변화를 감지하여 실행

2. Inventory의 현재 상태를 확인하여 활성화, 비활성화

 

 

 

아이템 넣기 Flow

public InventoryObject inventory;
public void OnTriggerEnter(Collider other)
{
var item = other.GetComponent<GroundItem>();
if(item)
{
inventory.AddItem(new Item(item.item), 1);
Destroy(other.gameObject);
}
}​
 
1. Collider 충돌을 인식 후 해당 오브젝트의 GroundItem의 유무에 따라 아이템 추가를 진행한다.
 
 
 
 
 
 
 
 
 
inventory.AddItem(new Item(item.item), 1); 실행 설명.
public string savePath;
public ItemDatabaseObject database;
public Inventory Container;
public void AddItem(Item _item, int _amount)
{
for(int i = 0; i<Container.Items.Length; i++)
{
if(Container.Items[i].ID == _item.Id)
{
Container.Items[i].AddAmount(_amount);
return;
}
}
SetEmptySlot(_item, _amount);
}

1. Inventory의 길이만큼 반복문 실행

 - Inventory에 동일한 ID를 가지고 있는 Item이 있을시 개수만 추가하고 종료

 - Inventory에 동일한 ID가 없을시 Item 추가 

 

 

 

SetEmptySlot 설명

public InventorySlot SetEmptySlot(Item _item, int _amount)
    {
        for(int i = 0; i < Container.Items.Length; i++)
        {
            if(Container.Items[i].ID <= -1)
            {
                Container.Items[i].UpdateSlot(_item.Id, _item, _amount);
                return Container.Items[i];
            }
        }
        return null;
    }

1. Item의 이름이 -일 경우 해당 슬롯을 비우기

 

 

 

 

아이템 꺼내기 Flow

 

    Dictionary<GameObject, InventorySlot> itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
    
    void Awake ()
    {
        CreateSlots();
    }
    public void CreateSlots()
    {
        itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
        for (int i = 0; i < inventory.Container.Items.Length; i++)
        {
            var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
            obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
            AddEvent(obj, EventTriggerType.PointerEnter, delegate { OnEnter(obj); });
            AddEvent(obj, EventTriggerType.PointerExit, delegate { OnExit(obj); });
            AddEvent(obj, EventTriggerType.PointerClick, delegate { OnDragEnd(obj); });

            itemsDisplayed.Add(obj, inventory.Container.Items[i]);
        }
    }
    void Update()
    {
        UpdateSlots();
    }
    public void UpdateSlots()
    {

        foreach (KeyValuePair<GameObject, InventorySlot> _slot in itemsDisplayed)
        {
            if (_slot.Value.ID >= 0)
            {
                _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[_slot.Value.item.Id].uiDisplay;
                _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().color = new Color(1, 1, 1, 1);
                _slot.Key.GetComponentInChildren<TextMeshProUGUI>().text = _slot.Value.amount == 1 ? "" : _slot.Value.amount.ToString("n0");
            }
            else
            {
                _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().sprite = null;
                _slot.Key.transform.GetChild(0).GetComponentInChildren<Image>().color = new Color(1, 1, 1, 0);
                _slot.Key.GetComponentInChildren<TextMeshProUGUI>().text = "";
            }
        }
    }
     public void OnDragEnd(GameObject obj)
    {
        for (int i = 1; i < Key_Item.Length; i++)
        {
            if (itemsDisplayed[obj].item.Id == Key_Item[i].item.id)
            {
                if(i == 3)
                    Instantiate(Key_Item[i], new Vector3(left_hand.position.x,left_hand.position.y+5,left_hand.position.z), Quaternion.identity);
                else
                    Instantiate(Key_Item[i], new Vector3(left_hand.position.x+1, left_hand.position.y, left_hand.position.z), Quaternion.identity);
                break;
            }
        }
        if (itemsDisplayed[obj].amount == 1)
            inventory.RemoveItem(itemsDisplayed[obj].item);
        else
            itemsDisplayed[obj].amount -= 1;

        Destroy(mouseItem.obj);
        mouseItem.item = null;
    }

1. CreateSlots()

 - 프로그램 실행시 Datebase를 연결하고 Play중 실행될 Event를 추가한다.

 

2. UpdateSlots()

 - 플레이어가 Play중에 확인하는 Inventory의 Item을 이미지로 보여준다.

 

3. OnDragEnd()

 - 클릭한 위치에 있는 오브젝트를 Prefab 순번과 연결하여 해당 ID에 연결된 오브젝트 생성(위치는 왼손 컨트롤러)

 - 클릭한 이미지의 해당 오브젝트 개수가 1보다 작거나 같을 경우 Inventory에서 해당 이미지 삭제(아이템 삭제)

 - 클릭한 이이지의 해당 오브젝트가 1보다 클경우 이미지에 들어있는 개수 -1

 

 

 

Inventory Play화면

 

 

 

 

 

 

 

 

 

'Project > Alice Project' 카테고리의 다른 글

Window Mission  (0) 2022.02.14
Cat Hunting  (0) 2022.02.13
Spider Hunting  (0) 2022.02.13