示例#1
0
    public void  MoveToInventory(Transform Item, int Amount, int SlotID)
    {
        if (InvManager.Items > InvManager.MaxItems)
        {
            return;
        }
        for (int a = 0; a < MaxItems; a++)                                                                       //Starting a loop in the slots of the inventory:
        {
            if (Slots[a].IsTaken == true)                                                                        //Checking if there's an item in this slot.
            {
                if (Slots[a].Item == Item)                                                                       //Check if the item exists in the bag.
                {
                    Item ItemScript = Slots[a].Item.GetComponent <Item>();                                       //Getting the item script from the items inside the bag.
                    if (ItemScript.Amount - Amount >= 1)                                                         //If the amount goes below 1.
                    {
                        ItemScript.CurrencyAmount -= Amount * (ItemScript.CurrencyAmount / (ItemScript.Amount)); //Reduce the price of the item since we reduced its amount.
                        ItemScript.Amount         -= Amount;                                                     //Reduce the amount of the item inside the bag.

                        Transform CloneObj;                                                                      //Create a temporary object to move the removed item to.
                        CloneObj = (Transform)Instantiate(Item, Item.position, Item.rotation);                   //Clone the original item.
                        Item CloneScript = CloneObj.GetComponent <Item>();                                       //Get the item script.
                        //Set the cloned item settings:
                        CloneScript.Amount         = Amount;
                        CloneScript.CurrencyAmount = Amount * (ItemScript.CurrencyAmount / ItemScript.Amount);

                        //Reactivate the clone and spawn it in real world.
                        CloneObj.gameObject.SetActive(true);
                        CloneObj.transform.GetComponent <Renderer>().enabled = true;
                        CloneObj.transform.parent        = null;
                        CloneObj.transform.localPosition = Player.transform.localPosition;


                        InvManager.GetComponent <AudioSource>().PlayOneShot(InvManager.RemovedItem);                        //a sound

                        InvManager.AddItemToSlot(CloneObj.transform, SlotID);

                        Panel.RefreshItems(); if (SaveAndLoad == true)
                        {
                            SaveItems();
                        }
                    }
                    else
                    {
                        //Reactivate the item and spawn it in real world.
                        Item.gameObject.SetActive(true);
                        Item.transform.GetComponent <Renderer>().enabled = true;
                        Item.transform.parent        = null;
                        Item.transform.localPosition = Player.transform.localPosition;



                        //Remove the item from this slot.
                        Slots[a].Item    = null;
                        Slots[a].IsTaken = false;

                        InvManager.GetComponent <AudioSource>().PlayOneShot(InvManager.RemovedItem);                        //a sound

                        Items--;

                        InvManager.AddItemToSlot(Item, SlotID);

                        Panel.RefreshItems(); if (SaveAndLoad == true)
                        {
                            SaveItems();
                        }
                    }
                }
            }
        }
    }
示例#2
0
    public Transform FreeItem(Transform Item, int Amount)                                                        //This is the only public void that returns a transform, which is the item that is now free.
    {
        for (int a = 0; a < MaxItems; a++)                                                                       //Starting a loop in the slots of the inventory:
        {
            if (Slots[a].IsTaken == true)                                                                        //Checking if there's an item in this slot.
            {
                if (Slots[a].Item == Item)                                                                       //Check if the item exists in the bag.
                {
                    Item ItemScript = Slots[a].Item.GetComponent <Item>();                                       //Getting the item script from the items inside the bag.
                    if (ItemScript.Amount - Amount >= 1)                                                         //If the amount goes below 1.
                    {
                        ItemScript.CurrencyAmount -= Amount * (ItemScript.CurrencyAmount / (ItemScript.Amount)); //Reduce the price of the item since we reduced its amount.
                        ItemScript.Amount         -= Amount;                                                     //Reduce the amount of the item inside the bag.

                        Transform CloneObj;                                                                      //Create a temporary object to move the removed item to.
                        CloneObj = Instantiate(Item, Item.position, Item.rotation) as Transform;                 //Clone the original item.
                        Item CloneScript = CloneObj.GetComponent <Item>();                                       //Get the item script.
                        //Set the cloned item settings:
                        CloneScript.Amount         = Amount;
                        CloneScript.CurrencyAmount = Amount * (ItemScript.CurrencyAmount / ItemScript.Amount);

                        //Reactivate the clone and spawn it in real world.
                        CloneObj.gameObject.SetActive(true);
                        CloneObj.transform.GetComponent <Renderer>().enabled = true;
                        CloneObj.transform.parent        = null;
                        CloneObj.transform.localPosition = Player.transform.localPosition;


                        if (AudioSC)
                        {
                            AudioSC.PlayOneShot(RemovedItem);                                 //a sound
                        }
                        InvUI.RefreshItems(); if (SaveAndLoad == true)
                        {
                            SaveItems();
                        }
                        return(CloneObj);
                    }
                    else
                    {
                        //Reactivate the item and spawn it in real world.
                        Item.gameObject.SetActive(true);
                        Item.transform.GetComponent <Renderer>().enabled = true;
                        Item.transform.parent        = null;
                        Item.transform.localPosition = Player.transform.localPosition;



                        //Remove the item from this slot.
                        Slots[a].Item    = null;
                        Slots[a].IsTaken = false;

                        if (AudioSC)
                        {
                            AudioSC.PlayOneShot(RemovedItem);                                 //a sound
                        }
                        Items--;
                        InvUI.RefreshItems(); if (SaveAndLoad == true)
                        {
                            SaveItems();
                        }
                        return(Item);
                    }
                }
            }
        }
        InvUI.RefreshItems(); if (SaveAndLoad == true)
        {
            SaveItems();
        }
        return(null);
    }