Spacefight – Subclassing into enemies, player and other stuff – part 5

In the last post I made a GameObject class. Now it’s time to subclass from the GameObject class into an enemy. The enemy object will make use of the shared functionality in GameObject, like position, access to a shared AnimationLibrary / SoundLibrary, a destruction routine (to mark the object to be deleted), depth index and other paramters.

The enemy has it’s own implementation of listen, which won’t be used. The enemy object doesn’t care if you move your character to the right or left. How ever the enemy uses the update function. However the player will implement the listen function and move the ship depending on what key is pressed.

The update implementation for the enemy object is responsible for the calculation where the enemy will end up (in the next draw()). It also checks whether the enemy has any health left or it will be marked as “terminate me”. This function also randomly launches fire against the player.

The last function is the draw implementation. For the enemy this means to draw a frame from the AnimationLibrary. It also draws a healthbar over the enemy. To show the current health.

All objects on the screen (even the player) are made like this. And I can put them all together in a vector (list) and run listen, update and draw and they’ll do their own thing. Neat!

This is how implementation in enemy.cpp ended up like:

 

Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*