平方已经开发了一些 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 = ; 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 <= ? : life;
this.protoLife = this.life; if ( null != actions )
this.actions.AddRange ( actions ); this.currentActionIndex = ;
}

字段 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 <= )
{
this.scene.AudioManager.PlaySound ( DeadSoundName ); this.IsDied = true;
this.PlayMovie ( "dead" ); this.dying ( );
}
else
this.PlayExtendMovie ( "flash" ); return this.life <= ;
}

NPC 管理器

NPCManager 类派生自类 SpiritManager<T>。

internal class NPCManager
: SpiritManager<NPC>
{
protected long frameCount = ; 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 = ; index1 < this.Spirits.Count; index1++ )
{
NPC npc1 = this.Spirits[ index1 ]; if ( npc1.IsDied )
continue; for ( int index2 = index1 + ; 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 = ;
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 > ;
this.loopCount = loopCount;
this.intervalFrameCount = World.ToFrameCount ( intervalSecond );
} internal virtual bool Next ( )
{ if ( !this.IsLoop || ( this.loopCount > && 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, , location,
"mynpc", null,
3f, angle,
new SingleRectangleHitArea ( new Rectangle ( -, -, , ) ),
, ,
,
actions,
, true, true, true, )
{ 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 ( , 0f, 0.5f, )
{ 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 ( , ), ) );
this.itemManager.Append ( new MyItem ( this, new Vector2 ( , ), ) ); this.npcManager.Append ( new MyNPC ( this, new Vector2 ( , ), ,
new NPCAction[] {
new MyNPCAction ( )
}
) );
}

本期视频 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(十九)的更多相关文章

  1. 使用 NPC,NPCManager 在 XNA 中创建 NPC

    使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...

  2. 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  3. 使用 Pinup,PinupManager 在 XNA 中创建贴图(十七)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  4. 使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  5. 使用 Bullet,BulletManager 在 XNA 中创建子弹攻击目标(十五)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  6. 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  7. 使用 Button 类在 XNA 中创建图形按钮(九)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  8. 使用 Scene 类在 XNA 中创建不同的场景(八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  9. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

随机推荐

  1. ArcGIS中经纬度数据转空间数据

    已有这样一批数据,后缀为.txt 或者.csv .现需要将其转化为空间数据. 转换过程: 打开ArcMap选择file——>Add Data——>Add XY Data 设置经纬度对应的字 ...

  2. CSS变量(CSS variable)

    使用 CSS 变量编写你的样式代码 基本使用: 1. --variable: <declaration-value> 2. <css-attribute>: var(--var ...

  3. Samuraiwtf-解决sshd安装启动

    东西没仔细看就给别人了,具体是系统默认的sshd怎么都起不来.于是重新安装一下,过程如下 root@samuraiwtf:~# netstat -nlt Active Internet connect ...

  4. 让SAP云平台上的Web应用使用destination服务

    首先在SAP云平台里创建一个destination,维护service的end point: 然后打开SAP云平台的WebIDE,创建一个新的文件夹和新的HTML5 Application Descr ...

  5. Android(java)学习笔记76:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  6. 使ListView控件中的选择项高亮显示

    实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...

  7. python_17_数据运算

    #//取整除,返回商的整数部分 print(9//2) print(10/3.3) print(10//3.0) #<>与!=都为不等于 #and 与 例(a and b) #or 或 # ...

  8. win10如何修改host文件

    首先找到host文件,一般位于:C:\Windows\System32\drivers\etc 之后用记事本打开,直接修改.保存txt文件到桌面. 最后删除后缀名,再粘贴回去就可以了.

  9. position的 relative+absolute实现固定标签在窗口的某个位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. struts2、hibernate的知识点

    以下内容是我在复习struts2.hibernate和spring的时候记下得到,部分书上找不到的内容来自网络 以下是网络部分的原文网站: http://blog.csdn.net/frankaqi/ ...