それを一括してやりたいと思っていたとき学校でvirtualというものを学びました。
それを使って以下のようなプログラムをくみました
GameMain.h
class GameMain : public ofBaseApp{
EnemyBase enemybase[16];
public:
void setup();
void update();
void draw();
};
GameMain.cpp
void* gpImage = NULL;
static const int ENEMY_COUNT_MAX = 200;
EnemyBase* gpEnemy[ENEMY_COUNT_MAX]; // 敵配列
//---------------------------------------------------------------------------
//! 敵配列に追加する
//!@param [in] p 追加する敵クラス
//!@param true 成功
//!@param false 失敗(満杯)
//---------------------------------------------------------------------------
bool addEnemy(EnemyBase* p)
{
for( int i = 0; i < ENEMY_COUNT_MAX; i++){
if( gpEnemy[i] != NULL ) continue;
//---- find
gpEnemy[i] = p;
return true;
}
return false;
}
//--------------------------------------------------------------
void GameMain::setup(){
//==========================================================
// 敵初期化
//==========================================================
for( int i=0; i<10; i++){
EnemyBase* p = new Crow;
if( addEnemy(p) == false){
delete p;
}
}
}
//--------------------------------------------------------------
void GameMain::update(){
//==========================================================
// 敵更新処理
//==========================================================
for( int i=0; i<ENEMY_COUNT_MAX; i++ ){
if( gpEnemy[i] == NULL ) continue;
gpEnemy[i]->update();
gpEnemy[i]->enemysetMove(bouei.mov);
}
for( int i=0; i<ENEMY_COUNT_MAX; i++ ){
if(CheckHitCircle( bouei.pos._x, bouei.pos._y, gpEnemy[i]->pos._x, gpEnemy[i]->pos._y, bouei.radius, gpEnemy[i]->radius) != true)
}
}
//--------------------------------------------------------------
void GameMain::draw(){
for( int i=0; i<ENEMY_COUNT_MAX; i++)
{
if( gpEnemy[i] == NULL) continue;
gpEnemy[i]->draw();
}
}
このChecHitCircleを50~54行目のように一回だけの呼び出しでgpEnemyすべてのあたり判定はできませんか?
virtualのように変数を仮想化して子クラスが上書きできるようなシステムがあれば教えてほしいです。
Hit.h
bool CheckHitCircle(float posx, float posy, float tposx, float tposy, float radius, float tradius);
// 二つのオブジェクトのあたり判定
bool CheckHitCircle(float posx, float posy, float tposx, float tposy, float radius, float tradius){
float x = posx - tposx;;
float y = posy - tposy;;
float r = radius + tradius;
float length = sqrtf(x * x + y * y);
if( length > r)
{
return true;
}else if( length < r )
{
return false;
}
}
あと、プログラムの書き方でアドバイスがあればお願いします。
回答よろしくお願いします。