Unity 背包道具搜索

因为背包有很多道具,用户要根据不同需要搜索出不同的道具.  道具的属性有非常居多,游戏快开发完毕的时候,突然发现ItemManager类里面几乎每一个搜索方法都有一个foreach循环, 循环里面因为一点点不同的搜索条件就会导致重新写一个搜索方法出来.最后发现有10多种搜索方法. 后来打算优化下这个问题, 参考了下Itween传入参数进行移动的方式.

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SearchOp
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, object> search = new Dictionary<string, object>();
            search.Add("ConfigId", 101);
            search.Add("Bind", true);

            ItemManager.I.TestInit();

            //第一次搜索
            List<ItemBase> itemList = ItemManager.I.Get(search);
            PrintItem(itemList);

            //第二次搜索
            search.Clear();
            search.Add("Bind", false);
            itemList = ItemManager.I.Get(search);
            PrintItem(itemList);


            Console.ReadLine();
        }

        public static void PrintItem(List<ItemBase> itemList)
        {
            foreach (var item in itemList)
            {
                Console.WriteLine("Name: " + item.Name);
                Console.WriteLine("Id: " + item.ConfigId);
                Console.WriteLine("Bind: " + item.Bind);
                Console.WriteLine("BigType: " + item.BigType);
                Console.WriteLine("SubType: " + item.SubType);
                Console.WriteLine("TimeType: " + item.TimeType);
                Console.WriteLine("-----------------------");
            }
        }

    }


    public class ItemManager
    {
        private static ItemManager m_I;
        public static ItemManager I
        {
            get
            {
                if (m_I == null)
                    m_I = new ItemManager();

                return m_I;
            }
        }


        private List<ItemBase> mItemList = new List<ItemBase>();

        public void TestInit()
        {
            mItemList.Add(new ItemBase()
            {
                Name = "幽梦之刃",
                ConfigId = 101,
                BigType = 1,
                SubType = 1,
                Bind = true,
                TimeType = TimelimitEnum.None
            });
            mItemList.Add(new ItemBase()
            {
                Name = "幽梦之刃",
                ConfigId = 101,
                BigType = 1,
                SubType = 1,
                Bind = false,
                TimeType = TimelimitEnum.None
            });
            mItemList.Add(new ItemBase()
            {
                Name = "幽梦之刃",
                ConfigId = 101,
                BigType = 1,
                SubType = 1,
                Bind = true,
                TimeType = TimelimitEnum.None
            });
            mItemList.Add(new ItemBase()
            {
                Name = "无尽之刃",
                ConfigId = 102,
                BigType = 1,
                SubType = 1,
                Bind = true,
                TimeType = TimelimitEnum.PastDue
            });
        }




        public List<ItemBase> Get(Dictionary<string, object> condition)
        {
            List<ItemBase> tempList = new List<ItemBase>();
            List<ItemFilter> conditionFunList = ConvertCondition(condition);

            bool isNotThrough = false;
            foreach (var item in mItemList)
            {
                isNotThrough = true;
                foreach (var cond in conditionFunList)
                {
                    if (cond.Execute(item) == false)
                    {
                        isNotThrough = false;
                        break;
                    }
                }

                if (isNotThrough)
                {
                    tempList.Add(item);
                }
            }

            return tempList;
        }

        public List<ItemFilter> ConvertCondition(Dictionary<string, object> dic)
        {
            List<ItemFilter> conditionFunList = new List<ItemFilter>();
            foreach (var str in dic)
            {
                switch (str.Key)
                {
                    case "ConfigId":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.EquilId,
                            FunArgs = str.Value
                        });
                        break;
                    case "Bind":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.IsBind,
                            FunArgs = str.Value
                        });
                        break;
                    case "TimeType":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.CheckTime,
                            FunArgs = str.Value
                        });
                        break;
                    case "IsEquip":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.IsEquip,
                            FunArgs = str.Value
                        });
                        break;
                    case "IsMaterial":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.IsMaterial,
                            FunArgs = str.Value
                        });
                        break;
                    case "IsSpecial":
                        conditionFunList.Add(new ItemFilter()
                        {
                            Fun = ItemFilterFun.IsSpecial,
                            FunArgs = str.Value
                        });
                        break;
                }
            }

            return conditionFunList;
        }

    }

    public class ItemFilterFun
    {
        public static bool EquilId(ItemBase item, object args)
        {
            int id = (int)args;
            return item.ConfigId == id;
        }

        public static bool IsBind(ItemBase item, object args)
        {
            bool isBind = (bool)args;
            return item.Bind == isBind;
        }

        public static bool CheckTime(ItemBase item, object args)
        {
            TimelimitEnum timeType = (TimelimitEnum)args;
            return item.TimeType == timeType;
        }

        public static bool IsEquip(ItemBase item, object args)
        {
            return item.BigType == (int)ItemType.Equip;
        }

        public static bool IsMaterial(ItemBase item, object args)
        {
            int type = item.BigType;
            return type == 2 || type == 4 || type == 6;
        }

        public static bool IsSpecial(ItemBase item, object args)
        {
            int type = item.BigType;
            return type == 1 || type == 5 || type == 7 || type == 8;
        }
    }

    public class ItemFilter
    {
        public Func<ItemBase, object, bool> Fun;
        public object FunArgs;

        public bool Execute(ItemBase item)
        {
            return Fun(item, FunArgs);
        }

    }


    public class ItemBase
    {
        public string Name;
        public int Pos;
        public int ConfigId;
        public bool Bind;
        public int BigType;
        public int SubType;
        public TimelimitEnum TimeType;
    }

    public enum TimelimitEnum
    {
        None = -1,
        PastDue = 0,
        NoPastDue = 1
    }
    public enum ItemType
    {
        //(0-装备,1-消耗品,2材料,3宝箱)

        Equip = 0,
        Consume = 1,
        Material = 2,
        Task = 3,
        Gem = 4,
        SkillBook = 5,
        SkillGoods = 6,
        NumberGoods = 7,
        Chest = 8,
        TimeLimit = 9,
        Currency = 999,
    }

}

image

posted @ 2017-03-02 14:50  盘子脸  阅读(1303)  评论(0编辑  收藏  举报