public void InstantiateCrafterButton(ItemDescription item, List <Cell> selected)
    {
        GameObject button = Instantiate(m_crafterButtonPrefab, m_crafterButtonsContainer.transform);
        //initialize button fields and render icon and name
        CrafterButton buttonDescription = button.GetComponent <CrafterButton>();

        buttonDescription.m_item = item;
        buttonDescription.m_selectedItemsForCraft          = selected;
        buttonDescription.m_controller                     = GetComponent <CraftCharacterController>();
        button.GetComponentInChildren <RawImage>().texture = Resources.Load <Texture>("Sprites/" + item.m_sprite);
        button.GetComponentInChildren <Text>().text        = item.m_name;
    }
Exemple #2
0
    public void CrafterButtonClick(CrafterButton button)
    {
        //if cursor items incompatible with button items -> exit
        if (m_inventory.m_cellOnCursor != null && m_inventory.m_cellOnCursor.m_item != button.m_item)
        {
            return;
        }

        int countUserWantsToCraft = 1;

        if (Input.GetKey(KeyCode.LeftShift))                                   //in this case user wants to craft as maximum items as possible
        {
            countUserWantsToCraft = button.m_selectedItemsForCraft[0].m_count; // it depends on minimum count of ingridients
        }
        //but this number can not be bigger than maximum stack size
        int countUserCanCraft = (int)Mathf.Min(countUserWantsToCraft, button.m_item.m_maxCount - (m_inventory.m_cellOnCursor == null ? 0 : m_inventory.m_cellOnCursor.m_count));

        if (countUserCanCraft == 0)
        {
            return;
        }
        foreach (Cell cell in button.m_selectedItemsForCraft)
        {
            cell.m_count -= countUserCanCraft;
            m_view.CellCountChangedHandler(cell);
        }
        CrafterAction();

        if (m_inventory.m_cellOnCursor == null)
        {
            m_inventory.m_cellOnCursor = new CellInfo {
                m_item = button.m_item, m_count = 0
            }
        }
        ;
        m_inventory.m_cellOnCursor.m_count += countUserCanCraft;
        m_view.CursorStateChangedHandler(m_inventory.m_cellOnCursor);
    }