开发者

Algorithm to launch methods defined in xml

开发者 https://www.devze.com 2023-03-19 05:19 出处:网络
I\'m new to java. I have game levellogic implemented with timer. In my game i\'m checking on which second of a level we are an开发者_运维知识库d then spawn enemies anddo more things

I'm new to java.

I have game levellogic implemented with timer.

In my game i'm checking on which second of a level we are an开发者_运维知识库d then spawn enemies and do more things

switch(pSecond){
case 1:
    xDestroyer.ClearPaths();
    xDestroyer.AddPathPredefined(1);
    xDestroyer.AddPathPredefined(2);
    xDestroyer.AddPathPredefined(3);
    xDestroyer.AddPathPredefined(4);
    xDestroyer.AddPathPredefined(5);
    xDestroyer.EnemyStart(14f, 5, 5, 0.0001f); 
    xShuttle0.EnemyStartByPath(1,5f,8,2,0.2f);
    xShuttle0.EnemyStartByPath(5,5f,8,2,0.2f); 
    break;
case 17:
    xShuttle0.EnemyStartByPath(1, 4f, 8, 2, 0.3f); 
    xShuttle1.EnemyStartByPath(5, 4f, 8, 2, 0.3f);
    break;
}

and so on. I would like to know how can i transfer this logic to XML file. What is the best way to do it ?


You might be able to do something like this:

<trigger time="1">
    <action target="xDestroyer" verb="ClearPaths"/>
    <action target="xDestroyer" verb="AddPathPredefined">
        <arg>1</arg> <!-- default type is "int" -->
    </action>
    <!-- etc. -->
    <action target="xDestroyer" verb="EnemyStart">
        <arg type="float">14</arg>
        <arg>5</arg>
        <arg>5</arg>
        <arg type="float">0.0001f</arg>
    </action>
    <action target="xShuttle0" verb="EnemyStartByPath">
        <arg>1</arg>
        <arg type="float">5</arg>
        <arg>8</arg>
        <arg>2</arg>
        <arg type="float">0.2</arg>
    </action>
    <!-- etc. -->
</trigger>
<trigger time="17">
    <!-- ... -->
</trigger>

The actions could then be applied to the targets using reflection.

An alternative would be to predefine a library of targets and verbs and reference them in the XML by key word or index. A useful technique here is to define an object type for each action (method name). You can then put them in an array or hash table and reference them by key word or index from the XML. They might be implemented something like this:

interface Action {
    void apply(String targetName, String... args);
}

class ClearPathsAction implements Action {
    void apply(String targetName, String... args) {
        Destroyer destroyer = findDestroyer(targetName);
        destroyer.ClearPaths(); // args ignored
    }
}

class AddPathPredefinedAction implements Action {
    void apply(String targetName, String... args) {
        Destroyer destroyer = findDestroyer(targetName);
        int index = Integer.parseInt(args[0]);
        destroyer.AddPathPredefined(index);
    }
}

// etc.

You'd obviously want to do some error checking and handling at some point in this process. :)


You don't really give us a lot of background about your motivation or where you see the problems. So I'm guessing a lot here.

I can see three distinct area where you might be asking the question:

  1. The actual technical stuff about reading XML into Java.
  2. How the represent the individual information relating to one artefact such as Destroyer, Shuttle0, Shuttle1 etc.
  3. How to represent the overall algorithmic stuff, how many seconds, the ranges of actions for each second.

For the first, there are many good Java libraries for reading XML, for reasons of history and the environments in which I work I use JAX/B, but there are many alternatives.

For the second item, you need to decide what kind of object you get from XML to represent some action. You code looks like the xDestroyer and xShuttle objects pre-exist (I'll call those game objects), and at certain times you do things to them. If that is the case then I think you're getting some kind "event" objects from the XML and you apply them to the appropriate game object.

So I would work top down, first defining the set of possible kinds events and then generating the XML represent the set of events. So a DestroyerEvent might have an array of predefined paths and an Enemy definition.

Now you just need to represent the groups of events that correspond to a particular second, your XMl file (or maybe several files, one per second?) contains a set of XML Events.

Summary: think about the classes of objects you want to read from XML, leave the detail of the XML to one side. There are nice tools for generating XML once you've got the class model right.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号