0

I'm working on a simple 2D top-down Zelda style game in C++, but I'm having trouble getting multiple instances of an enemy class to spawn in. Whenever I spawn more than one of an enemy, only the first one registers any collision detection; all other enemies seem to be merely visual "ghosts" that are rendered to the screen. When the first enemy dies, the only one that can, then all other "ghosts" disappear along with it.

I've created an enemy manager class that uses a vector list to hold active enemies, check each one's collision against any box passed in, and update/render the enemies.

class cEnemyMgr {
public:
std::vector<cEnemy*> mobList;

cEnemyMgr(){}
~cEnemyMgr(){
    for (int i=0; i < mobList.size(); i++) {
        mobList[i]->texture.Close();
            //delete mobList[i];
    } 
}

    void render() {
        for (int i=0; i < mobList.size(); i++) {
            mobList[i]->render();
        }
    }

    void update(float dt){
        for (int i=0; i < mobList.size(); i++) {
            if ( mobList[i]->hp <= 0 ){
                mobList[i]->die();
                mobList.pop_back();
            } else {
                mobList[i]->update(dt);
            }
        }
    }

    void spawnMob(int x, int y){
        cEnemy* pEnemy = new cMeleeEnemy();
        pEnemy->init(x, y);
        mobList.push_back(pEnemy);
    }

    cEnemy* checkCollisions(int x, int y, int wd, int ht){
        for (int i=0; i < mobList.size(); i++) {
            int left1, left2;
            int right1, right2;
            int top1, top2;
            int bottom1, bottom2;

            left1 = x;
            right1 = x + wd;
            top1 = y;
            bottom1 = y + ht;

            left2 = mobList[i]->pos.x;
            right2 = mobList[i]->pos.x + 64;
            top2 = mobList[i]->pos.y;       
            bottom2 = mobList[i]->pos.y + 64;

            if ( bottom1 < top2 ) { return NULL; }
            if ( top1 > bottom2 ) { return NULL; }
            if ( left1 > right2 ) { return NULL; }
            if ( right1 < left2 ) { return NULL; }

            return mobList[i];
        }
    }
};

The enemy class itself is pretty basic; cEnemy is the base class, from which cMeleeEnemy is derived. It has the standard hp, dmg, and movement variables so that it can crawl around the screen to try and collide with the player's avatar and also respond to being attacked by the player. All of this works fine, it's just that when I try to have multiple enemies, only the first one spawned in works correctly while the rest are empty shells, just textures on the screen. It doesn't matter if I make explicit calls to spawnMob rapidly in the same block or if I space them out dynamically with a timer; the result is the same. Can anyone point me in the right direction?

--EDIT-- Here's the code the for enemy.h:

#ifndef ENEMY_H
#define ENEMY_H

#include "texture.h"
#include "timer.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

class cEnemy {
public:
int hp;
int dmg;
D3DXVECTOR2 pos;
D3DXVECTOR2 fwd;
D3DXVECTOR2 vel;
D3DCOLOR color;
int speed;
float rotate;
bool hitStun;
float hitTime;
CTexture texture;

virtual void init(int x, int y) = 0;
virtual void update(float dt) = 0;
virtual void die() = 0;

void render(){
    texture.Blit(pos.x, pos.y, color, rotate);
}

void takeDamage(int dmg) {
    if (hitStun == false){
        extern CTimer Timer;        
        hitTime = Timer.GetElapsedTime();
        hp -= dmg;
        color = 0xFFFF0000;
        hitStun = true;
    }
}

void hitStunned(float duration) {
    extern CTimer Timer;
    float elapsedTime = Timer.GetElapsedTime();
    if ( elapsedTime - hitTime > duration ){
        color = 0xFFFFFFFF;
        hitStun = false;
    }
}

};

class cPlayer : public cEnemy {
public:
int facing;

void init(int x, int y);
void update(float dt);
void die();

};

class cMeleeEnemy : public cEnemy {
public:
cMeleeEnemy(){}
~cMeleeEnemy(){
    texture.Close();
}

void init(int x, int y);
void update(float dt);
void die();

};
#endif

And enemy.cpp:

#include "enemy.h"

void cPlayer::update(float dt){
// Player Controls
if ( KEY_DOWN('W') ) {
    pos.y -= speed * dt;
    facing = 0;
} else if( KEY_DOWN('S') ) {
    pos.y += speed * dt;
    facing = 2;
}

if ( KEY_DOWN('A') ) {
    pos.x -= speed * dt;
    facing = 3;
} else if( KEY_DOWN('D') ) {
    pos.x += speed * dt;
    facing = 1;
}

// Hit Recovery
if ( hitStun == true ) {    
    hitStunned(1.0);
}
}

void cMeleeEnemy::update(float dt){
extern cPlayer player1;
extern int ScreenWd;
extern int ScreenHt;

D3DXVECTOR2 dir;
dir = player1.pos - pos;
D3DXVec2Normalize(&dir, &dir);
//fwd = (fwd * 0.2) + (dir * 0.8);
fwd = dir;
vel = vel + fwd * speed * dt;

pos = pos + vel * dt;

//keep em on screen
if ( pos.x < 0 ) { pos.x = 0; }
if ( pos.x > ScreenWd - 64 ) { pos.x = ScreenWd - 64; }
if ( pos.y < 0 ) { pos.y = 0; }
if ( pos.y > ScreenHt - 64 ) { pos.y = ScreenHt - 64; }

// Hit Recovery
if ( hitStun == true ) {    
    hitStunned(0.5);
}

}

