Unity3D自学笔记——UGUI背包系统(一)ItemData

ItemData 创建数据模型

效果图

Unity3D自学笔记——UGUI背包系统(一)ItemData_第1张图片

模型说明

Unity3D自学笔记——UGUI背包系统(一)ItemData_第2张图片
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:在背包里最大堆叠数量

数据库

Unity3D自学笔记——UGUI背包系统(一)ItemData_第3张图片

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
    {
        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();
            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();
            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
    {
        private readonly ItemRepository itemRepository;
        private UnitOfWork unitOfWork;

        public GetAllItemInfoHandler()
        {
            unitOfWork = new UnitOfWork();
            itemRepository = new ItemRepository(unitOfWork.Session);
        }
        public ICommandResult Excute(GetItemInfoCommond command)
        {
            IEnumerable itemInfos = itemRepository.All();
            unitOfWork.Commit();
            return new CommandResult(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 result = commandBus.Submit(new GetItemInfoCommond()) as CommandResult;
                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;
}
}
“`

你可能感兴趣的:(Unity3D)