Ejemplo n.º 1
0
 // Remembers the saved item if it existed
 public override void RememberHeldItem()
 {
     ItemIDHeld = PlayerPrefs.GetString("GauntletsID" + SlotNumber, "None");
     if (ItemIDHeld == "None")
     {
         //Debug.Log("Nothing Loaded; ID: 'None'");
         SlotWeight = 0;
         ItemsHeld  = 0;
     }
     else
     {
         ItemsHeld = PlayerPrefs.GetInt("GauntletsCount" +
                                        "" + SlotNumber, 1);
         string[]      str = ItemIDHeld.Split('-');
         ItemCatalogue cat = Resources.Load <ItemCatalogue>("Prefabs/Items/Catalogue/" + str[0]);
         if (cat != null)
         {
             cat = Instantiate(cat);
             //Debug.Log(cat.gameObject.name + " Loaded");
             if (cat.FindObjectID(str[0], int.Parse(str[1])) != null)
             {
                 HeldItem = Instantiate(cat.FindObjectID(str[0], int.Parse(str[1])));
             }
             if (HeldItem != null)
             {
                 //Debug.Log(HeldItem.gameObject.name + " Loaded (ID: " + HeldItem.itemID + ")");
                 HeldItem.transform.parent        = ParentInventory.transform;
                 HeldItem.transform.localPosition = Vector3.zero;
             }
         }
         if (HeldItem == null)
         {
             string Name       = PlayerPrefs.GetString(ItemIDHeld);
             Item   folderItem = Resources.Load <Item>("Prefabs/Items/" + Name + ".prefab");
             if (folderItem != null)
             {
                 HeldItem = Instantiate(folderItem, ParentInventory.transform);
             }
         }
         Destroy(cat.gameObject);
         if (HeldItem != null)
         {
             Durability = PlayerPrefs.GetInt("GauntletID_Durability" + SlotNumber, Durability);
             SetDurability(Durability);
             HeldItem.SetStack(ItemsHeld);
             ItemsHeld = HeldItem.stackCount;
             SetHeldItem();
             HeldItem.DeactivateItem();
         }
     }
     UpdateItemDisplay();
 }
Ejemplo n.º 2
0
    /// <summary>
    /// Adds the item to the inventory slot
    /// </summary>
    /// <param name="item"></param>
    /// <returns> This Returns the left over amount of items that couldn't be picked up cause the slot was either full or it went over the characters strength limit </returns>
    public override Item AddItem(Item item)
    {
        Item temp = item;

        if (HeldItem == null)
        {
            if (ParentInventory != null && item != null)
            {
                if (item.stackCount * item.itemWeight <= ParentInventory.GetAddableWeight)
                {
                    HeldItem = item;
                    temp     = Instantiate(HeldItem, HeldItem.transform.position + new Vector3(0, 0.2f, 0), HeldItem.transform.rotation);
                    temp.transform.parent = null;
                    Destroy(temp.gameObject);
                    SlotWeight = item.stackCount * item.itemWeight;
                    ParentInventory.AddWeight(SlotWeight);
                }
                else
                {
                    int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight);
                    if (numberOfAddableItems > 0)
                    {
                        HeldItem = item;
                        temp     = Instantiate(HeldItem, HeldItem.transform);
                        temp.transform.parent = null;

                        HeldItem.RemoveFromStack(HeldItem.stackCount - numberOfAddableItems);
                        temp.RemoveFromStack(numberOfAddableItems);

                        SlotWeight = numberOfAddableItems * item.itemWeight;
                        ParentInventory.AddWeight(SlotWeight);
                    }
                }
            }
        }
        else
        {
            if (ParentInventory != null && item != null)
            {
                if (item.itemID == HeldItem.itemID)
                {
                    int ItemAmount = HeldItem.stackCount + item.stackCount;
                    if (ItemAmount > HeldItem.maxStackCount)
                    {
                        int AmountToFill = HeldItem.maxStackCount - HeldItem.stackCount <= item.stackCount ? HeldItem.maxStackCount - HeldItem.stackCount
                            : item.stackCount;
                        if (AmountToFill > 0)
                        {
                            if (AmountToFill * item.itemWeight <= ParentInventory.GetAddableWeight)
                            {
                                HeldItem.AddToStack(AmountToFill);
                                temp.RemoveFromStack(AmountToFill);
                                SlotWeight += AmountToFill * item.itemWeight;
                                ParentInventory.AddWeight(AmountToFill * item.itemWeight);
                            }
                            else
                            {
                                int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight) <= item.stackCount ? Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight)
                                    : item.stackCount;
                                if (numberOfAddableItems > 0)
                                {
                                    HeldItem.AddToStack(numberOfAddableItems);
                                    temp.RemoveFromStack(numberOfAddableItems);
                                    if (temp.stackCount <= 0)
                                    {
                                        Destroy(temp.gameObject);
                                    }
                                    SlotWeight += numberOfAddableItems * item.itemWeight;
                                    ParentInventory.AddWeight(numberOfAddableItems * item.itemWeight);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item.stackCount * item.itemWeight <= ParentInventory.GetAddableWeight)
                        {
                            HeldItem.AddToStack(temp.stackCount);
                            temp = Instantiate(HeldItem, HeldItem.transform);
                            temp.transform.parent = null;
                            temp.RemoveFromStack(temp.stackCount);
                            if (temp.stackCount <= 0)
                            {
                                Destroy(temp.gameObject);
                            }
                            SlotWeight += item.stackCount * item.itemWeight;
                            ParentInventory.AddWeight(item.stackCount * item.itemWeight);
                        }
                        else
                        {
                            int numberOfAddableItems = Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight) <= item.stackCount ? Mathf.FloorToInt(ParentInventory.GetAddableWeight / item.itemWeight)
                                : item.stackCount;
                            if (numberOfAddableItems > 0)
                            {
                                HeldItem.AddToStack(numberOfAddableItems);
                                temp.RemoveFromStack(numberOfAddableItems);
                                SlotWeight += numberOfAddableItems * item.itemWeight;
                                ParentInventory.AddWeight(numberOfAddableItems * item.itemWeight);
                            }
                        }
                    }
                }
            }
        }
        if (HeldItem != null)
        {
            HeldItem.DeactivateItem();
        }
        SetHeldItem();
        UpdateItemDisplay();
        if (temp != null)
        {
            temp.ReleaseItem();
        }
        return(temp);
    }