void cMeleeEnemy::die(){
extern int score;
extern int numMobs;
score += 1;
numMobs -= 1;
//texture.Close();
}

void cPlayer::die(){
extern char gameState[256];
sprintf(gameState, "GAMEOVER");
}

void cMeleeEnemy::init(int x, int y){
hp = 6;
dmg = 1;
speed = 25;
fwd.x = 1;
fwd.y = 1;
vel.x = 0;
vel.y = 0;
pos.x = x;
pos.y = y;
rotate = 0.0;
color = 0xFFFFFFFF;
hitStun = false;
texture.Init("media/vader.bmp");
}

void cPlayer::init(int x, int y){
facing = 0;
hp = 10;
dmg = 2;
color = 0xFFFFFFFF;
speed = 100;
fwd.x = 1;
fwd.y = 1;
vel.x = 0;
vel.y = 0;
pos.x = x;
pos.y = y;
rotate = 0.0;
hitStun = false;
texture.Init("media/ben.bmp");
}

As you can tell, I'm not that experienced yet. This is my first on-your-own project for school. I just have to say I'm a little confused on where I should be closing textures and deleting objects. Thanks for your time, guys!

DanTheMan
  • 3
  • 2
  • We need to see the code for the Enemy objects. Also, you appear to be leaking memory unless `die` causes an object to destruct itself. – tmpearce Jun 10 '12 at 21:09
  • Please post code that class checkCollisions() – std''OrgnlDave Jun 10 '12 at 21:11
  • You're right, I am not destroying each instance after it is made. Die() simply changes relevant score points and mob threshold variables currently. Do I do this by using something like delete mobList[i] in the if statement where hp is <= 0? – DanTheMan Jun 10 '12 at 23:18
  • 1) You *could* `delete mobList[i]`, or you could use automatic memory management through something like `shared_ptr`, which is usually the better (safer) option. There are *plenty* of topics on Stack Overflow dealing with that. 2) Take a look at `update` and note that you're "killing" `mobList[i]` but then `pop_back` takes off potentially a different element (if `i` isn't the end of the vector). – tmpearce Jun 11 '12 at 04:45

2 Answers2

2

In your checkCollisions function, you return NULL, or the object at the position of the first index of the enemy vector after every loop.

Therefore, when the first ghost is not hit, the checkCollisions function will return NULL instead of iterating through each of the subsequent ghosts in the vector.

To fix this, change your checkCollisions function to the following:

cEnemy* checkCollisions(int x, int y, int wd, int ht){
    for (int i=0; i < mobList.size(); i++) {
        int left1, left2;
        int right1, right2;
        int top1, top2;
        int bottom1, bottom2;

        left1 = x;
        right1 = x + wd;
        top1 = y;
        bottom1 = y + ht;

        left2 = mobList[i]->pos.x;
        right2 = mobList[i]->pos.x + 64;
        top2 = mobList[i]->pos.y;       
        bottom2 = mobList[i]->pos.y + 64;

        if ( bottom1 < top2 ) { continue; }
        if ( top1 > bottom2 ) { continue; }
        if ( left1 > right2 ) { continue; }
        if ( right1 < left2 ) { continue; }

        return mobList[i];
    }

    return NULL;
}

Hope this helps!

EDIT:

Note that when you are removing an enemy from the list if it's HP is 0 or less, you are using mobList.pop_back(), but this removes the final element from the vector, you should use something like the following to remove the enemy you want from the list:

std::remove_if( mobList.begin(), mobList.end() []( cEnemy* pEnemy )->bool
{
     if( pEnemy->hp <= 0 )
     {
         pEnemy->die();
         return true;
     }
     else
     {
         pEnemy->update();
         return false;
     }
});
Thomas Russell
  • 5,870
  • 4
  • 33
  • 68
  • He may be calling CheckCollisions, deleting the relevant ghost, then calling it again. This may only partially solve the problem. But good catch. – std''OrgnlDave Jun 10 '12 at 21:16
  • This has fixed the problem partially! I can hit and be hit by all instances, but it seems like an enemy that gets destroyed takes all that are created after it with it as well. – DanTheMan Jun 10 '12 at 23:15
  • @DanTheMan We'll need to see your calling code in order to help further then. – Thomas Russell Jun 10 '12 at 23:28
  • Ok I had a feeling it was the pop_back() giving me trouble. I just need to remove the specific element from my vector list, but the trouble is that my IDE is telling me that std has no member "remove_if". – DanTheMan Jun 11 '12 at 19:29
  • @DanTheMan have you included ? – Thomas Russell Jun 11 '12 at 19:31
0

Problem solved! I replaced the pop_back() with mobList.erase() method.

void update(float dt){
        for (int i=0; i < mobList.size(); i++) {
            if ( mobList[i]->hp <= 0 ){
                mobList[i]->die();
                mobList.erase(mobList.begin() + i);
            } else {
                mobList[i]->update(dt);
            }
        }
    }

Thank you all for your help, it's much appreciated!

Botz3000
  • 39,020
  • 8
  • 103
  • 127
DanTheMan
  • 3
  • 2