将 XML 编译为 Java
我在这里有一项艰巨的任务。我正在考虑构建一个简单的游戏,在这个游戏中我将应用一个 buff 系统(阅读此处了解更多信息)。
所以,假设我想创建一个 buff:
<buff name="Life Steal">
<afterAttack>
<heal target="attacker">$damage$ * 0.2</heal>`
</afterAttack>
</buff>
在论文中,这将生成以下输出:
public class Life_Steal extends Buff {
@Override
public void afterAttack(Object attacker, Object defender, int damageDone) {
heal(attacker, damageDone * 0.2);
}
}
这可能吗?如果是这样,是否有任何工具可以让我做类似的事情?
编辑
我发现混合 Javascript 和 XML 将是一个很好的方法。
<buff name="Life Steal>
<onattach>
<javascript>
attacker.hp += damage * 0.2;
</javascript>
</onattach>
</buff>
这是我能想到的最好的解决方案。有人有更好的吗?
I have a hard task here. I'm thinking of building a simple game, and in this game I'm going to apply a buff system (read here for more information).
So, let's say I want to create a buff:
<buff name="Life Steal">
<afterAttack>
<heal target="attacker">$damage$ * 0.2</heal>`
</afterAttack>
</buff>
This, in thesis, would generate the following output:
public class Life_Steal extends Buff {
@Override
public void afterAttack(Object attacker, Object defender, int damageDone) {
heal(attacker, damageDone * 0.2);
}
}
Is this possible? If so, is there any tool out there that enables me to do something like that?
EDIT
I found out that mixing Javascript and XML would be a good way to go for it.
<buff name="Life Steal>
<onattach>
<javascript>
attacker.hp += damage * 0.2;
</javascript>
</onattach>
</buff>
That's the best solution I could think of. Anyone have a better one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不赞成这个项目,但它符合你正在做的事情。 Jelly - 可执行 XML。加载 XML 最轻松的方法是 JAXB。
也就是说,真正的脚本语言将是更好的选择。
在任何情况下,请阅读 Java 平台脚本 和 Java 脚本程序员指南 在继续之前。
如果您只是出于某些奇怪的原因而必须使用 XML,那么将 JAXB 与 XML 中嵌入的脚本混合起来可能是另一种选择。
I don't condone this project, but it is along the lines of what you are doing. Jelly - Executable XML. The most painless way to load XML is JAXB.
That said, a real scripting language would be a much better choice.
In any case read Scripting for the Java Platform and Java Scripting Programmers Guide before you go any further.
If you just have to use XML for some bizarre reason, a mixture of JAXB with scripting imbedded in the XML might be another way to go.
我认为您可能会在 XSLT 方面取得很大的进展。例如,
元素中的自由格式表达式可能很棘手。一般来说,这是一个非常简单的问题。获取一个解析器 - 我建议使用 SAX 或 StAX 解析器而不是 DOM 解析器 - 并为您遇到的每个元素/属性输出适当的 Java 代码。使用 Javassist 之类的工具,您甚至可以将该代码直接编译为字节码,而无需将源代码输出到磁盘并单独调用编译器。
I think you could probably get quite a long way with XSLT. The free-form expressions inside, for example, the
<heal>
element might be tricky.In general, it's a pretty simple problem. Grab a parser - I'd recommend a SAX or StAX parser rather than a DOM parser - and output appropriate Java code for each element/attribute you encounter. Using something like Javassist you could even compile that code straight into bytecode without having to output the source to disk and invoke the compiler separately.
在我看来,你最好的选择是使用 DSL 来实现这样的东西,或者只是使用 JRuby 或 Groovy 等更动态的语言来实现这些部分,即使系统的其余部分是用 Java 编写的。
IMO your best bet is to implement stuff like this using a DSL, or just use a more dynamic language like JRuby or Groovy for these pieces, even if the rest of the system is in Java.
查看 Apache Jelly。这是我能想到的最符合您要求的内容(我认为这是允许非编码人员创建游戏内容)。
Take a look at Apache Jelly. This is the closest fit I can think of to what you're asking for (which I presume is allowing non coders to create game content).
这看起来你真的很渴望用 xml 进行编程,当你有一百或一千个这样的 XML 时,这真的会很痛苦(恕我直言)。然而,这里肯定有一些配置,您可以转储到 xml 中,以便您的 xml 被处理成 Buff 对象图,并且您有一个采用 vararg buff 集的攻击方法。
然后你可以将算法保留在java中,但在xml中进行所有的nerfing。
更新尝试并用代码回答问题 -
如果我要为此推荐一种技术,我会说您可能可以保留您的 xml 并使用 JAXB 解析器,我认为它是与后来的 jvm 一起打包的。
现在您可以使用您的 buff 配置并安全地读取它们作为配置类型。
This seems like you really yearn to program in xml, which is really gonna be a pain (imho) when you have a hundred or thousand of these. However, there is definitely some configuration here, that you could dump into xml so your xml gets processed into a Buff object graph and you have an attack method that takes vararg set of buffs.
Then you can keep the algorithm in java, but do all the nerfing in xml.
update to try and answer the question with code -
If I were going to recommend a technique for this, I would say you can probably leave your xml alone and use a JAXB parser, which I think is packaged with a later jvm.
now you can use your buff configurations and read them type safely as configuration.
考虑不使用 xml,也许类似:
consider not using xml and maybe something like:
如果您只想在表达式中支持有限的表达能力,那么使用嵌入 XML 中的 XPath 表达式是一个不错的选择,而评估它们的最简单方法是将 XML 转换为 XSLT。
如果您想支持更丰富的算法能力,请使用功能齐全的编程语言,而不是发明自己的语言。
If you only want to support limited expressive power in your expressions, using XPath expressions embedded in XML is a good choice and the simplest way to evaluate them is to transform the XML into XSLT.
If you want to support richer algorithmic power, then use a fully-featured programming language instead of inventing your own.
尽管 XStream 可用于解析 XML,但您最好坚持使用语言脚本。
Although XStream may be used to parse XML, you better stick with a language scripting.