Example #1
0
    //-------------------------------------------------------------------------------------------------
    // private methods
    //-------------------------------------------------------------------------------------------------
    private bool checkAndExchangeItems(EntityAction giver, EntityAction getter, GameTypes.ItemType type, float cycle_amount)
    //checks everything and moves some of one item from the giver to the getter
    {
        //check the progress
        mCycleProgress += cycle_amount;
        int amount = 0;

        if (mCycleProgress >= 100)
        {
            amount         = 1;
            mCycleProgress = 0f;
        }
        else
        {
            return(false);
        }
        //does the acter have the item?
        Item act_item = giver.getItemOfType(type);

        if (!act_item)
        {
            //acter doesnt have the item, so cant do exchange
            setRemoveItem(type);
            //Debug.Log("acter doesnt have the item");
            return(false);
        }
        //does the acter have enough of the item
        if (act_item.getAmount() < amount)
        {
            amount = act_item.getAmount();
            //Debug.Log("acter doesnt have enough of the item, adjusting amount");
        }
        //does the target have the item to receive?
        Item tar_item = getter.getItemOfType(type);

        if (!tar_item)
        {
            //target doesnt have the item, make one
            tar_item = ObjectManager.initItem(type, getter.getInventory().transform);
            getter.addItem(tar_item);
            //Debug.Log("target doesnt have the item");
        }
        //does the target have enough space for this item
        int cap     = getter.getInventory().mCapacity;
        int invsize = getter.getInventorySize();

        if (invsize + amount > cap)
        {
            //target doesnt have space, reduce the amount
            amount = cap - invsize;
            if (amount <= 0)
            {
                setRemoveItem(type);
                return(false);
            }
            //control flow moves on if cycle amount is big enough
        }
        //target has space, add and remove the amount
        tar_item.setAmount(tar_item.getAmount() + amount);
        act_item.setAmount(act_item.getAmount() - amount);
        if (mExchangeList[type] >= 0)
        {
            mExchangeList[type] = mExchangeList[type] - amount;
        }
        else if (mExchangeList[type] < 0)
        {
            mExchangeList[type] = mExchangeList[type] + amount;
        }

        return(false);
    }