(一)服务器端
在数据库结构中,一个角色对应多个道具物品。
(1)道具类
1.道具定义:
using SkillBridge.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Common.Data
{public enum ItemFuncition //物品功能 {RecoverHP, RecoverMP,AddBuff,AddExp,AddMoney,AddItem,AddSkillPoint,}class ItemDefine{public int ID { get; set; }public string Name { get; set; }public string Description { get; set; }public ItemType Type{ get; set; }public string Category { get; set; }public bool CanUse { get; set; }public int Price { get; set; }public int SellPrice { get; set; }public ItemFuncition Funcition { get; set; }public int Param { get; set; }public List<int> Params { get; set; }}
}
2.道具实体函数
namespace GameServer.Models
{class Item{TCharacterItem dbItem;public int ItemID;public int Count;public Item(TCharacterItem item){this.dbItem = item;this.ItemID = (short)item.ItemID;this.Count = (short)item.ItemCount;}public void Add(int count){this.Count += count;dbItem.ItemCount = this.Count;}public void Remove(int count){this.Count -= count;dbItem.ItemCount = this.Count;}public bool Use(int count = 1){return false;}//简化输出public override string ToString(){return string.Format("ID:{0},Count:{1}", this.ItemID, this.Count);}}
}
(2)道具管理器
对当前玩家的道具进行管理。
using Common;
using GameServer.Entities;
using GameServer.Models;
using GameServer.Services;
using SkillBridge.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace GameServer.Managers
{class ItemManager{Character Owner;public Dictionary<int, Item> Items = new Dictionary<int, Item>();public ItemManager(Character owner){this.Owner = owner;foreach (var item in owner.Data.Items){this.Items.Add(item.ItemID, new Item(item));}}public bool UseItem(int itemId, int count = 1) {Log.InfoFormat("[{0}]UserItem[{1}:{2}]", this.Owner.Data.ID, itemId, count);Item item = null;if(this.Items.TryGetValue(itemId,out item)){if (item.Count < count){return false;}item.Remove(count);return true;} return false;}public bool HasItem(int itemId){Item item = null;if (this.Items.TryGetValue(itemId, out item)){if (item.Count > 0){return true;}}return false;}public Item GetItem(int itemId){Item item = null;this.Items.TryGetValue(itemId, out item);Log.InfoFormat("[{0}]GetItem[{1}:{2}]", this.Owner.Data.ID, itemId, item);return item;}public bool AddItem(int itemId,int count){Item item = null;if (this.Items.TryGetValue(itemId, out item)){item.Add(count);}else{TCharacterItem dbItem = new TCharacterItem();dbItem.CharacterID = Owner.Data.ID;dbItem.Owner = Owner.Data;dbItem.ItemID = itemId;dbItem.ItemCount = count;Owner.Data.Items.Add(dbItem);item = new Item(dbItem);this.Items.Add(itemId, item);}Log.InfoFormat("[{0}]AddItem[{1}] addCount:{2}", this.Owner.Data.ID, itemId, count);DBService.Instance.Save();return true;}public bool RemoveItem(int itemId, int count){if (!this.Items.ContainsKey(itemId)){return false;}Item item = this.Items[itemId];if (item.Count < count) return false;item.Remove(count);Log.InfoFormat("[{0}]RemoveItem[{1}] removeCount:{2}", this.Owner.Data.ID, item, count);DBService.Instance.Save();return false;}public void GetItemInfos(List<NItemInfo> list){foreach(var item in this.Items){list.Add(new NItemInfo() { Id = item.Value.ItemID, Count = item.Value.Count });}}}
}
(3)角色持有物品管理器
角色持有道具管理器,且在角色创建时,读取数据库的道具数据对管理器进行初始化。
class Character{public TCharacter Data;public ItemManager ItemManager;public Character(CharacterType type,TCharacter cha){this.ItemManager = new ItemManager(this);this.ItemManager.GetItemInfos(this.Info.Items);}}
(二)客户端
1.道具实体函数
客户端不接触DB 所以直接取的协议的NItemInfo
using SkillBridge.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;namespace Models
{public class Item{public int ID;public int Count;public Item(NItemInfo item){this.ID = item.Id;this.Count = item.Count;}//简化输出public override string ToString(){return string.Format("ID:{0},Count:{1}", this.ID, this.Count);}}
}
2.道具管理器
因为只管理自己的道具,因此这里是个单例。
using Models;
using SkillBridge.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Common.Data;
public class ItemManager : Singleton<ItemManager>
{public Dictionary<int, Item> Items = new Dictionary<int, Item>();internal void Init(List<NItemInfo> items){this.Items.Clear();foreach (var info in items){Item item = new Item(info);this.Items.Add(item.ID, item);Debug.LogFormat("ItemManager Init[{0}]", item);}}public ItemDefine GetItem(int itemId){return null;}public bool UseItem(int itemId){return false;}public bool UseItem(ItemDefine item){return false;}
}
3.在进入游戏响应中初始化道具管理器
void OnGameEnter(object sender, UserGameEnterResponse response){Debug.LogFormat("OnGameEnter:{0} {1}", response.Result, response.Errormsg);if (response.Result == Result.Success){if (response.Character != null){ItemManager.Instance.Init(response.Character.Items);//添加玩家道具}}}