XNA之RPG游戏开发教程之九

在完成了游戏角色的选择,地图的绘制后,下一步我们就要给游戏添加角色了。想必大家都认为上一节中我们在StartMenuScreen页面上已经选择了玩家角色,并实现了根据选择的角色再游戏页面上呈现。但是,我们所做的仅仅是把一副图片加载到游戏中,而这个角色所具有的相关特性确是没有。玩游戏的朋友都知道,每个角色再游戏中都有自身一些独特的属性,像杀伤力,生命值,法宝等等,而这些都需要我们以类的形式去定义并实例化的。好了,下面就开始我们的代码操作

首先在RpgLibrary项目下添加一个新的文件夹CharacterClasses,在其中添加这些角色相关的属性类,第一个类就是角色属性类AttributePair类,在游戏中,一个角色它的属性往往也由很多的操作构成,例如怪物的法力属性,就要根据怪物等级的不同赋予不同的值,在玩家打怪的过程中,被打怪兽的生命力要不断下降,这些对属性的操作都可以封装起来,使得游戏逻辑更加清晰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class AttributePair
    {
        #region Field Region
        int currentValue;//属性的当前值
        int maximumValue;//属性可被赋予的最大值
        #endregion
        #region Property Region//将这两个属性设置为只读属性
        public int CurrentValue
        {
            get { return currentValue; }
        }
        public int MaximumValue
        {
            get { return maximumValue; }
        }
        public static AttributePair Zero//针对属性值的零值
        {
            get { return new AttributePair(); }
        }
        #endregion
        #region Constructor Region
        private AttributePair()
        {
            currentValue = 0;
            maximumValue = 0;
        }
        public AttributePair(int maxValue)
        {
            currentValue = maxValue;
            maximumValue = maxValue;
        }
        #endregion
        #region Method Region//有关属性值的常见操作,Heal加生命值;Damage被虐,生命值减少;SetCurrent进入新的游戏场景,赋予新的属性值;
        public void Heal(ushort value)
        {
            currentValue += value;
            if (currentValue > maximumValue)
                currentValue = maximumValue;
        }
        public void Damage(ushort value)
        {
            currentValue -= value;
            if (currentValue < 0)
                currentValue = 0;
        }
        public void SetCurrent(int value)
        {
            currentValue = value;
            if (currentValue > maximumValue)
                currentValue = maximumValue;
        }
        public void SetMaximum(int value)//给MaximumValue这个属性字段赋值
        {
            maximumValue = value;
            if (currentValue > maximumValue)
                currentValue = maximumValue;
        }
        #endregion
    }
}

