Enemy Plane and related Instancing

This week I have been focusing on compressing all enemy-related code, like collision, shooting and so on, into class specific functions, to allow more enemies to be generated, and to more easily adjust things like how and at what rate enemies fire bullets…

2016-02-25 (5).png
…and they do fire a lot of bullets; you could say its a shooting gallery out there and not be wrong.

 

I  started out by taking into account what functions each Enemy needed to have run, and how it would react to functions that the player has, such as the players potato projectiles, as well drawing all sprites created from functions if necessary:

  • Starting off, I needed a function and related variables to generate bullets for each enemy. I decided on using an array of Projectile pointers  with 10 elements to simulate a salvo of bullets. The array would make sure that there would only ever be 10 bullets generated from a single enemy object at a given time. Each bullet is an object of the Bullet class, complete with a Trajectory function that determines speed and direction.
  • Secondly, I would need to iterate through all bullets generated by each enemy object to subsequently get their Sprite information in order to check collision with the Player and draw their sprites onto the screen.
  • Finally, the Enemies themselves needed to be stored in an vector array so that they too can be iterated through in order to access each of their own iteration of bullets and other functions like update, and of course in order to draw them.

Along the way I was met with a multitude of issues: First of all, I found it close to impossible to use vectors to handle enemy bullets; it would have been useful on paper, as vectors do not have pre-determined size, each enemy could generate a variable amount of bullets… the problem was that by the time bullets collided or just passed their lifetime, they would be deleted and removed from the vector, leading to the vector checking empty positions during iteration, causing the game to crash. I figured that each enemy wouldn’t need more than about 10 bullets on screen at a time anyway, so a normal array should work, but it was more difficult iterating through it without using multiple variables to keep track of the process.

I got the process of iterating through enemies and their functions to work smoothly, but I might add that first I used the c++ auto iterator, but this slowed down the game considerably. After using for-loops to iterate through the Enemy vector speed framerate was much less impacted.

 

About Erik Nord

2015 Programming