Unity3D自学笔记——角色属性设计分析
2016-11-03 21:38
633 查看
角色属性设计分析
目前数据库有两张表与角色属性有关heroinfo 和 userinfoheroinfo
存放英雄的初始属性,和growth(升级后增加值),如英雄2级了总血量就是
hp + 2 * hp_growth
userinfo
存放英雄的当前属性,即所有属性影响的最终结果
影响角色属性的外因
可以看出主脚的属性其实就是各种状态的计算结果,从图可以看出其实就是一个树状结构,主角就是树的根,于是我用组合模式简单实验了一下。
类图
Attribute 为虚类,主要方法就是Caculate,通过递归计算总属性(这里是hpTotal和mpTotal)
public abstract class Attribute { public int Hp { get; set; } public int Mp { get; set; } public int HpTotal { get; set; } public int MpTotal { get; set; } private Attribute m_Parent; private string m_Name; public Attribute(int hp, int mp) { this.Hp = hp; this.Mp = mp; } public Attribute(int hp, int mp, string name) : this(hp, mp) { this.m_Name = name; } public abstract void Calc(); public abstract int ChildCount(); protected void SetParent(Attribute child) { child.m_Parent = this; } public Attribute GetParent() { return this.m_Parent; } public void Show() { Calc(); Console.WriteLine(string.Format("{0} 属性为: HP {1} MP {2}",this.m_Name, this.HpTotal, this.MpTotal)); } }
AttributeComponet为节点,实现了Attribute的方法,并且可以添加节点和删除节点
public class AttributeComponent:Attribute { private List<Attribute> m_AttributeList = new List<Attribute> c777 (); public AttributeComponent(int hp, int mp) : base(hp, mp) { } public AttributeComponent(int hp, int mp, string name) : base(hp, mp, name) { } public override void Calc() { this.HpTotal = this.Hp; this.MpTotal = this.Mp; foreach (Attribute item in m_AttributeList) { //递归计算属性和 if (item.ChildCount() > 0) item.Calc(); this.HpTotal += item.HpTotal; this.MpTotal += item.MpTotal; } } public override int ChildCount() { return m_AttributeList.Count; } public void AddAttribute(Attribute attribute) { SetParent(attribute); this.m_AttributeList.Add(attribute); } public void RemoveAttribute(Attribute attribute) { m_AttributeList.Remove(attribute); } }
测试代码
[TestMethod] public void TestMethod1() { AttributeComponent basic = new AttributeComponent(50, 100, "Ali"); AttributeComponent lvUp = new AttributeComponent(50, 100, "升级增加"); AttributeComponent weapon = new AttributeComponent(10, 20, "无级弓"); AttributeComponent weaponEnchanting = new AttributeComponent(5, 5, "附魔增加"); basic.Show(); Console.WriteLine("升级啦"); Console.WriteLine("---------------------------------------------------"); lvUp.Show(); basic.AddAttribute(lvUp); basic.Show(); Console.WriteLine(); Console.WriteLine("装备了武器"); Console.WriteLine("---------------------------------------------------"); weapon.Show(); basic.AddAttribute(weapon); basic.Show(); Console.WriteLine(); Console.WriteLine("武器附魔"); Console.WriteLine("---------------------------------------------------"); weaponEnchanting.Show(); weapon.AddAttribute(weaponEnchanting); weapon.Show(); basic.Show(); Console.WriteLine(); Console.WriteLine("卸载装备"); Console.WriteLine("---------------------------------------------------"); basic.RemoveAttribute(weapon); basic.Show(); }
测试结果
相关文章推荐
- Unity3D自学笔记——架构应用(十)角色属性
- Unity3D自学笔记——UGUI背包系统(六)角色换装及属性动态改变
- Unity3D自学笔记——有限状态机(二)角色行走控制
- Unity3D自学笔记——星际2技能系统分析(三)Ability(Effect)
- Unity3D自学笔记——UGUI背包系统(五)沿用Attribute设计实现物品等级
- Unity3D自学笔记——星际2技能系统分析(二)Ability(Move, Stop, Attack)
- Unity3D自学笔记——星际2技能系统分析(五)Effect(Damage)
- Unity3D自学笔记——架构应用(七)客户端识别角色是否已经创建
- Unity3D自学笔记——星际2技能系统分析(四)Ability(Behavior)
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- Unity3D自学笔记——架构应用(六)角色创建
- 系统设计以及javascript笔记:用户行为分析研究之数据采集
- Android 属性系统设计分析
- unity3d笔记二:基于组件的设计
- 算法分析与设计基础 学习笔记 第一章
- OEA 扩展属性系统 - 任意适配 WPF Binding 的设计分析
- 自学HTML笔记之CSS属性
- 韩顺平_轻松搞定网页设计(html+css+javascript)_第30讲_类和对象细节_创建对象的几种方式_js对象内存分析_学习笔记_源代码图解_PPT文档整理
- Android自学笔记:分析onXXX事件监听器中的两个参数position和id
- sqlserver 自学笔记 函数实训 学分学期转换函数的设计