1. 程式人生 > >Unity3D自學筆記——UGUI揹包系統(一)ItemData

Unity3D自學筆記——UGUI揹包系統(一)ItemData

ItemData 建立資料模型

效果圖

這裡寫圖片描述

模型說明

這裡寫圖片描述
ID:不是自增列,約束命名規則方便排序及長著,ID命名規則為,ItemType + HeroType + Index,如300001,代表武器 + 所有角色都能使用 + 001號
這裡寫圖片描述
Price:購買價格,售價就是購買價格乘以固定比例
IconPath: Resouces圖片檔案目錄相對路徑
ItemType: 型別,對應列舉變數
0: Other 10:Potion 20:Stuff 30:Weapon 31:Armor 32:Ring
33:Necklace
跳號進行編寫列舉是為了方便擴充套件
HeroType: 哪些英雄可以使用
CanLvUp

: 是否可以升級
Lv:基礎等級
Max:最大等級
Hp:基礎屬性,該Item給持有角色帶來的額外附加屬性
HpGrowth:每升一級增加屬性
StackCount:在揹包裡最大堆疊數量

資料庫

這裡寫圖片描述

CREATE TABLE `iteminfo` (
  `Id` int(11) NOT NULL DEFAULT '0' COMMENT 'ItemType + HeroTyp + Index',
  `name` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `itemType` int
(11) DEFAULT '0' COMMENT '0: Other 10:Potion 20:Stuff 30:Weapon 31:Armor 32:Ring 33:Necklace', `price` int(11) DEFAULT '0', `iconPath` varchar(255) DEFAULT NULL, `heroType` int(11) DEFAULT '0' COMMENT '0: All, 1: hero1, 2: hero2', `lv` int(11) DEFAULT '1', `maxLv` int(11) DEFAULT '1', `hp` int(11
) DEFAULT '0', `mp` int(11) DEFAULT '0', `atk` int(11) DEFAULT '0', `def` int(11) DEFAULT '0', `spd` int(11) DEFAULT '0', `hit` int(11) DEFAULT '0', `criticalRate` double(5,2) DEFAULT '0.00', `atkSpd` double(5,2) DEFAULT '0.00', `atkRange` double(5,2) DEFAULT '0.00', `MoveSpd` double(5,2) DEFAULT '0.00', `hp_growth` int(11) DEFAULT '0', `mp_growth` int(11) DEFAULT '0', `atk_growth` int(11) DEFAULT '0', `def_growth` int(11) DEFAULT '0', `spd_growth` int(11) DEFAULT '0', `hit_growth` int(11) DEFAULT '0', `criticalRate_growth` double(5,2) DEFAULT '0.00', `atkSpd_growth` double(5,2) DEFAULT '0.00', `atkRange_growth` double(5,2) DEFAULT '0.00', `moveSpd_growth` double(5,2) DEFAULT '0.00', `canLvUp` bit(1) DEFAULT b'0', `stackCount` int(11) DEFAULT '1', PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

服務端

建立ItemEntity
步驟參見之前的文章,不贅述

public enum ItemType
    {
        Other = 0,
        Potion = 10,
        Stuff = 20,
        Weapon = 30,
        Armor = 31,
        Ring = 32,
        Necklace = 33
    }

哪些英雄可以使用

 public enum HeroType
    {
        All = 0,
        Unity = 1,
        Sapphi = 2
    }

CanLvUp 表示該Item可以進行升級,升級後才計算Growth屬性,沿用前面的Attribute設計
StackCount 最大堆疊數量

 public class ItemEntity : IEntityIntKey
    {
        public virtual ItemType ItemType { get; set; }
        public virtual HeroType HeroType { get; set; }
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual int Price { get; set; }
        public virtual string IconPath { get; set; }
        public virtual int Lv { get; set; }
        public virtual int MaxLv { get; set; }
        public virtual int Hp { get; set; }
        public virtual int Mp { get; set; }
        public virtual int Atk { get; set; }
        public virtual int Def { get; set; }
        public virtual int Spd { get; set; }
        public virtual int Hit { get; set; }
        public virtual double CriticalRate { get; set; }
        public virtual double AtkSpd { get; set; }
        public virtual double AtkRange { get; set; }
        public virtual double MoveSpd { get; set; }
        public virtual int HpGrowth { get; set; }
        public virtual int MpGrowth { get; set; }
        public virtual int AtkGrowth { get; set; }
        public virtual int DefGrowth { get; set; }
        public virtual int SpdGrowth { get; set; }
        public virtual int HitGrowth { get; set; }
        public virtual double CriticalRateGrowth { get; set; }
        public virtual double AtkSpdGrowth { get; set; }
        public virtual double AtkRangeGrowth { get; set; }
        public virtual double MoveSpdGrowth { get; set; }
        public virtual bool CanLvUp { get; set; }
        public virtual int StackCount { get; set; }
    }

Map
注意列舉型別的對映,後面加CustomType

public class ItemMap : ClassMap<ItemEntity>
    {
        public ItemMap()
        {
            Table("iteminfo");
            Id(x => x.ID).Column("id");
            Map(x => x.Name).Column("name");
            Map(x => x.Description).Column("description");
            Map(x => x.IconPath).Column("iconPath");
            Map(x => x.ItemType).Column("itemType").CustomType<int>();
            Map(x => x.Price).Column("price");
            Map(x => x.Lv).Column("lv");
            Map(x => x.MaxLv).Column("maxLv");
            Map(x => x.HeroType).Column("heroType").CustomType<int>();
            Map(x => x.Hp).Column("hp");
            Map(x => x.Mp).Column("mp");
            Map(x => x.Atk).Column("atk");
            Map(x => x.Def).Column("def");
            Map(x => x.Spd).Column("spd");
            Map(x => x.Hit).Column("hit");
            Map(x => x.CriticalRate).Column("criticalRate");
            Map(x => x.AtkSpd).Column("atkSpd");
            Map(x => x.AtkRange).Column("atkRange");
            Map(x => x.MoveSpd).Column("moveSpd");
            Map(x => x.HpGrowth).Column("hp_growth");
            Map(x => x.MpGrowth).Column("mp_growth");
            Map(x => x.AtkGrowth).Column("atk_growth");
            Map(x => x.DefGrowth).Column("def_growth");
            Map(x => x.SpdGrowth).Column("spd_growth");
            Map(x => x.HitGrowth).Column("hit_growth");
            Map(x => x.CriticalRateGrowth).Column("criticalRate_growth");
            Map(x => x.AtkSpdGrowth).Column("atkSpd_growth");
            Map(x => x.AtkRangeGrowth).Column("atkRange_growth");
            Map(x => x.MoveSpdGrowth).Column("moveSpd_growth");
            Map(x => x.CanLvUp).Column("canLvUp");
            Map(x => x.StackCount).Column("stackCount");
        }
    }

Command

public class GetItemInfoCommond : ICommand
    {
        public int ID { get; set; }
    }

Handler
可以參考獲取英雄的程式碼

public class GetAllItemInfoHandler : ICommandHandler<GetItemInfoCommond>
    {
        private readonly ItemRepository itemRepository;
        private UnitOfWork unitOfWork;

        public GetAllItemInfoHandler()
        {
            unitOfWork = new UnitOfWork();
            itemRepository = new ItemRepository(unitOfWork.Session);
        }
        public ICommandResult Excute(GetItemInfoCommond command)
        {
            IEnumerable<ItemEntity> itemInfos = itemRepository.All();
            unitOfWork.Commit();
            return new CommandResult<ItemEntity>(true, itemInfos);
        }
    }

OprationCode

 public enum OperationCode : byte
    {
        Register,
        Login,
        GetHeroInfo,
        CreateHero,
        GetItemInfo,
    }
}

