示例#1
0
    // handle buying or selling
    public void handleItem()
    {
        GameObject itemObj = null;

        // whichever object is being bought or sold
        if (buying)
        {
            itemObj = merchantItemList.selectedItem;
        }
        else
        {
            itemObj = playerItemList.selectedItem;
        }

        // if nothing is selected, return
        if (itemObj == null)
        {
            return;
        }

        // convert the text from that object to an item
        // It's of the form "ObjectName (x)" where x is a quantity
        ItemAbstract item;

        UnityEngine.UI.Text indexItem = itemObj.transform.GetChild(0).
                                        GetComponent <UnityEngine.UI.Text>();
        Regex           regex   = new Regex(@"\w+");
        MatchCollection matches = regex.Matches(indexItem.text);

        item = ItemDefinitions.stringToItem(matches[0].ToString() + ":" +
                                            matches[1].ToString());

        int cost;

        // handle inventory and currency changes
        if (buying)
        {
            cost = ItemDefinitions.getCost(item, merchantId, buying);
            merchantInventory.addCurrency(cost);
            playerInventory.addCurrency(-cost);
            merchantItemList.removeItem(item);
            playerItemList.addItem(item);
            if (item.getTier() > 3)
            {
                BlueprintDefinitions.applyBlueprint(item);
                print("applying blueprint " + ItemDefinitions.itemToString(item));
            }
        }
        else
        {
            cost = ItemDefinitions.getCost(item, playerId, buying);
            merchantInventory.addCurrency(-cost);
            playerInventory.addCurrency(cost);
            playerItemList.removeItem(item);
            merchantItemList.addItem(item);
            if (item.getTier() > 3)
            {
                BlueprintDefinitions.deapplyBlueprint(item);
                print("deapplying blueprint " + ItemDefinitions.itemToString(item));
            }
        }
    }