定义好了游戏角色属性类,下面就开始定义游戏中实体的数据类,主要是记录所对应实体的一些游戏数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class EntityData
    {
        #region Field Region
        public string ClassName;//实体的名称,实际上我们在StartMenuScreen页面都选择了角色的名称
        public int Strength;//你有多强壮
        public int Dexterity;//你有多敏捷
        public int Cunning;//你有多聪明
        public int Willpower;//你的魔法等级和战斗力
        public int Magic;//你发出的魔法的杀伤力
        public int Constitution;//你的生命力
        public string HealthFormula;//呈现生命力,法术和战斗力
        public string StaminaFormula;
        public string MagicFormula;
        #endregion
        #region Constructor Region
        
private EntityData()
        {
        }
        #endregion
        #region Static Method Region
//将实体数据写入filename文件中 public static void ToFile(string filename) { }
//从filename文件中读取实体数据,并实例化实体数据类,并返回
public static EntityData FromFile(string filename) { EntityData entity = new EntityData(); return entity; } #endregion } }

好吧,我们已经定义好了游戏中实体所应具有的一些基本的游戏数据,接下来就是定义一个游戏实体的基类,有的人会问了,为什么还是基类,因为游戏中的角色少则两个,多则几十几百,为了简化代码,真正实现OOP中的类的抽象封装,先定义一个抽象的基类,其他具体的角色类再继承实现;添加Entity类如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public enum EntityGender { Male, Female, Unknown }//实体的性别属性枚举
    public abstract class Entity
    {
        #region Vital Field and Property Region
        protected string entityType;//实体类别
        protected EntityGender gender;//实体性别
        public string EntityType
        {
            get { return entityType; }
        }
        public EntityGender Gender
        {
            get { return gender; }
            protected set { gender = value; }
        }
        #endregion
        #region Basic Attribute and Property Region
        protected int strength;//实体所应具有的一些属性,在实体数据中都有了介绍
        protected int dexterity;
        protected int cunning;
        protected int willpower;
        protected int magic;
        protected int constitution;
        protected int strengthModifier;//属性的修正值
        protected int dexterityModifier;
        protected int cunningModifier;
        protected int willpowerModifier;
        protected int magicModifier;
        protected int constitutionModifier;
        public int Strength//实体对外的接口是修正后的属性值
        {
            get { return strength + strengthModifier; }
            protected set { strength = value; }
        }
        public int Dexterity
        {
            get { return dexterity + dexterityModifier; }
            protected set { dexterity = value; }
        }
        public int Cunning
        {
            get { return cunning + cunningModifier; }
            protected set { cunning = value; }
        }
        public int Willpower
        {
            get { return willpower + willpowerModifier; }
            protected set { willpower = value; }
        }
        public int Magic
        {
            get { return magic + magicModifier; }
            protected set { magic = value; }
        }
        public int Constitution
        {
            get { return constitution + constitutionModifier; }
            protected set { constitution = value; }
        }
        #endregion
        #region Calculated Attribute Field and Property Region
        protected AttributePair health;//Paired属性
        protected AttributePair stamina;
        protected AttributePair mana;
        public AttributePair Health
        {
            get { return health; }
        }
        public AttributePair Stamina
        {
            get { return stamina; }
        }
        public AttributePair Mana
        {
            get { return mana; }
        }
        protected int attack;//游戏的其它数据,攻击,破坏力,防卫能力
        protected int damage;
        protected int defense;
        #endregion
        #region Level Field and Property Region
        protected int level;//实体当前的级别,玩家都是奔着升级去的...
        protected long experience;//玩家经验值
        public int Level
        {
            get { return level; }
            protected set { level = value; }
        }
        public long Experience
        {
            get { return experience; }
            protected set { experience = value; }
        }
        #endregion
        #region Constructor Region//构造函数
        private Entity()
        {
            Strength = 0;
            Dexterity = 0;
            Cunning = 0;
            Willpower = 0;
            Magic = 0;
            Constitution = 0;
            health = new AttributePair(0);
            stamina = new AttributePair(0);
            mana = new AttributePair(0);
        }
        public Entity(EntityData entityData)//用游戏实体数据对象来初始化游戏实体
        {
            entityType = entityData.EntityName;
            Strength = entityData.Strength;
            Dexterity = entityData.Dexterity;
            Cunning = entityData.Cunning;
            Willpower = entityData.Willpower;
            Magic = entityData.Magic;
            Constitution = entityData.Constitution;
            health = new AttributePair(0);
            stamina = new AttributePair(0);
            mana = new AttributePair(0);
        }
        #endregion
    }
}

建立了基类现在就是我们去实现各类角色类的时候了,在我们这个游戏中有圣斗士Fighter类,怪兽rogues类,wizards术士类,以及priests牧师类.你会发现它们的实现十分相似

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class Fighter : Entity
    {
        #region Field Region
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public Fighter(EntityData entityData)
            : base(entityData)
        {
        }
        #endregion
        #region Method Region
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class Rogue : Entity
    {
        #region Field Region
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public Rogue(EntityData entityData)
            : base(entityData)
        {
        }
        #endregion
        #region Method Region
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class Priest : Entity
    {
        #region Field Region
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public Priest(EntityData entityData)
            : base(entityData)
        {
        }
        #endregion
        #region Method Region
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RpgLibrary.CharacterClasses
{
    public class Wizard : Entity
    {
        #region Field Region
        #endregion
        #region Property Region
        #endregion
        #region Constructor Region
        public Wizard(EntityData entityData)
            : base(entityData)
        {
        }
        #endregion
        #region Method Region
        #endregion
    }
}

当然上面的都是各个角色的雏形,都只是继承了基类的基本属性和操作,还没有实现个性化,明天继续吧...

posted on 2013-04-07 22:04  翠竹林  阅读(679)  评论(4编辑  收藏  举报

导航