Sign in to follow this  
Sean_Seanston

C++ : "Bunkers" in Space Invaders

Recommended Posts

So Space Invaders has 3 or 4 "Bunkers" or whatever used to protect the player as we all know. So now that I have almost everything else done in my SI clone, I was just wondering about the tidiest and most practical way to implement them. Every single tile acts the same: after 2/3/whatever hits, they disappear, perhaps displaying a damaged graphic halfway through. So every bit of wall tile is the same except for its graphic which depends on its position in the "Bunker" shape and then on its hit points. So all I need is an array of wall section objects, each with the appropriate graphic, to create each bunker. But just using a for loop to set each individual tile based on its position etc seems somewhat inelegant especially if I wanted to change the number of bunkers later. So would the best solution be to create a Wall_Tile class and a Bunker class which then contains an array of Wall_Tile objects for the particular Bunker's wall tiles? That seems more tidy since you could just have a constructor that sets everything up and changing the position of an entire Bunker would be easy if it was done right. Am I thinking along the right lines? I've never really put an object inside another object like this before, or an array, would it be best to use a pointer in this case? How would that be defined? Something like: Wall_Tile (*ptr)[5][5] = new Wall_Tile[5][5]; ? Maybe I'm thinking too deeply into this, but it just seems like there should be a fairly easy and concise way to create some kind of Bunker class that controls all of each Bunker's tiles.

Share this post


Link to post
Share on other sites
You can create a Bunker class that includes a vector of walls (eg. vector <CWall> ).

At the rendering phase you can just drop the right wall image based on the index inside the vector for each bunker (assuming all bunkers are identical) or just use a wall type variable inside your wall class.

There is no problem with putting objects inside other objects, it is even common.

Share this post


Link to post
Share on other sites

Hi,

I think that you have all the right pieces for creating the bunker. Of course, in the old (good) times, the logic for this was much simpler.

Having an object inside an object is nothing special, as a matter of fact, it is rather common after all.



class WallTile
{
};

class Bunker
{
enum{BUNKER_DIMX = 5;};

WallTile Wall[BUNKER_DIMX * BUNKER_DIMX]; //this might be enough for you
//WallTile Wall[BUNKER_DIMX][BUNKER_DIMX]; //or this, although I prefer to put things in 1D-array

//WallTile *Wall; //or this, if you feel that it gives you some advantage to allocate things dynamically

//WallTile *Wall[BUNKER_DIMX * BUNKER_DIMX]; //I don't see any point of dynamically allocating each piece of the wall.
};

Cheers!


Share this post


Link to post
Share on other sites
Quote:
Original post by Sean_Seanston
So now that I have almost everything else done in my SI clone, I was just wondering about the tidiest and most practical way to implement them.

There's often not one single best way to do things. The trick is to get something done with as little hassle as possible, while still keeping possible optimizations in mind should they become necessary.

Quote:
So would the best solution be to create a Wall_Tile class and a Bunker class which then contains an array of Wall_Tile objects for the particular Bunker's wall tiles? That seems more tidy since you could just have a constructor that sets everything up and changing the position of an entire Bunker would be easy if it was done right.

Sounds good. Why don't you try it and see if it's practical or not? :)

Quote:
Am I thinking along the right lines? I've never really put an object inside another object like this before, or an array, would it be best to use a pointer in this case? How would that be defined? Something like: Wall_Tile (*ptr)[5][5] = new Wall_Tile[5][5]; ?

I prefer a std::vector over an array, and surely over a pointer hassle. If you're not familiar with std::vector, it's a container class that's included in the C++ standard library. You can add and remove objects from it whenever you want, it'll automatically resize to make sure there's enough room to store the new object.

Placing objects inside other objects is sometimes referred to as composition. It's a pretty usefull relationship.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this