1. 程式人生 > >Unity3D自學筆記——角色屬性設計分析

Unity3D自學筆記——角色屬性設計分析

角色屬性設計分析

目前資料庫有兩張表與角色屬性有關heroinfo 和 userinfo
這裡寫圖片描述
heroinfo
存放英雄的初始屬性,和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>();

            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();
        }

測試結果
這裡寫圖片描述