EntityManager, collision is a breeze

It was a little while ago that I finished the Entitymanager for our spaceshooter project. However today I am going to explain how it works and why it makes collision so much easier for our game.

Lets first look at our Entitymanager.h

Entitymanagerh

Which includes a vector of Entity pointers, a SpriteManager pointer and a map that will control collision.

First there is the constructors, which are not really important at this point. One of them contains a pointer to the sprite manager, however that is for animations. Which will be discussed next week.

Secondly there is the Init() function which contains these things at the moment:

Initfunction

Which is a collisionmap that secures that we do not do any unnecessary collision checks, this will be more clear when we get to the update function. However the map consists of a pair of Alignments as a key and a int value thereafter. The “Alignment ” type is an enum which describes what sort of object an entity is.

The latter part of the functions is where the player is created (making sure that the player is at position zero). Setting its position, adding its animationer, start the entities initialising function and set the shooting delay.

Thirdly there is the AttachEntity function:

AttachEntity

Which does the same as the ladder part of the Init() function, but with a switch case with Alignment as parameter. Making it easy to add another sort of entity.

The next function is almost the same as the AttachEntity, but with Projectiles instead. Since they need to know what direction they are flying in.

AttachProjectile

Then there is a cleanup function which is not that exciting. It only goes through the vector and deleting the pointers and setting them to null.

However the next segment is the most interesting one, the “Update()” function:

Update1 Update2

The first thing it does is to update the objects, and then it checks through the rest of the objects in the vector. If both objects have colliders and the objects alignments are in the collisionmap, then it checks for the collision. Meaning that instead of all objects checking against all objects (checking n to the power of two collisions) it only checks all friendly to all enemies. Meaning that if I had 1 friendly player and 99 enemies, I would check 99 collisions, instead of 10 000 collisions. Not a bad trade.

Then there is the projectile creation part, it would feel quite self-explanatory, as I check if the required delay has passed since the last shot and if the shoot button has been pressed. It is only implemented for the player at the moment but it will be of the same premises that the AI shooting will be built upon.

And then there is the last part where I clean up the entities which have been flagged for death.

Well that would be all, if you got any questions feel free to post them.