1
\$\begingroup\$

I am writing a beat'em up like game in Libgdx while following the ECS pattern brought up by ashley. For setting up the stage, I am using TileMap and Tiled. An object layer for collisions, and a tile layer for the tiles. Usually, an entity -in this case, an enemy- has a lot of components so harcoding one class for each stage on start up is out of the box. I have already considered this options:

  1. SQLite database. Seems rather complex for the task, somehow.
  2. Json files. I'd have to overwrite and create my own serialization class
  3. Using another object layer for spawning enemies by using the "Custom attributes" option in order to store extra data.

Have anyone come up with a similar problem or experience already? What would be the best approach?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I'm not 100% sure if I understood your question, so hopefully this answer answers some of your questions haha :)

My current approach

I've encountered this problem multiple times, and the method which I'm using right now is creating my own interpreter. Basically I have a file that looks something like this:

[catapult]
setName:catapult,
setType:siege,
addComponent:health,
addComponent:attack,
...

This file contains information that is split into two parts: action:value and separated by a comma. I then use my own interpreter called ObjectLoader that reads this file and creates objects. Pseudo code might be something like this:

MyObject myObj = new MyObject();

// Here I read the file and add all action:value pairs to a list.

for(Pair pair : pairs) {
    if(pair.getAction().equals("setName")) {
        myObj.setName(pair.getValue());
    } else if(pair.getAction().equals("addComponent") {
        if(pair.getValue().equals("health") {
            myObj.getEntity().addComponent(new HealthComponent());
        }
    } else if(...) {
        ...
    }
}

This can also be applied to creating a stage, perhaps by doing something like this:

[SomeEnemy]
addComponent:health,
...

[SomeStage]
addEnemy:([SomeEnemy], 4), //Create 4 of "SomeEnemy"
...

This is of course not very clean code, and can quickly get very long if you have a lot of different components, for example. With this in mind I've started developing a better method using annotations and reflection, but that's not finished yet.

My previous approach

Previously I used one of the methods you named yourself; JSON. LibGDX already has JSON functionality built into it, using their Json classes (official tutorial). This works very well, and you can customize your serialization using their Json.Serializable and Json.Serializer<T> classes. I would suggest this method if you absolutely don't need the functionality of the other method I mentioned. It is also a lot easier to use.

Honorable mention

If you want an alternative to JSON you could look into XML. LibGDX also has built in functionality for this, just like JSON (official tutorial).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .