[Tutorial 5] libGDX – The Component

Component interface:

After declaring the entity class it is time for the component interface.What methods should a component have? Keep in your mind, that scene2d divides the gameloop iteration into two different parts. In every gameloop there is a update() and render() method called. The update-method is responsible for moving the game world and running  the game logic, while the draw-method just iterates over all entities(actors) and call their own draw-method for drawing them into the spritebatch. Because the entity does not know, if the component has a drawable or updateable part, we just make the update-method available in the interface. The draw method will be a special for the RenderComponent. The component does also, not know about its container attributes, but it needs it for updating and drawing. A good approach is to pass the parent instance to the component, so its flexible and encapsulate enough. Starting the game means to load resources into the memory and set up other things. If we load an entity with it components some things should be done before trying to render or update it. For this case we need a initialize method and again we pass the parent object. While using a third party library as box2D, we also need to do some things, when the objects needs to be destroyed.

 

Putting it together:

The next step is to get it all working together. Thats really simple:

Because I decided to use and xml based level format, next time we will take a look at how to serialize and deserialize the entities.

Related Posts

Leave a reply