Autocreate function (with monsters!)

Hi guys, I need help solving a problem in my game. I have around 10 different objects in my game (doors, monsters, swords.. etc) that each have their own individual positions. There is a cave which spawns a monster every time I walk into it. It will create monster_1, monster_2 and so on. Currently I have to define(position etc) each monster beforehand. Thus the amount of monsters will be limited to the amount of monsters I have predefined. I want to avoid predefining their stats by creating an autocreate function which will assign each new monster with new stats triggered by the player walking into the cave.


In my game I currently have to state everything I will be using:
float posX_player= 0.0, posY_player= 0.0, posZ_player = 0;
float posX_door_1= -0.9, posY_door_1= 0.5, posZ_door_1 = 0;
float posX_door_2= 0.5, posY_door_2= -0.9, posZ_door_2 = 0;
float posX_door_3= 1.2, posY_door_3= 1.2, posZ_door_3 = 0;
float posX_monster_1= 0.8, posY_monster_1= 0.2, posZ_monster_1 = 0;

When I spawn something in my game I use this code:

//start
glPushMatrix();
//individual position
glTranslatef(posX_door_3,posY_door_3,posZ_door_3);
//draw graphic
door();
//end
glPopMatrix();

I cannot keep spawning something such as door_100 unless I have already written the above code with door_100.

Thank you in advance.
Any time you are naming things like "var1", "var2", "var3", etc... you are probably doing something very wrong.

In this case... what you want is a dynamically resizable container for all your objects. std::vector is very easy to use for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// put your door information in a class or a struct
struct Door
{
    float posX;
    float posY;
    float posZ;

    // any other properties that you want your door to have
};

// make a vector of that struct so that you can have any number of them:
std::vector<Door> doors;

// you can add new doors with push_back
Door newdoor = {1,2,3};
doors.push_back( newdoor );

// or you can use resize() to set the size explicitly
doors.resize(10); // we now have 10 doors

// to see how many doors you have, you can use size()
if(doors.size() == 10)
{
    // we have exactly 10 doors
}

// and to access individual doors, you can use the [] operator as if it were an array:
for(int i = 0; i < doors.size(); ++i)
{
    glPushMatrix();
    glTranslatef( doors[i].x, doors[i].y, doors[i].z );
    drawDoorGraphic( doors[i] );  // or doors[i].draw() if you make this more class-ish
    glPopMatrix();
}



Lastly... glPushMatrix, glPopMatrix, glTranslate, and probably all the other openGL stuff you're using are outdated (and deprecated in modern versions of OpenGL). I recommend you drop whatever tutorial you're using and use this one instead:

http://www.arcsynthesis.org/gltut/
So use an array of structs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
struct Door
{  float posx; 
    float posy; 
    float posz;
};

//  Add more doors by extending the following array
const int MAX_DOORS = 3;
Door doors[MAX_DOORS] = 
{ -0.9, 0.5, 0, 
   0.5, -0.9, 0, 
   1.2, 1.2, 0 
};

int n = 0;  // door number
glTranslatef (door[n].posx, door[n].posy, door[n].posz); 


This is really a good place to use inheritance.
Define an Object class which contains posx, posy and posz.
Now your door, monster and player can all inherit from the Object class and all have the same posx, posy and posz attributes.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Thanks for responding.
Topic archived. No new replies allowed.