Hey !!
I got a struct for a type of enemy.
Now i want to make like 10 enemies of the same struct
I used:
int Spawned = 0;
for(int i=0; i < 10;i++)
{
int spd = dbRnd(2);
enemy[i].LoadEnemy(spd);
Spawned ++;
}
Ok, the spd is the speed of the enemy.
This for loop in theory will load 10 enemies from the Enemy struct here it is:
struct Enemy1
{
void LoadEnemy(int dificulty);
void Spawn(int SpawnX, int SpawnY, int id);
void Move(int id, int speed, int LastX, int LastY);
void Update(int id, int speed);
void DeleteEnemy(int id);
bool PathFinished (void) const;
bool cSpawn;
int image_id;
int speed;
int health;
int id;
int level;
int money;
int score;
}enemy[MAX_ENEMIES];
Ok now in the game loop
I got the Update func that updates the enemy movement and all.
Definition:
void Enemy1::Update(int id, int speed)
{
if(cSpawn)
{
Spawn(Map1V[0].x, Map1V[0].y, id);
cSpawn = false;
}
if( Map1V.size() > 0 )
{
PointSprite(id, Map1V[0].x, Map1V[0].y);
dbMoveSprite(id, speed);
int distX = abs ( dbSpriteX(id) - Map1V[0].x );
int distY = abs ( dbSpriteY(id) - Map1V[0].y );
if(distX*distX + distY*distY < speed*speed) //to avoid using sqrt
{
dbSprite ( id, Map1V[0].x, Map1V[0].y, image_id );//not necessary, but it's better to have(more accurate)
Map1V.erase (Map1V.begin ());
}
}
else
{
PathFinished();
}
if(PathFinished())
{
LoadEnemy(1);
}
}
And using:
for(int n=0; n < Spawned; n++)
{
enemy[n].Update(enemy[n].id, enemy[0].speed);
}
It will get the Spawned enemies number and update them.
The problem here is that it's only creating 1 enemy but with his things multiplyed by 10.
here's the Path definition:
Map1 path1[] = {
{ -100, 160 },
{ 318, 160 },
{ 318, 311 },
{ 560, 311 },
{ 560, 151 },
{ 750, 151 },
{ 750, 677 },
{ 611, 677 },
{ 611, 432 },
{ 112, 432 },
{ 109, 686 },
{ 438, 694 },
{ 438, 800 }
};
Map1 path2[] = {
{ -100, 160 },
{ 597, 425 },
{ 94, 440 }
};
int random = dbRnd(100);
if(random <= 50)
{
for( int i = 0; i < sizeof(path1) / sizeof (Map1); i++)
{
Map1V.push_back (path1[i]);
}
}
else
{
for( int i = 0; i < sizeof(path2) / sizeof (Map1); i++)
{
Map1V.push_back (path2[i]);
}
}
It will generate a random number and choose the path for that enemy...
Well... if you need any more code just say
If you know whats wrong please tell me
Thank you
C++ Medium 3.5/5
www.oryzhon.com <-- My company's website (W.I.P)