[Tutorial 4] libGDX – Levelformat, entitys and scene2d

Levelformat and object life cycle:

A game consists of levels. After loading a level this level has a initial state and this state is the „scene“, which is a „static initial level“. This scene has different actors, like a static background, a playerobject which can show animations or play a sound and many more. But not only the scene is part of a level, there will be more objects created and deleted while playing. I call this objects dynamic, temporary objects, because they are not part of the scene.

So the first thing to declare is, how the game should load a level. Loading all elements of a level(levelfile only includes the scene) and build dynamic temporary objects in the code? Or should all objects be loaded, even the ones, which may never be used, and declare them kind of inactive? No i found another solution, which is more flexible. I define entities and template entities.Template entities are only skeletons of entities which are initially not loaded, but known by ID/name. This means i can easily add this template entities to the game on runtime.

Level files… what about saving them? Just use JSON or XML or any other serialization format.I took XML.

The entity class:

Back to the component-based thing. We need a container for all components and we should think about which attributes do all components share.

  • Position, Rotation, Scale, Dimension
  • Layer (Z-Position), Name/ID, visible flag

Because libGDX has a powerful feature included i chose to include scene2d in my approach. Scene2d makes a lot of things, you don´t want to implement yourself like culling, powerful events(actions), hit detection, group transformation, viewport. (For details look here: Scene2d)

Using scene2d needs us to use the actor class, knowing that this class has some overhead and limits us in our component-based design. I am pretty sure there is another, better way to go, but i made this decision years ago. So our Entity class extends the Actor class in libGDX, which needs us to write a little more complex de/serializer when loading from xml.

Ok now here is a simple example how a level can be structured and how the entity without components looks:


Next time lets take a look at the component interface.


Related Posts

Leave a reply