With collision detection in place we can start to add some interactivity to the game. The first step will be the ability to fire a weapon, which will collide with an destroy the enemies.
BaseWeapon.cpp / BaseWeapon.h
The base class for all weapons is the BaseWeapon class. This class is very similar to the BaseEnemy class in that it is set up to remove the weapon once it has moved off the screen. [code]
We also remove the weapon from the system once it has collided with another object. [code]
Eventually the BaseWeapon class will have more properties common to all weapons, like the amount of damage that they do, but for now we just want to get the weapons on the screen.
BasicWeapon.cpp / BasicWeapon.h
The BasicWeapon class extends BaseWeapon, and provides the logic to move in a straight line across the screen. It's very basic, but most weapons will work in this way. [code]
WeaponFactory.cpp / WeaponFactory.h
Much like the enemies and enemy waves, the weapons will be defined and constructed in a factory called WeaponFactory. The weapons are catagorised by weapon type, and the level of the weapon. So you might have a weapon type of 1 (standard bullets) and a weapon level of 2 (maybe that fires two bullets instead of one).
This weapon type and level database is stored in an STL map object called database, which is populated by the function PopulateDatabase. [code]
When the player or an enemy wants to create a new weapon, it calls the CreateWeapon function. [code]
Explosion.cpp / Explosion.h
When the enemies die we need an explosion. The explosion itself is represented by the Explosion class. It is simply a circular texture that scales up as it fades out.
The standard SDL library does not have the ability to scale a texture. Fortunately there are a number of 3rd party libraries that can add this functionality. One of them is the Sprig library. By using the SPG_Scale function we can create a new, temporary scaled surface to draw to the screen. [code]
BaseEnemy.cpp / BaseEnemy.h
Now when an enemy is involved in a collision it creates a new instance of the Explosion class. [code]
Player.cpp / Player.h
Finally the Player class gains the ability to create new weapons. The timeToNextShot property defines how long it will be before the Player can create a new weapon. This value comes from the timeBetweenShots property of the BaseWeapon class.
The timeToNextShot property decreases every frame until it reaches 0, at which point, if the shooting property has been set true in the MouseButtonDown function, the player creates a new weapon through the WeaponFactory class. [code]
The weapon and weaponLevel properties define the players current weapon. These are fixed at 1 each for now, but as we add powerups and new weapon types they will change.
Finally we have something that resembles a game. The player can shoot the enemies, who die in response. In fact we are getting close to having all the ingredients needed to make a full game.
Download the source code here.
Read more in the SDL Programming Tutorial series