RPG游戏-道具系统
(一)服务器端
在数据库结构中,一个角色对应多个道具物品。
(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);//添加玩家道具}}}
RPG游戏-道具系统相关推荐
- RPG 游戏数值系统—1
今天讲一下做RPG游戏一个插件-RPGStatCollection,RPG游戏最主要的就是我们所谓的一些数值,就像英雄联盟一样,你选择每个角色的时候如果不带天赋和符文进去,每个角色都是存在基本的数值的 ...
- RPG 游戏数值系统—2
接着前面所讲的RPG数值系统,前面讲到怎么添加一些基本属性然后怎么通过可视化的方法给每个人物进行配置他们所有属性,接下来就是怎么知道我的属性它的值是多少,例如当角色进入攻击状态的时候我们需要知道角色的 ...
- [转载]Unity的RPG游戏装备系统的实现
原作者和链接: RPG游戏<黑暗之光>流程介绍与代码分析之(八):装备系统的实现 - s1314_JHC的博客 - CSDN博客 https://blog.csdn.net/s1314_J ...
- RPG游戏-小地图系统
1.地图资源的制作 通常有三种方式: 1)实时渲染场景: 2)预渲染顶视图+润色 3)纯美术制作 这里采用第二种方式 (1)使用图片MASK方式设置小地图的蒙层,这里的Mask图片采用的是圆形的白色图 ...
- RPG游戏-小地图系统(二)
这里对上次的代码做优化,当进入或者地图时,小地图UI也应该进行变化. 调用次序:MapController->MiniMapManager -->UIminiMap 1.MapContro ...
- RPG游戏-刷怪系统
一.数据定义 (一).刷怪规则定义: namespace Common.Data {public class SpawnRuleDefine{public int ID { get; set; }pu ...
- 游戏道具系统策划参考
......部分略去
- 浅谈RPG游戏中的属性系统设定
先来张我最喜欢的Nero和Dante的帅照!!! 最近的E3展也是让广大游戏爱好者们打开眼界,小编最喜欢的鬼泣系列也将于明年春季迎来鬼泣4的正统续作鬼泣5,再加上最近小编也在自己开发着RPG游戏,所以 ...
- Silverlight 2.5D RPG游戏技巧与特“.NET技术”效处理:(十一)AI系统
谈到人工智能(AI),这个话题就太大了:大学里有<人工智能教程>专门讲这方面的知识,什么大名鼎鼎的人工神经网络.遗传算法等等均可一窥究竟,这里如赘述似乎有些班门弄斧,我们暂且丢它一边去吧. ...
最新文章
- 利用java多线程向MongoDB中批量插入静态文件
- “堆”,栈,堆栈,队列,它们的区别?
- 网络推广外包——网络推广外包公司为每个线下商城实现“线上梦”!
- 后台开发经典书籍--mysql从入门到精通
- 使用Gson进行json数据转换list to json 和json to list
- 什么是7层负载均衡?
- [luogu3676]小清新数据结构题
- easyui plugin——etreegrid:CRUD Treegrid
- Android应用开发—Intent组件详解
- 离散数学 (II) 习题 7
- LeetCode常见题型——背包问题
- PRD文档范例,千万收藏的产品经理写作手册
- vue3:兄弟组件,跨组件传值,事件总线的通信方式(mitt / tiny-emitter)
- 【HDU4960】Another OCD Patient
- CSS基础(emmet 语法,CSS 复合选择器, 布局认知,背景样式)
- CF 613C(Necklace-构造法)
- Hbuilder x想调试时却无法检测到IOS手机,但是安卓手机能检测到,这里有解决方案
- CentOS7下使用YUM安装MySQL5.6
- DAO赛道异军突起,M-DAO的优势在哪里?
- Win10Ubuntu双系统安装教程