Original Post
Ok so i have a little bit of a problem regarding using boost's ptr_vector and performing an operation in order for me to get somthing sorted on my game. Its probably a problem with how i'm going about it so if someone could make some suggestions it would be much appreaciated. Ok here goes, firstly i have my base class and my derived class; Now for one reason or another the a rabbit object needs to know the direction the player is facing. I would normally pass this into an update function, but because the update function is inherited i can't (or can i!?) change it's parameters. My enemies are stored in a boost::ptr_vector declared and filled like this; I am also cleaning up the enemy vector with using stl algorithms; Almost there now people, thanks for sticking with me this far. In order to set the player dir in the rabbit objects i do this; Now to me that make sense but i'm sure its very wrong. Anyway with the above code in i get an assertion that seems to emenate from where i am cleaning up the enemy list. I'm not sure but it seems to be something related to deleting a pointer that is not there anymore. Ok so what if someone could point some things out that would be good. Is this a problem with how i'm using pointers and containers? i.e. is some of the above just stupid Is this a design problem? i.e. How could i change me classes to not have this issue of needed to check the type of the object and cast it to set the direction. Any help much appreciated. Thanks. [Edited by - JimmyDeemo on March 2, 2009 6:20:48 AM]
class CBaseEnemy
{
public:
///...stuff here
virtual void Update( D3DXVECTOR3 target, float delta );
///...stuff here
private:
///...stuff here
};
class CRabbit : public CBaseEnemy
{
public:
CRabbit(D3DXVECTOR3 startPos);
virtual ~CRabbit(void);
void SetPlayersDir( D3DXVECTOR3 dir ) {m_playersDir = dir;}
void Update( D3DXVECTOR target, float delta );
private:
D3DXVECTOR3 m_playersDir;
};
class MainGame
{
//....
private:
CBaseEnemy* GetNewEnemy( eMainGameSpriteIDs enemyID = eInvalidId );
//....
boost::ptr_vector<CBaseEnemy> m_pEnemies;
};
CBaseEnemy* CMainGame::GetNewEnemy( eMainGameSpriteIDs enemyID )
{
CBaseEnemy* newEnemy;
switch( enemyID )
{
case eChicken:
newEnemy = new CChicken( GenEnemyStartPos() );
break;
case eRabbit:
newEnemy = new CRabbit( GenEnemyStartPos() );
break;
case eCow:
newEnemy = new CCow( GenEnemyStartPos() );
break;
case eLlama:
newEnemy = new CLlama( GenEnemyStartPos() );
break;
}
return newEnemy;
}
//In MainGame::Update()
if( m_spawnTimer.Expired( 1000 ) )
{
m_spawnTimer.Restart();
m_pEnemies.push_back( GetNewEnemy() );
}
bool EnemyPred( CBaseEnemy e )
{
//TODO: Make the time a static const in the bullet class.
return e.IsDead();
}
//In maingame::Update
if( m_pEnemies.size() > 0 )
{
m_pEnemies.erase( std::remove_if( m_pEnemies.begin(),m_pEnemies.end(),EnemyPred ), m_pEnemies.end() );
}
boost::ptr_vector<CBaseEnemy>::iterator itor;
for( itor = m_pEnemies.begin(); itor < m_pEnemies.end(); ++itor )
{
//This seems to cause memory issues. Maybe smart pointer?
//Special cases dependant on enemy type
switch( itor->SpriteId() )
{
case eRabbit:
//Set the players dir
CRabbit* rabbit = static_cast<CRabbit*>(&(*itor));
rabbit->SetPlayersDir( m_player.GetPlayerDir() );
break;
}
itor->Update( m_player.Position(), fDelta );
}