示例#1
0
    //class responsible for constructing an item from the name sent, can be Weapon, Armor, or Consumable
    public Item itemFactory(string itemName, ItemsMasterList itemMasterList)
    {
        Item toReturn;

        //find the index of our item by Name
        int indexOf = itemMasterList.itemListNames.IndexOf(itemName);

        if (indexOf == -1)
        {
            Debug.Log("ITEM.itemFactory, item was not found in list");
            return(null);
        }

        //Debug.Log(itemMasterList);
        //Debug.Log(itemMasterList.itemListNames[0]);
        //Debug.Log(itemMasterList.itemListNames[1]);
        //Debug.Log(indexOf);

        //if our item is a Weapon, construct weapon class
        if (itemMasterList.itemListTypes[indexOf] == "Weapon")
        {
            return(toReturn = new Weapon(itemMasterList.itemListNames[indexOf], itemMasterList.itemListWeights[indexOf], itemMasterList.itemListPrices[indexOf]));
        }

        //if our item is a Consumable
        else if (itemMasterList.itemListTypes[indexOf] == "Consumable")
        {
            return(toReturn = new Consumable(itemMasterList.itemListNames[indexOf], itemMasterList.itemListWeights[indexOf], itemMasterList.itemListPrices[indexOf]));
        }

        //if our item is armor
        else if (itemMasterList.itemListTypes[indexOf] == "Armor")
        {
            return(toReturn = new Armor(itemMasterList.itemListNames[indexOf], itemMasterList.itemListWeights[indexOf], itemMasterList.itemListPrices[indexOf]));
        }

        return(toReturn = new Item()); //satisfy compiler, we should never get here
    }
 public void addItemToPlayerInventory(string itemName, ItemsMasterList itemsMasterList)
 {
     //adds item to our inventory, gets item by calling to the itemFactory, which will construct our item to add.
     //this allows us to add items, by only knowing their name, it will be populated.
     inventory.addItem(itemReference.itemFactory(itemName, itemsMasterList));
 }