You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
6.4 KiB
214 lines
6.4 KiB
3 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
|
||
|
namespace ET
|
||
|
{
|
||
|
public class StoreComponentSystemDestroySystem : DestroySystem<StoreComponent>
|
||
|
{
|
||
|
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<Item>();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
item = self.AddChildWithId<Item>(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<Item>();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
item = self.AddChildWithId<Item>(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<Item> removeList = new List<Item>();
|
||
|
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;
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获得仓库内指定类型(可选子类型)的所有道具
|
||
|
/// </summary>
|
||
|
/// <param name="self"></param>
|
||
|
/// <param name="mainType">AllItem表内的关联表ID</param>
|
||
|
/// <param name="subType">关联表对应的子类型</param>
|
||
|
/// <returns></returns>
|
||
|
public static List<Item> GetItemsWithType(this StoreComponent self, int mainType, int subType = -1)
|
||
|
{
|
||
|
if (mainType == 0) return null;
|
||
|
var result = new List<Item>();
|
||
|
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<int, int> itemDic)
|
||
|
{
|
||
|
foreach (var pair in itemDic)
|
||
|
{
|
||
|
if (self.GetItemNum(pair.Key) < pair.Value)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|