如何使用除了和任何字符串而不是类

时间:2014-01-20 04:37:49

标签: c# linq unity3d

如上所述我将如何使用linq以及除了any()之外的一些代码。目前我使用的项目(一个类),待识别等,但这会导致问题,所以我希望用字符串来识别它们,但我不知道如何。代码的两个函数是:ContainsAllItems,另一个是函数函数。 谢谢!

EDIT;为了更清楚以防万一,现在它检查两个列表中的项目,而不是我要求的是如何使它检查列表中的项目的项目名称,但仍然使它具有相同的效果,因此它会破坏物品并检查物品,并确保在销毁时不会超过需要销毁物品。

如果您不想查看代码,请参阅以下两行:

public static bool ContainsAllItems(List<Item> a, List<Item> b)
    {
        return !b.Except(a).Any();
    }

List<Item> list = new List<Item>();
                list = InventoryItems.Except(Recipe.InputItem).ToList();
                InventoryItems = list;

代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class PlayerInventory : MonoBehaviour {

    public int MaxInventory = 26;

    public List<Item> InventoryItems = new List<Item>();

    void OnGUI(){
        if (GUILayout.Button ("+5 Wood")) {
            AddItem(ItemConstructors.Wood());
            AddItem(ItemConstructors.Wood());    
            AddItem(ItemConstructors.Wood());    
            AddItem(ItemConstructors.Wood());    
            AddItem(ItemConstructors.Wood());
        }
        if (GUILayout.Button ("Craft Wall")) {
            Craft(CraftingRecipes.WoodenWall());
        }

    }


    public void AddItem(Item _item){
        if (InventoryItems.Count < MaxInventory) {
            InventoryItems.Add(_item);
            //SaveItems();
        }
    }

    public void RemoveItem(Item _item){
        InventoryItems.Remove (_item);
    }

    public void UseItem(Item _item){
        if (_item.ItemType == "Food") {
            ConsumeItem(_item);
        }
        SaveItems();
    }

    public void ConsumeItem(Item _item){
        if (_item.ItemDur > 1) {
            _item.ItemDur--; 
        }
        else if(_item.ItemDur <= 1){
            RemoveItem(_item);
        }
    }

    public void SaveItems(){
        for (int i = 0; i<MaxInventory; i++) {
            PlayerPrefs.SetString("Itemn"+i,InventoryItems[i].ItemName); 
            PlayerPrefs.SetString("Itemt"+i,InventoryItems[i].ItemType);
            PlayerPrefs.SetInt("Itemv"+i,InventoryItems[i].ItemValue);
            PlayerPrefs.SetInt("Itemd"+i,InventoryItems[i].ItemDur);
        }
//LoadItems ();
    }
    public void LoadItems(){
        InventoryItems.Clear();
        for (int i = 0; i<MaxInventory; i++) {
            Item it = new Item();
            it.ItemName = PlayerPrefs.GetString("Itemn"+i);
            it.ItemType = PlayerPrefs.GetString("Itemt"+i);
            it.ItemValue = PlayerPrefs.GetInt("Itemv"+i);
            it.ItemDur = PlayerPrefs.GetInt("Itemd"+i);
            if(it.ItemName.Length <= 2 && it.ItemType.Length <= 2){
            }
            else{
                InventoryItems.Add(it);
            }
        }
    }

    public void Craft(CraftingItem Recipe){
        bool canCraft = ContainsAllItems (InventoryItems,Recipe.InputItem);
        if(canCraft){
            if(InventoryItems.Count < MaxInventory){
                List<Item> list = new List<Item>();
                list = InventoryItems.Except(Recipe.InputItem).ToList();
                InventoryItems = list;
                AddItem(Recipe.OutputItem);
            }
            else{
                Debug.Log("Not enough space!");
            }
        }
        else{
            Debug.Log("Invalid Items.");
        }
    }

    public static bool ContainsAllItems(List<Item> a, List<Item> b)
    {
        return !b.Except(a).Any();
    }

}

0 个答案:

没有答案