# Step 2: Writing Objects

This is how you write objects for the bomberman game. I'll give you the basics and run throught he existing object.

## Quick Overview

Everything in the game can be seen as an object. The player, bombs, blocks, explosions, etc. Even things like the score are objects. Basically if something needs to be drawn, needs to be updated (ie - position or count or something), and/or has some sort of collision detection - then it is an object.

Every object inherits from CGameObject (CGameObject.h/.cpp). These gives each objects some common characteristics. These are: An X/Y cordinate, A bounding box for collision detection, an Update method, a Draw function, and a function for collision detection (Collision). Every object is going to over-ride and implement these functions.

## void Draw();

Obviously, this gets called every frame and should draw the object and the appropriate position with the appropriate frame. Some Objects may have a single image, others may use an animation system, others just might be text. Some may use particle effects, or some may not get drawn at all. You get the idea.

Heres the basic procedure for drawing an image on the screen and position X,Y:

    //Load Image. Only do this once

    CImage* img  = g_GraphicSystem.LoadCImage("filename.bmp");  //we can load: .tga .png .jpeg .dds and .bmp

    //Draw Image. This will happen every frame

    g_GraphicSystem.DrawCImage(img, X, Y);  //img is a pointer to a loaded CImage. X/Y are ints of the screen cord.

I'll cover the animation classes later.

## void Update();

Update is the function where you should update the position of the object, count down to explosion, update the animation, etc. Its up to you what exactly needs to be done. Check out the other examples to see how they work.

## void Collision(CGameObject* obj);

This method gets called automatically whenever two objects collide. To be sure collision detection works properly you MUST setup the bounding box correctly. However once you set it up, you can pretty much ignore it. Heres how it works:

    SetRect(&m_boundingBox,0,0,m_Img->getWidth(),m_Img->getHeight());

The above example will properly setup a bounding box around the object with the same deminsions as m_Img (a CImage*). This is all you have to do. But it is important to do this BEFORE setting the x and y cordinates. It is also very important that you always adjust the X and Y values with setX(int x) and setY(int y). These automatically take care of the bounding box. Note: This should all be done in the Init function

In the case of the bomberman (player) object I cheated a bit and manually set the bounding box to the size of a game tile and hard-coded its position relative the the x and y cordinate. What Im saying is, some objects require a bit more work, but in most cases setting it up with the initial width and height of one of its images is enough.

Finally you need to actually write the function. Heres an example:

    void CPlayerObject::Collision(CGameObject* obj)
    {
    	//This is called when the player's bounding box (m_boundingBox) collides with another objects bounding box.

    	switch(obj->typ)	//figure out what type it is.
    	{

    	case typBlock:
    		{

    		//Code to stop player

    		}

    		break;

    	case typBomb:
    		{

            //stop player IF he didnt just place this bomb

    		}

    		break;

        case typExplosion:
            {

            //Kill the player

            }

            break;

    	default:

    		break;

    	}

    }

Again, pretty easy. You switch on the object's typ. Which BTW MUST be set in the Init function for anything you write. You will also need to define the object type in CGameObject.h (in the enum).

Again what you do is up to the object. So what actually goes in those case statements is up to you.

Part III: Creating, Deleting, and Advance Topics.