为 XNA 游戏实现简单的基于 XML 的脚本语言 [英] Implementing a simple XML based Scripting Language for an XNA Game

查看:18
本文介绍了为 XNA 游戏实现简单的基于 XML 的脚本语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个团队合作使用 C# 和 XNA 开发 RPG 引擎.我们计划针对 Windows 和 Windows Phone 7,但在过场动画期间遇到 AI 交互和控制玩家动作的问题.大多数情况下,一切都是使用 MVC 设计模式提取的,但是将所有逻辑和运动抽象到控制器中可能会导致问题.所以这个想法是有一个接口(IScriptEngine),它接受一个 IScriptObject 并相应地更新地图模型中的数据.我正在考虑将脚本放在 XML 类型的语法中:

<脚本名称="MoveNPC_1"><Action Command="MoveToTile" Target="NPC_1" Value="10,2"/></脚本>

并让 IScriptEngine 相应地解析它.我非常怀疑解析一个巨大的 switch 语句会是一个好主意,但这是我一直在使用的.

switch(Action.Command){案例MoveToTile":{doMovement(Action.Value, Action.Target);休息;}}

即使我的高中编程经验也告诉我这是一个坏主意,但我想不出其他办法.

我想以某种方式将这一切嵌入到地图文件中.在地图 XML 文件中,我们为 指定了区域,其中包含多个标签.然后会有一个部分,指定 NPC 在地图上的位置,以及它将使用哪些资源和脚本.然后会有一个部分,在那里定义所有的战斗(同样,定义资源和什么不是),然后可能会有一个部分,在那里定义这些脚本.我只是希望有一种方法可以使用 WP7 即时编译 C#.如果我能得到双方的支持,这将不是问题.我想知道是否有任何可以移植到 WP7 的 Mono 库可以共享 CodeDom 和所有编译器类的功能?

解决方案

XML Scripting!?喘不过气来!现在可能会导致问题"!

为什么不直接使用 C#?我听说它非常棒.我已经给出了我的对此事的意见.

你需要问问自己:你的脚本真的需要数据驱动吗?数据不能用C#表达是不是有原因?

它们真的需要在运行时被解释吗?因为,如果他们真的这样做了,可以用 C# 来完成.>

这是另一个我对一个几乎相同的回答问题,在 gamedev.stackexchange.com 上.我什至在那里放了一个可能实现的小例子.

如果你想有一个帧以上的动作来执行(基本上是协同例程),例如:走过去,说话,等等,走那边",你可以用 C# 中的 yeild 也相当好地实现了这一点.

<小时>

如果您想混合使用 XML 级别和 C# 脚本,以下是我的意思的示例:

<门位置="4,4" name="spookyDoor"/><Region position="4,2" name="spookyOpener"/></级别>

在 C# 中:

public void LevelStart(){this.Regions["spookyOpener"].OnPlayerEnter += () =>{(this.Items["spookyDoor"] as Door).Open();};}

I'm working with a team on a RPG engine in C# and XNA. We're planning on targeting Windows and Windows Phone 7, but are running into issues with AI interactions and controlling player actions during cutscenes. FOr the most part, everything is extracted using the MVC design pattern, but abstracting all logic and movement into a controller could cause issues down the line. So the idea is to have an interface (IScriptEngine) that takes an IScriptObject and updates data in the map model accordingly. I was thinking about putting the scripts in an XML sort of syntax:

<Script Name="MoveNPC_1"> 
    <Action Command="MoveToTile" Target="NPC_1" Value="10,2"/> 
</Script> 

And have an IScriptEngine parse it accordingly. I highly doubt parsing in a giant switch statement would be a good idea, but it's what I've been working with.

switch(Action.Command)
{
    case "MoveToTile":
      { 
         doMovement(Action.Value, Action.Target); 
         break;
      }
}

Even my high school programming experience tells me that this is a bad idea, but I can't think of any other way around it.

Edit: I'd like to somehow embed this all in the map file. In the map XML file, we have areas designated for , which contains multiple tags. Then there would be an section, designating where the NPC is on the map, and what resources and scripts it would use. Then there would be a section, where all battles would be defined (Again, defining resources and what not), and then possibly a Section, where these scripts would be defined. I just wish there was a way to compile C# on the fly using WP7. If I could get support on both sides, this wouldn't be an issue. I wonder if there would be any Mono library that could be ported to WP7 that would share the functionality of CodeDom and all of the Compiler classes?

解决方案

XML Scripting!? Gasp! Now that is something that "could cause issues down the line"!

Why not just use C#? I hear it's pretty terrific. I've already given my opinion on this matter.

You need to ask yourself: Do your scripts really need to be data driven? Is there a reason that data can't be expressed using C#?

Do they really need to be interpreted at runtime? Because, if they really do, that can be done with C#.

And here's another answer of mine to an almost identical question, over on the gamedev.stackexchange.com. I've even put a little example of a possible implementation in there.

If you want to have actions that take more than a frame to execute (basically co-routines), for example: "Walk over here, Talk, Wait, Walk over there", you can implement this with yeild in C# reasonably well, too.


Edit: If you want to mix your XML levels, and C# script, here is an example of what I mean:

<Level>
    <Door position="4,4" name="spookyDoor" />
    <Region position="4,2" name="spookyOpener" />
</Level>

And in C#:

public void LevelStart()
{
    this.Regions["spookyOpener"].OnPlayerEnter += () =>
    {
        (this.Items["spookyDoor"] as Door).Open();
    };
}

这篇关于为 XNA 游戏实现简单的基于 XML 的脚本语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