Game Developement and Coding 13 March 2014 – Entity Manager

The Entity Manager

The entity manager is a class that makes sure all objects (or entities) in the game space are updated and displayed on the screen properly.

 

The entity manager can be divided into five parts:

-Add pointers to objects and store them in vectors.

-Update all the objects via their pointers.

-Draw all objects via their pointers.

-Make sure all objects are added to the collision check.

-Check the HP of all objects and delete those that have reached zero or lower.

 

It stores pointers to each class of object in a separate vector container. For example all rubbish bin pointers are stored in the vector “m_rubbishBins”. Sorting them this gives me some extra control.

I can make sure that the program does not try to update or draw something that does not exist by checking first to see if the container is empty or not. I can also adapt the way each class of object is updated and drawn to suit their needs. For example, most objects only needs a pointer to the rendered window to be drawn, but the rubbish bin also needs to know the players turn angle in order to know its own sprites’ turn angle in case it is knocked over.

New objects are added to their respective vector containers by using their own add method, for example “AddRubbishBin” that only takes in pointers to rubbish bins. The method only takes in pointers to the objects created by the right “Spawner”, for example “AddRubbishBin” only takes in pointers made by the class “SpawnerRubbishBin”.

The objects are then updated in their separate update method that goes through the vector of pointers. This means you can adapt what kind of parameters are used in the different update methods rather than storing them all in the same vector and check each and every one which type they are and then update accordingly. It also means that if something happens in the update that should make something else spawn, for example a bullet from a ranged attack, it can be fixed right in the same update method.

Placing the updates in their own methods means you can change their order a lot easier. For example, if I discover that the player needs to be updated before any enemy I can simply move the “UpdatePlayer” method to a different place in the Entity Managers’ update rather then move around all the code that goes through how to update the player depending on user input.

1 entity manager update

The separate draw methods are built pretty much the same way as the update methods, for the same reasons, only difference being that in these methods the objects gets drawn to the rendered window.

2 entity manager draw

The entity manager also makes sure pointers to all the objects get added to the collision manager so that proper collision detection can be made by the application.

Lastly, the entity manager goes through all vectors, checking all the objects via the pointers in them, to see if their HP. If the HP is 0 or less, the object gets deleted and, in some cases, gives the player a score point.

One response to “Game Developement and Coding 13 March 2014 – Entity Manager

  1. Hello Niklas!

    I like the progress that you have made with your game overall between the playtests. Thumbs up!

    Your entity manager seems well thought out. Well explained with good pictures.
    We have about the same structure but with some differences of course. I liked the HP-check, we have the check only when enemies/the avatar takes damage at each specific place in the code. I believe that your check is better. However, our code doesn’t loop through everything each update to check. The processor load for this is minimal though so I guess that it doesn’t matter.

    There is another idea for this, using events. Jonas Lundgren discussed this with me. World of Warcraft uses it and the idea of it is to call upon events when certain stuff needs to happen instead of writing code like we do right now. I don’t know how to explain how it works since I don’t really know but hopefully we will learn this in the future programming courses.
    I like your way of adding objects except for one thing. Only the entity managers should be able to create objects of different entity types and the Create()-methods could only take parameters like position. Then inside the Create()-method is where the entity-pointer is new:ed and stored in its respective vector.

    We use the same principle for the update-/draw-methods and it works really well, good to see that you thought the same way.

    Well written, sir. Good luck on the home stretch now!

Leave a comment