The solution for this problem are enumerations. These are lists of words. Internally the first word is equal to zero and every word after that is a higher number. The interesting part is that you can use these words in your code, but the application will use numbers internally.
enum ENEMY_TYPE {
ET_NONE ,
ET_WARRIOR,
ET_ROGUE ,
5 ET_PRIEST ,
}
class enemy {
ENEMY_TYPE type = ET_NONE;
10 Rect r;
void setType(ENEMY_TYPE type) {
T.type = type;
}
15
void draw() {
switch(type) {
case ET_WARRIOR: Images(=== warriorImage ===).draw(r); break;
case ET_ROGUE : Images(=== rogueImage ===).draw(r); break;
20 case ET_PRIEST : Images(=== priestImage ===).draw(r); break;
}
}
}
Values like ET_WARRIOR or ET_ROGUE can be used anywhere in your code. The method setType won’t accept a type of enemy which doesn’t exists. And at every moment you will have clear idea of what you’re doing.
The values of an enum are often capitals. This is just a convention, but it’s a good one. You can instantly see that this is not a variable. And just like with constants (also often written in capitals), the value of an enum cannot be changed. Something like ET_WARRIOR = 3 is not possible.
Starting the enum value with ET_ is also a matter of choice. I like to use the first characters of the enumeration in its value name because it makes them easier to remember. “ENEMY_TYPE” can be remembered as ET. The autocomplete of the editor will help you by showing your options as soon as you’ve typed ET_.