Beispiel #1
0
    public void setExchangeItem(GameTypes.ItemType type, int amount)
    {
        Dictionary <GameTypes.ItemType, int> list = new Dictionary <GameTypes.ItemType, int>();

        list.Add(type, amount);
        setExchangeList(list);
    }
Beispiel #2
0
    public void dropdownChanged()
    {
        //get the chosen string
        int    index  = mItemDropdown.value;
        string choice = mOptStrings[index];

        //get the type from the chosen string
        string[] splitString = choice.Split('(');
        if (splitString.Length != 2)
        {
            Debug.LogError("Reading of the choice string failed.");
        }
        choice = splitString[0];
        //get type from the string
        GameTypes.ItemType type = (GameTypes.ItemType)System.Enum.Parse(typeof(GameTypes.ItemType), choice);
        if (type == 0)
        {
            Debug.LogError("Reading of the choise string as an Item type failed.");
        }
        //set chosen type
        mChoiceType = type;
        //set the min and max
        if (!mOptDict.ContainsKey(type))
        {
            Debug.LogError("The options dictionary does not contain the chosen option?");
        }
        mChoiceMin = mOptDict[type].Key;
        mChoiceMax = mOptDict[type].Value;
        //reset the number field
        mNumberField.text = "";
        //Debug.Log(mChoiceType.ToString());
        //Debug.Log(mChoiceMin.ToString());
        //Debug.Log(mChoiceMax.ToString());
    }
Beispiel #3
0
    public bool makeInventorySpaceFor(GameTypes.ItemType itype)
    {
        //dumps everything in the inventory except items of a certain type
        //return true if unit needs to return to stockpile, false other wise
        Dictionary <GameTypes.ItemType, int> dict = getInventoryDictionary();
        Building sp         = getStockpile();
        bool     new_action = false;

        foreach (KeyValuePair <GameTypes.ItemType, int> pair in dict)
        {
            //skip- if the item is equipped
            Item      it = getItemOfType(pair.Key);
            EquipItem ei = it as EquipItem;
            if (ei)
            {
                if (ei.isEquipped())
                {
                    continue;
                }
            }
            //skip if its the item we want
            if (pair.Key != itype)
            {
                //make an exchange with the stockpile
                exchangeWith(sp, pair.Key, pair.Value);
                new_action = true;
            }
        }
        return(new_action);
    }
Beispiel #4
0
        //init item function
        public static Item initItem(GameTypes.ItemType type, Transform parent)
        {
            Item item = GameObject.Instantiate(getItem(type.ToString()), parent).GetComponent <Item>();

            item.setAmount(0);
            item.setType(type);
            return(item);
        }
Beispiel #5
0
    public void exchangeWith(EntityAction target, GameTypes.ItemType type, int amount)
    {
        Exchange ex = ObjectManager.initExchange(transform);

        ex.setTarget(target);
        ex.setExchangeItem(type, amount);
        mActions.prependAction(ex);
    }
Beispiel #6
0
        //init Resource function
        public static Resource initResource(Vector3 pos, GameTypes.ItemType type, Region reg)
        {
            Resource res = GameObject.Instantiate(getResource(type.ToString()), reg.getResourceObject().transform).GetComponent <Resource>();

            res.gameObject.transform.position = pos;
            res.mType = type;
            reg.addEntity("resources", res);
            res.mAmount = Globals.RESOURCE_DEFAULT_AMOUNT;
            return(res);
        }
Beispiel #7
0
 public Item getItemOfType(GameTypes.ItemType type)
 {
     foreach (Item item in mItems)
     {
         if (item.getType() == type)
         {
             return(item);
         }
     }
     return(null);
 }
Beispiel #8
0
    private void _removeItemOfType(GameTypes.ItemType itype)
    {
        int ind = _getIndexOfItemOfType(itype);

        if (ind < 0 || ind > mItems.Count - 1)
        {
            return;
        }
        //THIS SHOULD REALLY PLACE AN ENTITY THAT IS A BAG OF ITEMS THAT CAN BE PICKED UP BY SOMEONE ELSE
        Destroy(mItems[ind].gameObject);
        mItems.RemoveAt(ind);
    }
Beispiel #9
0
    public void returnToStockpile(GameTypes.ItemType type)
    {
        Item item = getItemOfType(type);

        if (!item)
        {
            //this means the unit doesnt contain any of this item, which means the inventory is already full
            //so dump the inventory
            //Debug.Log("No item of type, the inventory must already be full. Dumping inventory");
            dumpInventory();
            return;
        }
        exchangeWith(getStockpile(), type, item.getAmount());
    }
Beispiel #10
0
 public void removeChoice()
 {
     //remove the last element from the choice dict
     GameTypes.ItemType rmtype = GameTypes.ItemType.Unknown;
     foreach (KeyValuePair <GameTypes.ItemType, int> entry in mExchangeList)
     {
         rmtype = entry.Key;
     }
     if (rmtype != GameTypes.ItemType.Unknown)
     {
         mExchangeList.Remove(rmtype);
         updateSelection();
     }
 }
Beispiel #11
0
    //-------------------------------------------------------------------------------------------------
    // private methods
    //-------------------------------------------------------------------------------------------------

    private int _getIndexOfItemOfType(GameTypes.ItemType itype)
    {
        int ind = 0;

        foreach (Item item in mItems)
        {
            if (item.getType() == itype)
            {
                return(ind);
            }
            ind++;
        }
        return(-1);
    }
Beispiel #12
0
    public Resource getClosestResourceOfType(GameTypes.ItemType type, Entity ent)
    {
        Resource res       = null;
        float    mag       = -1f;
        float    close_mag = -1f;

        foreach (Resource lres in mGroupMap["resources"])
        {
            if (lres.mType == type)
            {
                mag = (ent.pointOfTouchingBounds(lres) - ent.transform.position).magnitude;
                if (close_mag < 0 || mag < close_mag)
                {
                    close_mag = mag;
                    res       = lres;
                }
            }
        }
        return(res);
    }
Beispiel #13
0
    //-------------------------------------------------------------------------------------------------
    // private methods
    //-------------------------------------------------------------------------------------------------
    private void setupType()
    {
        //sets up the type info in case it isnt initalised correct and so I can remember what it should be
        switch (mType)
        {
        case GameTypes.BuildingType.Farm:
            getInventory().mCapacity = 50;  //default 50
            mUnitInventory.mCapacity = 2;
            mMaxProgress             = 100; //default 100
            mCreateItemType          = GameTypes.ItemType.Food;
            mWorkers.mCapacity       = 2;
            break;

        default:
            WorkedProdBuilding wpb = (WorkedProdBuilding)this;
            if (!wpb)
            {
                Debug.LogError("Building type not recognised");
            }
            break;
        }
    }
Beispiel #14
0
    public void populate(EntityAction acter, EntityAction target)
    {
        gameObject.SetActive(true);
        mCancelled = false;
        mInUse     = true;
        //add options to the acter drop down
        //List<string> act_opts = new List<string> { "Item1", "Item2", "Item3", "Item4" };
        //mItemDropdown.AddOptions(act_opts);
        //make  lsit of potential options
        Dictionary <GameTypes.ItemType, int> act_items = acter.getInventoryDictionary();
        Dictionary <GameTypes.ItemType, int> tar_items = target.getInventoryDictionary();
        Dictionary <GameTypes.ItemType, KeyValuePair <int, int> > opts_dict = new Dictionary <GameTypes.ItemType, KeyValuePair <int, int> >();
        KeyValuePair <int, int> temp_kp = new KeyValuePair <int, int>();

        foreach (KeyValuePair <GameTypes.ItemType, int> it in act_items)
        {
            if (!opts_dict.ContainsKey(it.Key))
            {
                //the opt dict doesnt already contain this key
                //get the max value which is how much the acter can give
                temp_kp = new KeyValuePair <int, int>(0, it.Value);
                opts_dict.Add(it.Key, temp_kp);
            }
            else
            {
                Debug.LogError("Opts Dict already contains the ItemType key. This shoudln't be possible if the inventory only has one of each item as it should.");
            }
        }
        //now fill the min value with the target items
        foreach (KeyValuePair <GameTypes.ItemType, int> it in tar_items)
        {
            if (opts_dict.ContainsKey(it.Key))
            {
                //the acter also has this item type
                opts_dict[it.Key] = new KeyValuePair <int, int>(-1 * it.Value, opts_dict[it.Key].Value);
            }
            else
            {
                //the acter didnt contain this item and we need to make a ew entry
                temp_kp = new KeyValuePair <int, int>(-1 * it.Value, 0);
                opts_dict.Add(it.Key, temp_kp);
            }
        }
        //now make a string for each entry
        List <string> opt_strings = new List <string>();

        GameTypes.ItemType first_opt = GameTypes.ItemType.Unknown;
        int i = 0;

        foreach (GameTypes.ItemType type in opts_dict.Keys)
        {
            opt_strings.Add(string.Format("{0}({1}:{2})", type.ToString(), opts_dict[type].Key, opts_dict[type].Value));
            if (i == 0)
            {
                first_opt = type;
            }
            i++;
        }
        mOptDict    = opts_dict;
        mOptStrings = opt_strings;
        mItemDropdown.AddOptions(opt_strings);

        //set the first option as the chosen option
        if (first_opt != GameTypes.ItemType.Unknown)
        {
            mChoiceType  = first_opt;
            mChoiceMin   = mOptDict[first_opt].Key;
            mChoiceMax   = mOptDict[first_opt].Value;
            mChoiceValue = mChoiceMax;
        }

        //bring to front of the sidebar
        transform.SetAsLastSibling();
        mPopulated = true;
    }
Beispiel #15
0
 public void setType(GameTypes.ItemType t)
 {
     mType = t;
 }
Beispiel #16
0
    private void doExchange()
    {
        //the exchange speed is too fast so only collect every nth cycle
        if (mCycleCounter % mCyclesToSkip == 0)
        {
            if (mCycleCounter == mCyclesToSkip)
            {
                mCycleCounter = 1;
            }
            else
            {
                mCycleCounter++;
            }
            return;
        }
        //Debug.Log("doing the exchange");
        //Debug.Log("in do exchange size of the exchange list is "+mExchangeList.Count.ToString());
        mDoneType  = GameTypes.ItemType.Unknown;
        mEmptyItem = false;
        //loop over the type,amount pairs in the ExchangeList
        List <GameTypes.ItemType> tlist = new List <GameTypes.ItemType>(mExchangeList.Keys);

        foreach (GameTypes.ItemType itype in tlist)
        {
            int ival = mExchangeList[itype];
            //check if the amount is almost zero
            if (Mathf.Abs(ival) <= 0)
            {
                //if its zero remove from the ExchangeList
                setRemoveItem(itype);
                continue;
            }
            //split it into separate functions for positive and negative amounts
            //positive amount, move items from acter to target
            float cycle_amount = mActer.getExchangeSpeed() / 0.1f;
            if (ival > 0)
            {
                //Debug.Log("positive value");
                if (checkAndExchangeItems(mActer, mTarget, itype, cycle_amount) == false)
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
            //negative amount, move items from target to acter
            else if (ival < 0)
            {
                //Debug.Log("negative value");
                if (checkAndExchangeItems(mTarget, mActer, itype, cycle_amount) == false)
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
        }
        //remove the empty entries in ExchangeList
        if (mEmptyItem && mDoneType != GameTypes.ItemType.Unknown)
        {
            mExchangeList.Remove(mDoneType);
        }
    }
Beispiel #17
0
    public bool getResource(GameTypes.ItemType type, int amount = -1)
    {
        //tells the unit to get some of a resource. Chceks the unit inventory, if the
        //unit has none of the item it checks the stockpile and tells them to get the item from there
        //if theres none in the stockpile it tells them to go and find the resource.
        //amount set to -1 means fill the inventory

        //what amount do we need?
        int invcap  = getInventory().mCapacity;
        int invsize = getInventorySize();

        if (amount == -1 || invcap < invsize + amount)
        {
            amount = invcap - invsize;
        }

        //check the inventory and do nothing if the unit has the item
        Item item = getItemOfType(type);

        if (item)
        {
            //unit has it, do nothing, if it has the desired amount
            if (item.getAmount() >= amount)
            {
                return(true);
            }
        }
        //does the stockpile have some
        Building sp = getStockpile();

        item = sp.getItemOfType(type);
        if (item)
        {
            //is the inventory full
            if (isInventoryFull())
            {
                //is the stockpile full
                if (sp.isInventoryFull())
                {
                    dropInventory();
                }
                else
                {
                    dumpInventory();
                }
            }
            //stockpile has it, send Unit to get it
            exchangeWith(sp, type, -1 * amount);
            return(false);
        }
        //stockpile doesn't have it, get the closest resource of this type
        Resource res = mTown.getRegion().getClosestResourceOfType(type, this);

        if (res)
        {
            //Debug.Log(res.transform.position);
            //go and collect this resource for one cycle
            collectResource(res, true);
        }
        return(false);
    }
Beispiel #18
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);
    }
Beispiel #19
0
 public void setTarget(Resource target)
 {
     mTarget = target;
     mType   = mTarget.mType;
 }
Beispiel #20
0
 private void setRemoveItem(GameTypes.ItemType type)
 {
     //sets the members to remove an element from the list
     mDoneType  = type;
     mEmptyItem = true;
 }
Beispiel #21
0
 public Item getItemOfType(GameTypes.ItemType type)
 {
     return(mInventory.getItemOfType(type));
 }