使用 NPC,NPCManager 在 XNA 中创建 NPC
使用 NPC,NPCManager 在 XNA 中创建 NPC
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)
NPC
NPC 是游戏中重要的内容,也就是非玩家控制单位。所以,平方创建了 NPC 类和其他相关的类。下面是 NPC 类的一些字段。
静态字段 InjuredSoundName 和 DeadSoundName 表示 NPC 受伤的声音和死亡的声音。由于使用静态字段,所以所有的 NPC 都将使用此声音。
事件 Injured 表示 NPC 受伤之后,你可以在这个事件中让 NPC 显示受伤的效果。
字段 frameCount 用来计算时间,将根据 frameCount 来执行动作。
internal static string InjuredSoundName;
internal static string DeadSoundName; internal event EventHandler<NPCEventArgs> Injured; private long frameCount = 0; protected readonly List<NPCAction> actions = new List<NPCAction> ( );
protected readonly List<NPCAction> loopActions = new List<NPCAction> ( );
private int currentActionIndex;
protected int life;
protected int protoLife;
internal bool IsDied = false; internal NPCManager Manager; protected NPC ( IPlayScene scene, int type, Vector2 location, string movieName, string extendMovieName, float speed, int angle, HitArea hitArea, int width, int height, int life, IList<NPCAction> actions, double destroySecond, bool isMovieRotable, bool isAreaLimited, bool isAreaEntered, double areaSecond )
: base ( scene, type, location, movieName, extendMovieName, speed, angle, hitArea, width, height, destroySecond, isMovieRotable, isAreaLimited, isAreaEntered, areaSecond )
{ this.life = life <= 0 ? 1 : life;
this.protoLife = this.life; if ( null != actions )
this.actions.AddRange ( actions ); this.currentActionIndex = 0;
}字段 actions,loopActions,currentActionIndex 都和动作相关。actions 和 loopActions 用来存放动作,currentActionIndex 表示当前动作的索引。
字段 life 表示 NPC 当前的生命值,字段 protoLife 表示 NPC 生命的原始值。
字段 IsDied 表示 NPC 是否已经死亡。字段 Manager 表示 NPC 管理器。
继承自 NPC 的类可以修改方法 execute,这样就可以完成他们的自定义动作,比如:敌人的战斗机在 1 秒后发射子弹。
在 updating 方法中,我们将会判断动作的执行时间,如果可以则执行这些动作,并判断这些动作是否可以重复执行,如果可以则将动作添加到 loopActions 字段中。
protected virtual void execute ( NPCAction action )
{ } protected override void updating ( GameTime time )
{
this.frameCount++; foreach ( NPCAction action in this.loopActions.ToArray() )
if ( action.FrameIndex <= this.frameCount )
{
this.execute ( action ); if ( !action.Next ( ) )
this.loopActions.Remove ( action ); } for ( int index = this.currentActionIndex; index < this.actions.Count; index++ )
{
NPCAction action = this.actions[ index ]; if ( action.FrameIndex > this.frameCount )
break;
else
{
this.execute ( action ); if ( action.IsLoop )
{
this.loopActions.Add ( action );
action.Next ( );
} this.currentActionIndex++;
} } base.updating ( time );
}方法 Injure 将扣除 NPC 的生命值,如果生命值小于等于 0,那么我们将播放 NPC 死亡的电影,但是并不会调用 Destroy 方法,而是在 movieEnded 方法中判断死亡电影是否播放完毕,在播放完毕后我们才会调用 Destroy 方法。
派生类可以修改 dying 方法来添加一些代码,这些代码将在 NPC 死亡前执行。
protected override void movieEnded ( object sender, MovieEventArgs e )
{ if ( e.SequenceName == "dead" )
this.Destroy ( ); } protected virtual void dying ( )
{ } public virtual bool Injure ( int life, int type )
{ if ( this.IsDied )
return true; this.scene.AudioManager.PlaySound ( InjuredSoundName ); int injuredLife; if ( this.life < life )
injuredLife = this.life;
else
injuredLife = life; this.life -= injuredLife; if ( null != this.Injured )
this.Injured ( this, new NPCEventArgs ( this, injuredLife, type ) ); if ( this.life <= 0 )
{
this.scene.AudioManager.PlaySound ( DeadSoundName ); this.IsDied = true;
this.PlayMovie ( "dead" ); this.dying ( );
}
else
this.PlayExtendMovie ( "flash" ); return this.life <= 0;
}
NPC 管理器
NPCManager 类派生自类 SpiritManager<T>。
internal class NPCManager
: SpiritManager<NPC>
{
protected long frameCount = 0; internal event EventHandler<SpiritEventArgs> Destroyed; internal event EventHandler<NPCEventArgs> Injured; internal NPCManager ( )
: base ( )
{ } protected override void spiritDestroyed ( object sender, SpiritEventArgs e )
{ if ( null != this.Destroyed )
this.Destroyed ( sender, e ); NPC npc = sender as NPC;
npc.Injured -= this.Injured;
npc.Manager = null;
base.spiritDestroyed ( sender, e );
} internal override void Append ( NPC spirit, int order )
{ spirit.Manager = this;
spirit.Injured += this.Injured;
base.Append ( spirit, order );
} internal override void Update ( GameTime time )
{
this.frameCount++;
} internal List<NPC> HitTest ( HitArea area )
{
List<NPC> npcs = new List<NPC> ( ); foreach ( NPC npc in this.Spirits )
if ( !npc.IsDied && area.HitTest ( npc.HitArea ) )
npcs.Add ( npc ); return npcs;
} internal List<NPC[]> HitTest ( )
{
List<NPC[]> npcs = new List<NPC[]> ( ); for ( int index1 = 0; index1 < this.Spirits.Count; index1++ )
{
NPC npc1 = this.Spirits[ index1 ]; if ( npc1.IsDied )
continue; for ( int index2 = index1 + 1; index2 < this.Spirits.Count; index2++ )
{
NPC npc2 = this.Spirits[ index2 ]; if ( !npc2.IsDied && npc1.HitArea.HitTest ( npc2.HitArea ) )
npcs.Add ( new NPC[] { npc1, npc2 } ); } } return npcs;
} internal void SetReelSpeed ( DirectionType direction, float speed )
{ foreach ( NPC npc in this.Spirits )
npc.SetReelSpeed ( direction, speed ); } }事件 Destroyed 和 Injured 用来通知外界 NPC 已经被摧毁或者受伤,你可以在这些事件中为玩家加分。
方法 spiritDestroyed 会在 NPC 被摧毁后执行,在这个方法中,我们移除了 NPC 的 Injured 事件,而在基类中会移除 Destroyed 事件。
第一个 HitTest 方法接收了一个 HitArea 类型的参数 area,我们将判断哪些 NPC 和 area 发生了碰撞,然后将这些 NPC 作为列表返回。而第二个 HitTest 方法,我们测试互相碰撞的 NPC 并返回,比如:在竞速类游戏中,我们需要检测车辆之间的碰撞。
方法 SetReelSpeed 用来设置所有 NPC 的卷轴速度。
NPC 动作
每一个 NPC 在被创建之后,都可能会改变自己当前的状态。因此,平方创建了类 NPCAction,他包含了一些信息,这些信息说明了如何改变 NPC 的状态。
有时候要完成一个动作还需要其他的类,所以使用字段 Type 来说明动作的类型。字段 frameIndex,IsLoop,loopedCount,intervalFrameCount 用来说明动作是否可以被重复执行,第一次执行的时间,以及频率,执行次数。
当动作执行一次之后,将调用 Next 方法来获取下一次的执行时间,或者判断是否可以继续执行。
internal abstract class NPCAction
{ internal readonly int Type; private long frameIndex;
internal long FrameIndex
{
get { return this.frameIndex; }
} internal readonly bool IsLoop; private readonly int loopCount; private int loopedCount = 0;
internal int LoopedCount
{
get { return this.loopedCount; }
} private readonly long intervalFrameCount; protected NPCAction ( NPCAction action )
{
this.Type = action.Type; this.frameIndex = action.frameIndex; this.IsLoop = action.IsLoop;
this.loopCount = action.loopCount;
this.intervalFrameCount = action.intervalFrameCount;
}
protected NPCAction ( int type, float second, float intervalSecond, int loopCount )
{
this.Type = type; this.frameIndex = World.ToFrameCount ( second ); this.IsLoop = intervalSecond > 0;
this.loopCount = loopCount;
this.intervalFrameCount = World.ToFrameCount ( intervalSecond );
} internal virtual bool Next ( )
{ if ( !this.IsLoop || ( this.loopCount > 0 && this.loopedCount >= this.loopCount ) )
return false; this.frameIndex += this.intervalFrameCount;
this.loopedCount++;
return true;
} internal abstract NPCAction Clone ( ); }
示例
场景 SceneT20 是 SceneT19 的扩展,除了有 SceneT19 的功能,还将创建一些 NPC,这些 NPC 将自动旋转。
类 MyNPC 继承自 NPC,方法 execute 可以用来执行自定义动作,也就是 MyNPCAction。类 MyNPCAction 的 OffsetAngle 字段表示旋转角度。
internal class MyNPC
: NPC
{ internal MyNPC ( IPlayScene scene, Vector2 location, int angle, IList<NPCAction> actions )
: base ( scene, 1, location,
"mynpc", null,
3f, angle,
new SingleRectangleHitArea ( new Rectangle ( -20, -20, 40, 40 ) ),
40, 40,
1,
actions,
0, true, true, true, 0 )
{ this.isMoving = true; } protected override void execute ( NPCAction action )
{
MyNPCAction myAction = action as MyNPCAction; this.Angle += myAction.OffsetAngle;
} protected override void updateSpeed ( )
{
this.xSpeed = Calculator.Cos ( this.angle ) * this.speed;
this.ySpeed = Calculator.Sin ( this.angle ) * this.speed; base.updateSpeed ( );
} protected override void move ( )
{
this.Location.X += this.xSpeed;
this.Location.Y += this.ySpeed;
} } internal class MyNPCAction
: NPCAction
{
internal readonly int OffsetAngle; internal MyNPCAction ( MyNPCAction action )
: base ( action )
{
this.OffsetAngle = action.OffsetAngle;
}
internal MyNPCAction ( int offsetAngle )
: base ( 1, 0f, 0.5f, 10 )
{ this.OffsetAngle = offsetAngle; } internal override NPCAction Clone ( )
{ return new MyNPCAction ( this ); } }接下来,我们为 SceneT20 增加了一个 NPCManager。
private NPCManager npcManager;最后,我们在 goButtonSelected 方法增加了代码,创建一个新的 MyNPC。
private void goButtonSelected ( object sender, ButtonEventArgs e )
{
this.bulletManager.Append ( new MyBullet ( this, new Vector2 ( 10, 10 ), 45 ) );
this.itemManager.Append ( new MyItem ( this, new Vector2 ( 420, 30 ), 135 ) ); this.npcManager.Append ( new MyNPC ( this, new Vector2 ( 420, 420 ), 100,
new NPCAction[] {
new MyNPCAction ( 20 )
}
) );
}
本期视频 http://v.youku.com/v_show/id_XNjAxNDczMTIw.html
项目地址 http://wp-xna.googlecode.com/
更多内容 WPXNA
平方开发的游戏 http://zoyobar.lofter.com/
QQ 群 213685539
欢迎访问我在其他位置发布的同一文章:http://www.wpgame.info/post/decc4_858ce9
使用 NPC,NPCManager 在 XNA 中创建 NPC的更多相关文章
- 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Pinup,PinupManager 在 XNA 中创建贴图(十七)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Bullet,BulletManager 在 XNA 中创建子弹攻击目标(十五)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Button 类在 XNA 中创建图形按钮(九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Scene 类在 XNA 中创建不同的场景(八)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 CommandScene 类在 XNA 中创建命令场景(十二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
随机推荐
- 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)
原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...
- Swift中文手册 -- The Basics
原文:Swift中文手册 -- The Basics 基础部分 Swift 是 iOS 和 OS X 应用开发的一门新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 S ...
- 回想一下著名的BigTable论题
GFS捕捉一些业务场景的分布式文件系统的需求.很自然.此外还有一些与他们一些业务或依赖于文件系统是不那么容易,他们需要一个分布式数据库系统. BigTable那是,Google结构化数据处理的需求而产 ...
- vSphere HA状况:未知配置错误解决的方法
问题:vSphere HA配置出现未知错误,导致打不开主机上的虚拟机电源,vmware client连接vcenter后,主机显示警报信息,例如以下: 解决:例如以下图,选中有问题的物理主机,然后又一 ...
- c++中string类的具体解释
通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...
- java23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- HDU 4324 Triangle LOVE 拓扑排序
Problem Description Recently, scientists find that there is love between any of two people. For exam ...
- cocos2d-x 3.1.1 学习笔记[13] listen 监听器
文章出自于 http://blog.csdn.net/zhouyunxuan //创建监听器 auto listen = EventListenerTouchOneByOne::create(); ...
- [Unity3D]Unity3D游戏开发之刀光剑影特效的实现
大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 我实在不明确有的人为什么不喜欢武侠/仙侠类游戏,也许是因为武侠/仙侠类游戏身上被永远烙上的国 ...
- SQL常规查询详解
一.交叉连接(cross join) 交叉连接(cross join):有两种,显式的和隐式的,不带on子句,返回的是两表的乘积,也叫笛卡尔积. 例如:下面的语句1和语句2的结果是相同的. 语句1:隐 ...
