using System.Collections.Generic; using System.Linq; namespace ET { public class StoreComponentSystemDestroySystem: DestroySystem { public override void Destroy(StoreComponent self) { } } [FriendClass(typeof (Item))] public static class StoreComponentSystem { public static void Add(this StoreComponent self, int configId, long amount, long id = 0, bool bUpdateTime = false) { var config = AllItemConfigCategory.Instance.Get(configId); if (config.SuperpositionMax > 1) { foreach (var v in self.Children.Values) { var tempItem = (Item) v; if (tempItem.ConfigId == configId) { tempItem.Amount += amount; return; } } Item item; if (id == 0) { item = self.AddChild(); } else { item = self.AddChildWithId(id); } item.Amount = amount; item.ConfigId = configId; if (bUpdateTime) { item.CreateTime = TimeHelper.ServerUnix(); } } else { for (int i = 0; i < amount; i++) { Item item; if (id == 0) { item = self.AddChild(); } else { item = self.AddChildWithId(id); } item.Amount = 1; item.ConfigId = configId; if (bUpdateTime) { item.CreateTime = TimeHelper.ServerUnix(); } } } } public static long GetItemNum(this StoreComponent self, int configId) { var config = AllItemConfigCategory.Instance.Get(configId); if (config.SuperpositionMax > 1) { foreach (var v in self.Children.Values) { var item = (Item) v; if (item.ConfigId == configId) { return item.Amount; } } } else { long num = 0; foreach (var v in self.Children.Values) { if (((Item) v).Config == config) { num++; } return num; } } return 0; } public static bool Remove(this StoreComponent self, int configId, int amount) { if (GetItemNum(self, configId) < amount) { return false; } var config = AllItemConfigCategory.Instance.Get(configId); if (config.SuperpositionMax > 1) { foreach (var v in self.Children.Values) { var item = (Item) v; if (item.ConfigId == configId) { item.Amount -= amount; if (item.Amount <= 0) { item.Dispose(); } return true; } } } else { List removeList = new List(); foreach (var v in self.Children) { var item = (Item) v.Value; if (item.ConfigId == configId) { removeList.Add(item); if (removeList.Count == amount) { break; } } } foreach (var v in removeList) { self.RemoveChildWithId(v.Id); } } return true; } /// /// 获得仓库内指定类型(可选子类型)的所有道具 /// /// /// AllItem表内的关联表ID /// 关联表对应的子类型 /// public static List GetItemsWithType(this StoreComponent self, int mainType, int subType = -1) { if (mainType == 0) return null; var result = new List(); foreach (var v in self.Children.Values) { var item = (Item) v; var config = AllItemConfigCategory.Instance.Get(item.ConfigId); if (config.RelatedTable != mainType) continue; if (subType == -1) { result.Add(item); } else { switch (mainType) { case 1: { MaterialConfig materialConfig = MaterialConfigCategory.Instance.Get(config.RelatedId); if (materialConfig == null) continue; var types = materialConfig.Type.ToList(); if (types.Contains(subType)) { result.Add(item); } } break; } } } return result; } public static bool IsEnoughItem(this StoreComponent self, Dictionary itemDic) { foreach (var pair in itemDic) { if (self.GetItemNum(pair.Key) < pair.Value) { return false; } } return true; } public static bool IsEnoughItem(this StoreComponent self, int configId, int amount) { if (self.GetItemNum(configId) < amount) { return false; } return true; } } }