Files
JJBB/Assets/Project/Script/Player/Item/GameItemContainer.cs

820 lines
27 KiB
C#
Raw Permalink Normal View History

2024-08-23 15:49:34 +08:00
//********************************************************************
// 文件名: GameItemContainer.cs
// 描述: 物品容器
// 作者: TangYi
// 创建时间: 2013-12-25
//
// 修改历史:
//********************************************************************
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using GCGame.Table;
using Games.GlobeDefine;
using GCGame;
using Module.Log;
namespace Games.Item
{
public class GameItemContainer
{
public enum Type
{
TYPE_INVALID = -1,
TYPE_BACKPACK,
TYPE_EQUIPPACK,
TYPE_BUYBACKPACK = 2,
TYPE_STORAGEPACK = 3,
TYPE_MAGICPACK = 4,
TYPE_ADVANCEPACK = 5,
}
// 背包默认容量
public static int SIZE_BACKPACK = -1;
// 背包最大容量
public static int MAXSIZE_BACKPACK = -1;
// 装备槽默认容量
public static int SIZE_EQUIPPACK = -1;
// 回购背包容量
public static int MAXSIZE_BUYBACKPACK = -1;
// 仓库最大容量
public static int MAXSIZE_STORAGEPACK = -1;
// 仓库默认容量
public static int SIZE_STORAGEPACK = -1;
// 法宝槽默认容量
public static int SIZE_MAGICPACK = -1;
//坐骑装备默认容量
public static int SIZE_ADVANCPACK = -1;
public static void InitStaticParams()
{
if (SIZE_BACKPACK > 0)
return;
SIZE_BACKPACK = SystemParam.GetSystemParam_INT(62);
MAXSIZE_BACKPACK = SystemParam.GetSystemParam_INT(63);
SIZE_EQUIPPACK = SystemParam.GetSystemParam_INT(64);
MAXSIZE_BUYBACKPACK = SystemParam.GetSystemParam_INT(65);
MAXSIZE_STORAGEPACK = SystemParam.GetSystemParam_INT(66);
SIZE_STORAGEPACK = SystemParam.GetSystemParam_INT(67);
SIZE_MAGICPACK = SystemParam.GetSystemParam_INT(68);
SIZE_ADVANCPACK = SystemParam.GetSystemParam_INT(69 );
}
private List<GameItem> m_Items = new List<GameItem>();
public List<GameItem> Items
{
get
{
return m_Items;
}
}
private int m_ContainerSize = 0;
public GameItemContainer(int nSize, Type nType)
{
InitStaticParams();
m_ContainerSize = nSize;
m_ContainerType = nType;
for (int i = 0; i < m_ContainerSize; ++i)
{
m_Items.Add(new GameItem());
}
}
/// <summary>
/// 容器大小
/// </summary>
public int ContainerSize
{
get { return m_ContainerSize; }
}
public void AddContainerSize(int nAdd)
{
m_ContainerSize += nAdd;
for (int i = 0; i < nAdd; ++i)
{
m_Items.Add(new GameItem());
}
}
/// <summary>
/// 容器可用大小
/// </summary>
public int GetCanContainerSize()
{
int Num = 0;
for (int i = 0; i < m_Items.Count; ++i)
{
if (m_Items[i].IsValid() == false)
{
Num++;
}
}
return Num;
}
/// <summary>
/// 容器类型
/// </summary>
private Type m_ContainerType = Type.TYPE_INVALID;
public GameItemContainer.Type ContainerType
{
get { return m_ContainerType; }
}
/// <summary>
/// 取得物品
/// </summary>
public virtual GameItem GetItem(int slot)
{
if (slot >= 0 && slot < m_Items.Count)
{
return m_Items[slot];
}
return null;
}
public int GetItemIdx(GameItem gameItem)
{
return m_Items.IndexOf(gameItem);
}
/// <summary>
/// 根据GUID取得物品
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public GameItem GetItemByGuid(UInt64 guid)
{
if (guid == GlobeVar.INVALID_GUID)
{
return null;
}
for (int i = 0; i < m_Items.Count; ++i )
{
if (m_Items[i].Guid == guid)
{
return m_Items[i];
}
}
return null;
}
/// <summary>
/// 更新物品
/// </summary>
/// <param name="slot">槽位</param>
/// <param name="item">物品</param>
public bool UpdateItem(int slot, GameItem item)
{
bool bRet = false;
if (slot >= 0 && slot < m_Items.Count)
{
m_Items[slot] = item;
bRet = true;
}
return bRet;
}
/// <summary>
/// 有效物品数量
/// </summary>
/// <returns></returns>
public int GetItemCount()
{
int count = 0;
for (int i = 0; i < m_Items.Count; ++i)
{
GameItem item = m_Items[i];
if (item.IsValid())
{
++count;
}
}
return count;
}
/// <summary>
/// 取得指定ID物品数量
/// </summary>
/// <returns></returns>
public int GetItemCountByDataId(int dataid)
{
int count = 0;
if (dataid >= 0)
{
for (int i = 0; i < m_Items.Count; ++i)
{
if (m_Items[i].DataID == dataid)
{
count = count + m_Items[i].StackCount;
}
}
}
return count;
}
/// <summary>
/// 取得指定类型物品数量
/// </summary>
/// <returns></returns>
public int GetFellowItemCountByClass(int classID, int subClassID)
{
int count = 0;
for (int i = 0; i < m_Items.Count; ++i)
{
Tab_CommonItem tabItem = TableManager.GetCommonItemByID(m_Items[i].DataID, 0);
if (tabItem == null)
continue;
if (tabItem.ClassID == classID && tabItem.SubClassID == subClassID)
{
Tab_FellowHandBook _FellowBook = TableManager.GetFellowHandBookByID(m_Items[i].DataID * 100 + 1, 0);
if(_FellowBook != null)
count = count + m_Items[i].StackCount;
}
}
return count;
}
/// <summary>
/// 取得指定类型物品数量
/// </summary>
/// <returns></returns>
public int GetItemCountByClass(int classID, int subClassID)
{
int count = 0;
for (int i = 0; i < m_Items.Count; ++i)
{
Tab_CommonItem tabItem = TableManager.GetCommonItemByID(m_Items[i].DataID, 0);
if (tabItem == null)
continue;
if (tabItem.ClassID == classID && tabItem.SubClassID == subClassID)
{
count = count + m_Items[i].StackCount;
}
}
return count;
}
/// <summary>
/// 取得指定ID绑定物品数量
/// </summary>
/// <returns></returns>
public int GetBindItemCountByDataId(int dataid)
{
int count = 0;
if (dataid >= 0)
{
for (int i = 0; i < m_Items.Count; ++i)
{
if (m_Items[i].DataID == dataid && m_Items[i].BindFlag)
{
count = count + m_Items[i].StackCount;
}
}
}
return count;
}
/// <summary>
/// 根据DataID找到第一个物品的guid
/// </summary>
/// <param name="dataID"></param>
/// <returns></returns>
public UInt64 GetItemGuidByDataID(int dataID)
{
if (dataID >= 0)
{
for (int i = 0; i < m_Items.Count; ++i)
{
if (m_Items[i].DataID == dataID)
{
return m_Items[i].Guid;
}
}
}
return GlobeVar.INVALID_GUID;
}
/// <summary>
/// 根据DataID找到第一个物品的guid
/// </summary>
/// <param name="dataID"></param>
/// <returns></returns>
public GameItem GetItemByDataID(int dataID)
{
if (dataID >= 0)
{
for (int i = 0; i < m_Items.Count; ++i)
{
if (m_Items[i].DataID == dataID)
{
return m_Items[i];
}
}
}
return null;
}
/// <summary>
/// 取得物品列表
/// </summary>
/// <returns></returns>
public List<GameItem> GetList()
{
return m_Items;
}
}
//进阶装备槽位
public enum AdvanceSlot
{
//坐骑(后面有需要再添加)
Slot_MATAO = 0,
Slot_MABIAN = 1,
Slot_MAAN = 2,
Slot_MADENG= 3,
}
/// <summary>
/// 装备槽位类型
/// </summary>
public enum EquipPackSlot
{
Slot_WEAPON = 0, //武器
Slot_HEAD, //头部
Slot_ARMOR, //上衣
Slot_LEG_GUARD, //下衣
Slot_CUFF, //护腕
Slot_SHOES, //鞋子
Slot_NECK, //吊坠
Slot_RING, //戒指
Slot_AMULET, //护符
Slot_BELT, //腰带
Slot_NUM, //数量
};
public enum GemSlot
{
OPEN_NUM = 3, //宝石槽位数量
}
class ItemTool
{
/// <summary>
/// 过滤出需要类型的物品
/// </summary>
/// <param name="Container"></param>
/// <param name="nClass"></param>
/// <param name="nSubClass"></param>
/// <returns></returns>
static public List<GameItem> ItemFilter(GameItemContainer Container, int nClass, int nSubClass = 0, bool repeat = true,int backPackType=0)
{
List<GameItem> resultlist = new List<GameItem>();
for (int nIndex = 0; nIndex < Container.ContainerSize; ++nIndex)
{
GameItem item = Container.GetItem(nIndex);
if (item.IsValid())
{
Tab_CommonItem tabItem = TableManager.GetCommonItemByID(item.DataID, 0);
if (null != tabItem)
{
int itemclass = tabItem.ClassID;
int itemsubclass = tabItem.SubClassID;
int itemBackPackType = tabItem.BackPackType;
if ((itemclass == nClass || nClass == 0) &&(itemsubclass == nSubClass || nSubClass == 0)&&(itemBackPackType==backPackType|| backPackType==0))
{
if (!repeat)
{
var findItem = resultlist.Find((containItem) =>
{
if (containItem.DataID == item.DataID)
return true;
return false;
});
if (findItem != null)
continue;
}
resultlist.Add(item);
}
}
}
}
return ItemSort(resultlist);
}
static public List<GameItem> GetPageItem(GameItemContainer container, int posStart, int posEnd)
{
List<GameItem> resultList = new List<GameItem>();
for (int nIndex = posStart; nIndex < posEnd; ++nIndex)
{
GameItem item = container.GetItem(nIndex);
resultList.Add(item);
}
return resultList;
}
/// <summary>
/// 筛选出高于品质的物品
/// </summary>
/// <param name="ItemList"></param>
/// <param name="nQuality"></param>
/// <returns></returns>
static public List<GameItem> ItemFilter(List<GameItem> ItemList, int nQuality)
{
List<GameItem> resultlist = new List<GameItem>();
for (int nIndex = 0; nIndex < ItemList.Count; ++nIndex)
{
GameItem item = ItemList[nIndex];
if (item.IsValid())
{
Tab_CommonItem tabItem = TableManager.GetCommonItemByID(item.DataID, 0);
if (null != tabItem)
{
int quality = tabItem.Quality;
if (quality >= nQuality)
{
resultlist.Add(item);
}
}
}
}
return ItemSort(resultlist);
}
/// <summary>
/// 背包显示排序
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
static public List<GameItem> ItemSort(List<GameItem> list)
{
for (int i = 0; i < list.Count - 1; ++i)
{
int flag = 0;
for (int j = 0; j < list.Count - i - 1; ++j)
{
//排序等级高的排前面
if (list[j].SortId() < list[j + 1].SortId())
{
GameItem temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
flag = 1;
}
else if (list[j].SortId() == list[j + 1].SortId())
{
//装备
if (list[j].IsEquipMent() && list[j + 1].IsEquipMent())
{
//装备等级小的排后面
if (list[j].GetEquipLevel() < list[j + 1].GetEquipLevel())
{
GameItem temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
flag = 1;
}
}
//非装备
else
{
//Guid小的排后面
if (list[j].Guid < list[j + 1].Guid)
{
GameItem temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
flag = 1;
}
}
}
}
if (flag == 0)
{
break;
}
}
return list;
}
static public string GetStarColourSprite(int value)
{
switch (value)
{
case 0:
return "WhiteStar";
case 1:
return "GreenStar";
case 2:
return "BlueStar";
case 3:
return "PurpleStar";
case 4:
return "OrangeStar";
default:
break;
}
return "";
}
static public string GetStarColor(int starLevel)
{
string strColor = "[FFFFFF]";
if (starLevel >= 0 && starLevel <= 12)
{
strColor = "[FFFFFF]";
}
else if (starLevel >= 13 && starLevel <= 24)
{
strColor = "[33CC66]";
}
else if (starLevel >= 25 && starLevel <= 36)
{
strColor = "[33CCFF]";
}
else if (starLevel >= 37 && starLevel <= 48)
{
strColor = "[CC66FF]";
}
else if (starLevel >= 49 && starLevel <= 60)
{
strColor = "[FF9933]";
}
return strColor;
}
static public string GetGemAttr(int GemId)
{
Tab_GemAttr lineGemAttr = TableManager.GetGemAttrByID(GemId, 0);
if (lineGemAttr != null)
{
if (lineGemAttr.MaxHP > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.MAXHP), lineGemAttr.MaxHP);
}
if (lineGemAttr.MaxMP > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.MAXMP), lineGemAttr.MaxMP);
}
if (lineGemAttr.PysAttack > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.PYSATTACK), lineGemAttr.PysAttack);
}
if (lineGemAttr.MagAttack > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.MAGATTACK), lineGemAttr.MagAttack);
}
if (lineGemAttr.PysDef > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.PYSDEF), lineGemAttr.PysDef);
}
if (lineGemAttr.MagDef > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.MAGDEF), lineGemAttr.MagDef);
}
if (lineGemAttr.Hit > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.HIT), lineGemAttr.Hit);
}
if (lineGemAttr.Dodge > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.DODGE), lineGemAttr.Dodge);
}
if (lineGemAttr.Critical > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.CRITICAL), lineGemAttr.Critical);
}
if (lineGemAttr.Decritical > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.DECRITICAL), lineGemAttr.Decritical);
}
if (lineGemAttr.Strike > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.STRIKE), lineGemAttr.Strike);
}
if (lineGemAttr.Ductical > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.DUCTICAL), lineGemAttr.Ductical);
}
if (lineGemAttr.CritiAdd > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.CRITIADD), lineGemAttr.CritiAdd);
}
if (lineGemAttr.CritiMis > 0)
{
return string.Format("{0} + {1}", ItemTool.ConvertAttrToString(COMBATATTE.CRITIMIS), lineGemAttr.CritiMis);
}
}
return "";
}
static public string ConvertAttrToString(COMBATATTE attr)
{
switch (attr)
{
case COMBATATTE.MAXHP:
return Utils.GetDicByID(1573);//生命
case COMBATATTE.MAXMP:
return Utils.GetDicByID(1574);//"法力";
case COMBATATTE.MAXXP:
return Utils.GetDicByID(1575);//"战意";
case COMBATATTE.PYSATTACK:
return Utils.GetDicByID(1576);//"外功攻击";
case COMBATATTE.MAGATTACK:
return Utils.GetDicByID(1577);//"内功攻击";
case COMBATATTE.PYSDEF:
return Utils.GetDicByID(1578);//"外功防御";
case COMBATATTE.MAGDEF:
return Utils.GetDicByID(1579);//"内功防御";
case COMBATATTE.HIT:
return Utils.GetDicByID(1580);//"命中";
case COMBATATTE.DODGE:
return Utils.GetDicByID(1581);//"闪避";
case COMBATATTE.CRITICAL:
return Utils.GetDicByID(1582);//"暴击";
case COMBATATTE.DECRITICAL:
return Utils.GetDicByID(1583);//"暴击抗性";
case COMBATATTE.STRIKE:
return Utils.GetDicByID(1584);//"穿透";
case COMBATATTE.DUCTICAL:
return Utils.GetDicByID(1585);//"韧性";
case COMBATATTE.CRITIADD:
return Utils.GetDicByID(1586);//"暴击加成";
case COMBATATTE.CRITIMIS:
return Utils.GetDicByID(1587);//"暴击减免";
case COMBATATTE.MOVESPEED:
return Utils.GetDicByID(1588);//"移动速度";
case COMBATATTE.ATTACKSPEED:
return Utils.GetDicByID(1589);//"攻击速度";
default:
break;
}
switch ((int)attr)
{
case 1000:
return Utils.GetDicByID(1590);//"全攻击";
case 1001:
return Utils.GetDicByID(1591);//"全防御";
default:
break;
}
return "";
}
/// <summary>
/// 取得装备槽位类型 1攻击 2防御
/// </summary>
/// <param name="slot"></param>
/// <returns></returns>
static public int GetEquipSlotType(int slot)
{
switch (slot)
{
case (int)EquipPackSlot.Slot_WEAPON:
return 1;
case (int)EquipPackSlot.Slot_HEAD:
return 2;
case (int)EquipPackSlot.Slot_ARMOR:
return 2;
case (int)EquipPackSlot.Slot_LEG_GUARD:
return 2;
case (int)EquipPackSlot.Slot_SHOES:
return 2;
case (int)EquipPackSlot.Slot_CUFF:
return 2;
case (int)EquipPackSlot.Slot_NECK:
return 1;
case (int)EquipPackSlot.Slot_RING:
return 1;
case (int)EquipPackSlot.Slot_AMULET:
return 1;
case (int)EquipPackSlot.Slot_BELT:
return 1;
default:
break;
}
return 0;
}
/// <summary>
/// 装备背包 UI显示顺序 转换为 数据容器顺序
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
static public int GetEquipSlotByUIIndex(int index)
{
switch (index)
{
case 0: return (int)EquipPackSlot.Slot_WEAPON;
case 1: return (int)EquipPackSlot.Slot_HEAD;
case 2: return (int)EquipPackSlot.Slot_RING;
case 3: return (int)EquipPackSlot.Slot_ARMOR;
case 4: return (int)EquipPackSlot.Slot_NECK;
case 5: return (int)EquipPackSlot.Slot_CUFF;
case 6: return (int)EquipPackSlot.Slot_AMULET;
case 7: return (int)EquipPackSlot.Slot_LEG_GUARD;
case 8: return (int)EquipPackSlot.Slot_BELT;
case 9: return (int)EquipPackSlot.Slot_SHOES;
default:
break;
}
return -1;
}
/// <summary>
/// 装备背包 数据容器顺序 转换为 UI显示顺序
/// </summary>
/// <param name="slot"></param>
/// <returns></returns>
static public int GetUIIndexByEquipSlot(int slot)
{
switch (slot)
{
case (int)EquipPackSlot.Slot_WEAPON: return 0;
case (int)EquipPackSlot.Slot_HEAD: return 1;
case (int)EquipPackSlot.Slot_RING: return 2;
case (int)EquipPackSlot.Slot_ARMOR: return 3;
case (int)EquipPackSlot.Slot_NECK: return 4;
case (int)EquipPackSlot.Slot_CUFF: return 5;
case (int)EquipPackSlot.Slot_AMULET: return 6;
case (int)EquipPackSlot.Slot_LEG_GUARD: return 7;
case (int)EquipPackSlot.Slot_BELT: return 8;
case (int)EquipPackSlot.Slot_SHOES: return 9;
default:
break;
}
return 0;
}
public static List<Tab_CommonItem> GetFilterSellItem(string containerName)
{
List<Tab_CommonItem> commonItems = new List<Tab_CommonItem>();
var tabItems = TableManager.GetUserShopItem();
foreach (var tabItem in tabItems)
{
var commonItem = TableManager.GetCommonItemByID(tabItem.Key, 0);
if (commonItem.Name.Contains(containerName))
{
commonItems.Add(commonItem);
}
}
return commonItems;
}
public static List<Tab_CommonItem> GetFilterCommonItem(int lvMin, int lvMax, int type, int subType, int quality, int profession)
{
List<Tab_CommonItem> commonItems = new List<Tab_CommonItem>();
var tabItems = TableManager.GetUserShopItem();
foreach (var tabItem in tabItems)
{
var commonItem = TableManager.GetCommonItemByID(tabItem.Key, 0);
if (commonItem == null || commonItem.MinLevelRequire < lvMin || commonItem.MinLevelRequire > lvMax)
continue;
if (subType!= -1 && commonItem.SubClassID != subType)
continue;
if (commonItem.Quality != quality)
continue;
if (commonItem.ClassID != type)
continue;
//if ((type == 1 && subType == 1 && tabItem.Value[0].ProfessionRequire != 1)
// || (type == 1 && subType == 2 && tabItem.Value[0].ProfessionRequire != 2)
// || (type == 1 && subType == 3 && tabItem.Value[0].ProfessionRequire != 4)
// || (type == 1 && subType == 4 && tabItem.Value[0].ProfessionRequire != 8))
// continue;
if (profession >= 0 && ((commonItem.ProfessionRequire >> profession) & 1) == 0)
continue;
{
commonItems.Add(commonItem);
}
}
return commonItems;
}
};
}