开发者

Is it better to hardcode in, or leave it to external XML files

I'm creating a 2D based RPG game. Similar to that of Pokemon for the Gameboy. Basically, my games maps are created up by x by x size terrain. There are also scenery objects (trees, shrubs, etc.) as well as intractable things (i.e. signs, doors, items, etc.). Currently, I've been hard coding these objects in. The problem is, every time I add a scenery object, terrain tile, or anything else, it requires me to go through and add a class for it, specify some different data, etc. Basically, I feel like I have to do a lot of repetitive tasks to do something so simple.

I'm scared that later, my project will become unmanageable. Each piece of new terrain, scenery, or other object inherited from a class called 'GridElement'.

So, would it be better to have all the tile, scenery, etc. information put into an external XML file and loaded at run time, or continue hard coding these elements in?

The main problem is, most of these elements require specific functions to be applied to them. Some of them need to call events when they are stepped on. Some elements are also dynamic (the tile changes every couple seconds, e.g. water, flower, etc.).

Thanks for the he开发者_如何转开发lp!

Cheers!


As a loose rule, you should create a new class only when you want to represent behaviour, and control the parameters of this class with your data.

In your case, you probably want one scenery class for your flowers, shrubs, walls and such things. You can then customise objects of this class by calling functions to set the bitmap(s), the animation speed. It then doesn't really matter then where this data comes from, it could be hardcoded, come from an object factory or read from xml.

When you come to your interactable objects, you do need additional classes, but try to make them more generic than specific. So if you want to display a message when your user interacts with an object you have a generic interact class, rather than a sign class. This class can then be used for signs, bookshelves, mysterious looking items.

When it comes to special events you have a few options. The simplest is to create a generic event class, which can direct new actors to appear on screen or to bestow objects on the player. Alternately you could pass a function pointer/delegate to the object which is called when the user hits certain triggers (ie, step into a square, defeat a monster). Finally, most complicated of all but most likely what Pokemon and similar games use, you could create/use your own scripting language.


It is better to put data into data files, and write your code to process those data files. There are multiple reasons: you alluded to one, that the data is more manageable this way (ie. it'll be more cleanly organized and easier to find). Another important benefit is that it makes the data easier to change. When working with others that makes a HUGE difference, but even when working individually it's much easier for you to tweak all your gameplay values when they are in config files rather than in the code itself.

Your caveats do mean that you will often have to have separate classes for separate kinds of elements, and then reference those classes in the XML; for example:

<grid_element>
  <class>GrassElement</class>
  <image>dark_grass.png</image>
  <xpos>10</xpos>
  <ypos>10</ypos>
</grid_element>

but as much as possible move away from hard-coded stuff and do it in data files. For example, the different events that get called can be defined in the XML:

<step_event>somethingOrOther</step_event>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