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

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

问题描述

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

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

并让IScriptEngine对其进行相应的解析.我高度怀疑在巨大的switch语句中进行解析将是一个好主意,但这就是我一直在努力的目标.

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

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

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

解决方案

XML脚本! 天哪!现在,是可能导致问题的根本原因"!

为什么不只使用C#?我听说这太棒了.我已经给出了对此事的意见.

>

您需要问自己:您的脚本真的需要数据驱动吗?有没有理由不能使用C#表示数据?

它们真的需要在运行时进行解释吗?因为,如果确实如此,则可以使用C#完成.

这是我的另一个我的回答几乎与相同问题,请访问gamedev.stackexchange.com.我什至在其中放了一个可能的实现的小例子.

如果您要执行的动作要花费一个框架以上的时间(基本上是协同例程),例如:在这里走动,交谈,等待,在那走动",则可以

在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天全站免登陆