AliPeer

protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            if (AliPeer.log.IsDebugEnabled)
            {
                AliPeer.log.DebugFormat("OnOperationRequest. Code={0}", operationRequest.OperationCode);
            }
            switch (operationRequest.OperationCode)
            {
                case (byte)OperationCode.Register:
                    HandleRegisterOperation(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.Login:
                    HandleLoginOperation(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.GetHeroInfo:
                    HandleGetHeroinfoOperation(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.CreateHero:
                    HandleCreateHeroOperation(operationRequest, sendParameters);
                    break;
                case (byte)OperationCode.GetItemInfo:
                    HandleGetItemInfoOperation(operationRequest, sendParameters);
                    break;
                default:
                    break;
            }
        }

protected virtual void HandleGetItemInfoOperation(OperationRequest operationRequest, SendParameters sendParameters)
        {
            try
            {
                OperationResponse response = new OperationResponse((byte)OperationCode.GetItemInfo);
                CommandResult<ItemEntity> result = commandBus.Submit(new GetItemInfoCommond()) as CommandResult<ItemEntity>;
                if (result.Success)
                {
                    response.ReturnCode = (short)ReturnCode.Sucess;
                    Dictionary<byte, object> dict = new Dictionary<byte, object>();
                    string strJson = JsonMapper.ToJson(result.Result);
                    dict.Add((byte)ReturnCode.Sucess, strJson);
                    response.Parameters = dict;
                }
                else
                {
                    response.ReturnCode = (short)ReturnCode.Faild;
                }

                SendOperationResponse(response, sendParameters);
            }
            catch (Exception ex)
            {
                if (AliPeer.log.IsDebugEnabled)
                {
                    AliPeer.log.DebugFormat("Error: {0}", ex.Message);
                }
            }
        }

客戶端

因為是基礎資料,所以在遊戲開始執行就進行初始化並快取
所以在GameMain中增加程式碼

    IEnumerator LoadData()
    {
        GetAllHeroInfo();
        GetAllItemInfo();
        yield return null;
    }

    public void GetAllItemInfo()
    {
        if (PhotonManager.Instance.IsConnected)
        {
            Dictionary<byte, object> parameter = new Dictionary<byte, object>();
            parameter.Add((byte)OperationCode.GetItemInfo, null);
            PhotonManager.Instance.Peer.OpCustom((byte)OperationCode.GetItemInfo, parameter, true);
        }
    }

在獲取資料後,進行快取
PhotonManager

public void OnOperationResponse(OperationResponse operationResponse)
{
switch ((OperationCode)operationResponse.OperationCode)
{
case OperationCode.Login:
HandleLoginResponse(operationResponse);
break;
case OperationCode.Register:
HandleRegisterResponse(operationResponse);
break;
case OperationCode.GetHeroInfo:
HandleGetHeroInfoResponse(operationResponse);
break;
case OperationCode.CreateHero:
HandleCreateHeroResponse(operationResponse);
break;
case OperationCode.GetItemInfo:
HandleGetItemInfoResponse(operationResponse);
break;
}
}

private void HandleGetItemInfoResponse(OperationResponse operationResponse)
{
switch ((ReturnCode)operationResponse.ReturnCode)
{
case ReturnCode.Sucess:
object json;
operationResponse.Parameters.TryGetValue((byte)ReturnCode.Sucess, out json);
if (json != null)
{
JsonData data = JsonMapper.ToObject(json.ToString());
new JsonToEntity(PhotonCacheType.ItemList, data).BuildEntityToCache();
}
var cache = PhotonDataCache.GetAll(PhotonCacheType.ItemList);
if(cache != null && cache.Count > 0)
{
Debug.Log(“物品資料初始化成功”);
}
else
{
Debug.Log(“物品資料初始化失敗”);
}
break;
}
}
“`