Archived

This topic is now archived and is closed to further replies.

myme15

Comments/Criticism

Recommended Posts

myme15    122
I''m currently in the process of making a top-down 2D space shooter type game. (Anyone ever played Raptor: Call of the Shadows? I''m trying to make a clone of that game.) Basically, waves upon waves of aliens attack you until you kill them or they go off the bottom of the screen. I created an Alien class, and now I''m going to make a bunch of classes that inherit from the Alien class for specific types of enemies. Each type will have different weaponry, graphics, armor, etc. Each of my levels will be stored in a file that records each alien that you fight, its type and some waypoints to describe its path through the game screen. My problem is how to create new aliens of the correct type when I load levels from my file. I planned to have a vector full of Alien pointers that could point to any subclass of alien. However, to create new aliens of the right type, the best code I can think of is something vaguely like this:

cin >> type;

if (type==GREEN_CIRCULAR_THING)
     Aliens[1] = new GreenRoundAlien;

if (type==SHINY_METAL_ALIEN)
     Aliens[1] = new ShinyAlien;

if (type==BOSS1)
     Aliens[1] = new Boss1;

 
and so on for a whole lot more alien types. I could do this, but I want to know if there is a better/simpler alternative. Also, any criticism about the way I am structuring everything is welcome; this is my first non-trivial game project and I want to know if I am doing everything at least semi-logically. Things are not what they are.

Share this post


Link to post
Share on other sites
Kylotan    10008
Yes, this is pretty much the way you have to do it in C++. Perhaps the best implementation though is to encapsulate this in a static ''factory'' method, such as

Alien* Alien::CreateAlien(int type)
{
// your switch statement here, calling new
// and returning the correct subtype

return NULL; // if type was invalid. Or throw an exception
}

So the rest of your code looks like:

cin >> type;
Aliens[1] = Alien::CreateAlien(type);


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites
myme15    122
Oh well, it won''t be that messy to do it with a switch statement, if that is my only option.

If I cheated and used a single Alien class with an Type variable instead of polymorphism, I would just end up with several huge and ugly switch statements in each method. I guess you always have to have at least one big switch statement

Thanks for the reply.






Things are not what they are.

Share this post


Link to post
Share on other sites
Kylotan    10008
Except with the polymorphic method mentioned above, you only ever need 1 switch statement. With a ''type'' value in each Alien, you need a switch statement anywhere in the program that you want the behaviour to vary according to type. But no, you can''t get out of at least one conditional statement to generate different Alien types from untyped bytes in a file in the first place.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites