This tutorial shows you how to add powerups to the game.
An essential part of any good shoot’em’up are the powerups. In our game the powerups allow the player to pick up new weapons, either to increase the power of the current type of weapon, or to use a completely new type of weapon.
The powerup itself is like a basic enemy in that once it is added to the game it simply moves across the screen until it is no longer visible, at which point it removes itself from the game. The powerup is created by a new type of enemy, the PowerupEnemy, when it dies, and when the player collides with the powerup the player will modify the current weapon.
Powerup.h / Powerup.cpp
Everything in the Powerup class should be familiar. The only difference is that we have a weapon property and a GetWeapon function, both of which are used to define the type of powerup and allow the player to modify the current weapon appropriately.
Of course it is pointless having powerups without a way to add them to the game. For this we create a new type of enemy called PowerupEnemy.
PowerupEnemy.h / PowerupEnemy.cpp
The PowerupEnemy has the same functionality as the BasicEnemy (and therefore extends the BasicEnemy class), except that when it is involved in a collision, and the Collision function is called, it will create a new instance of the Powerup class. [code]
PowerupFactory.h
As you can see though the Powerups are not created directly by the PowerupEnemy, but through a new factory called PowerupFactory. Like all the other factory classes, the PowerupFactory is essentially a database that creates new Powerup classes with the appropriate constructor paramaters when supplied with an integer that defines the type of powerup.
EnemyFactory.h
When a new enemy class is defined it needs to have an entry in the EnemyFactory for it to be created in the game. In this case we define two new enemy types that are both represented by the PowerupEnemy class, and these are created through the CreatePowerupEnemy1 and CreatePowerupEnemy2 functions. [code]
Because enemies are created by enemy waves, new waves have to be defined to add the new enemy types to the game. This is done in the EnemyWaveFactory class.