第11回:パワーアップさせよう
ゲームにパワーアップアイテムの概念を追加することにしましょう。まずはプレイヤークラスにパワーアップに関するフラグや処理を追加します。このプログラムでは、パワーアップしてpowerupフラグが有効になっていれば、弾発射を3way弾に切り替るようにしています。
class CPlayer : public CCharactor
{
/* 省略 */
private:
bool powerup;
public:
void PowerUp(){ powerup = true; }
};
CPlayer::CPlayer()
{
/* 省略 */
powerup = false;
}
void CPlayer::Shoot()
{
AppendObject(new CPlayerBullet(x, y - 8.0f, d2r(0.0f)), BULLET_PRIORITY, true);
if(powerup == true){
// パワーアップ状態なら左右2方向にも弾を発射
AppendObject(new CPlayerBullet(x, y - 8.0f, d2r(20.0f)), BULLET_PRIORITY, true);
AppendObject(new CPlayerBullet(x, y - 8.0f, d2r(-20.0f)), BULLET_PRIORITY, true);
}
}
次にパワーアップアイテムを追加します。パワーアップアイテムの振る舞いは、プレイヤーに衝突したときの処理が異なる程度で、敵キャラクターとほぼ同じ振る舞いです。
class CPowerupItem : public CCharactor
{
private:
int frame, animframe;
CPlayer *player;
public:
CPowerupItem(float sx, float sy);
protected:
virtual void Init();
virtual void Exec();
};
CPowerupItem::CPowerupItem(float sx, float sy)
{
x = sx;
y = sy;
frame = 0;
animframe = 0;
}
void CPowerupItem::Init()
{
sprite.SetTexture((CTexture*)FindItemBox("item"));
sprite.SetSpriteSize(32, 17);
player = (CPlayer*)FindItemBox("player");
}
void CPowerupItem::Exec()
{
// アニメーション処理
frame++;
if(frame == 10){
frame = 0;
sprite.SetFrame(animframe++);
if(animframe >= 6) animframe = 0;
}
y += 1.2f;
if(y > 500.0f){
RemoveObject(this);
return;
}
// プレイヤーと衝突したら、プレイヤーにパワーアップ処理を行う
if(player->HitTest(this)){
player->PowerUp();
RemoveObject(this);
return;
}
sprite.Draw(x, y);
}
パワーアップアイテムは中ボスを破壊したときにを出現させることにしましょう。
void CEnemy3::Damaged()
{
hardness--;
if(hardness == 0){
/* 省略 */
AppendObject(new CPowerupItem(x, y), PLAYER_PRIORITY, true);
RemoveObject(this);
}
}