Spacefight – The GameObject – part 4

A lot of objects in the game has a many things in common. They contain an Animationlibrary, a position on the screen, width and height. They also got common functions like:

  • Listen
    Sends any event from the user’s keyboard/mouse/joystick to the object to handle.
  • Update
    Calculates where the object should be positioned next time, if the object should do any kind of action during this frame.
  • Draw
    It will get a frame from the object’s current animation and draw it on the screen. The screen surface will be passed into this function.
  • Handle collision
    If the object collides with another game object, it will be told what it’s been colliding with through here via a collision detection routine.

I created a base game object like this:

As you can see it the game object has a number of virtual functions for update, listen, draw and handleCollision. This means that the sub class needs to implement all these function. It works as an abstract method if you know Java, PHP or similar. The GameObject class will never be instantiated. It will only be used to inherit from.

I have added a SDL_Rect on the game object as well. This will be used for collision detection later on. I might change this to a list of rectangles instead. Since some objects might be more complex and need more detailed “hit” surfaces.

So what is the point in all these shared methods?

It makes it possible for me to create a vector (or list) that contains GameObjects. The game object can be an Enemy, it can be an explosion, a rocket or the player or any new type. All of them shares the same function name for updating their position, but their implementation might be very different.

In the game-loop the game objects will be used something like this:

The example above is very simplified. It doesn’t mention the screen or the event in the game loop. Before iterating through the game objects a background image should probably be drawn on the screen surface. This way each loop will put a background image on the screen, and start drawing the enemies, bullets and player from the game object list, until it composed a complete game screen.

Layers doesn’t exists in SDL. What has been drawn first will end up in the back. And what has been drawn last will end up in the front.

If you look in the GameObject header code. You will see there is a variable called zIndex. All the game objects in the vector will be sorted on this variable. The lower number the higher up in the list the object will be placed. If the specific gameobject has the highest zIndex value, it will be in the end of the list and therefore be drawn on top of all the other game objects.

I will probably come back to the game object code at some point. To add references to audio or other properties that needs to implemented by all objects. But it will work for now.

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.