-
-
Save tks2shimizu/cc3741fe830c2e06a2cc to your computer and use it in GitHub Desktop.
シューティングゲームを作ろう!(5)弾の発射
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Bullet.h" | |
USING_NS_CC; | |
//初期化 | |
bool Bullet::init() | |
{ | |
if (!Sprite::initWithFile("Bullet.png")) | |
{ | |
return false; | |
} | |
auto winSize = Director::getInstance()->getWinSize(); | |
//弾が移動するアニメーション | |
auto move = MoveBy::create(1, Vec2(0, winSize.height)); | |
auto remove = RemoveSelf::create(); | |
auto seq = Sequence::create(move, remove, nullptr); | |
runAction(seq); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __NyanSky__Bullet__ | |
#define __NyanSky__Bullet__ | |
#include "cocos2d.h" | |
class Bullet : public cocos2d::Sprite | |
{ | |
public: | |
virtual bool init(); //初期化 | |
CREATE_FUNC(Bullet); //create関数マクロ | |
}; | |
#endif /* defined(__NyanSky__Bullet__) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GameLayer.h" | |
#include "Player.h" | |
#include "Enemy.h" | |
#include "Bullet.h" | |
USING_NS_CC; | |
#define BULLET_INTERVAL 0.5 //弾発射の間隔(秒) | |
//シーン作成 | |
Scene* GameLayer::createScene() | |
{ | |
auto scene = Scene::create(); | |
auto layer = GameLayer::create(); | |
scene->addChild(layer); | |
return scene; | |
} | |
//初期化 | |
bool GameLayer::init() | |
{ | |
if (!Layer::init()) | |
return false; | |
//背景を表示 | |
initBackground(); | |
//プレーヤーを表示 | |
initPlayer(); | |
//タップイベントの初期化 | |
initTouchEvent(); | |
//敵設定の初期化 | |
initEnemyConfig(); | |
//毎フレーム処理の開始 | |
scheduleUpdate(); | |
return true; | |
} | |
//背景を表示 | |
void GameLayer::initBackground() | |
{ | |
//画面サイズの取得 | |
Size winSize = Director::getInstance()->getWinSize(); | |
//背景の生成 | |
auto background = Sprite::create("Background.png"); | |
//背景のアンカーポイント設定 | |
background->setAnchorPoint(Vec2(0.5, 0)); | |
//背景の位置設定 | |
background->setPosition(Vec2(winSize.width / 2, 0)); | |
//背景の追加 | |
addChild(background, Z_Bg); | |
//アニメーションの作成 | |
auto movePosition = Vec2(winSize.width / 2, winSize.height - background->getContentSize().height); | |
auto move = MoveTo::create(120, movePosition); | |
//アニメーションの実行 | |
background->runAction(move); | |
} | |
//プレーヤーを表示 | |
void GameLayer::initPlayer() | |
{ | |
Size winSize = Director::getInstance()->getWinSize(); | |
//プレーヤーの生成 | |
_player = Player::create(); | |
//プレーヤーの位置設定 | |
_player->setPosition(winSize.width * 0.5, winSize.height * 0.2); | |
//プレーヤーの追加 | |
addChild(_player, Z_Player); | |
} | |
//タップイベントの初期化 | |
void GameLayer::initTouchEvent() | |
{ | |
//シングルタップイベントの取得 | |
auto touchListener = EventListenerTouchOneByOne::create(); | |
touchListener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); | |
touchListener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this); | |
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); | |
} | |
//敵設定の初期化 | |
void GameLayer::initEnemyConfig() | |
{ | |
//敵のタイプと出現時間を配列で設定する | |
//今回はあらかじめ出現時間順にソートしておくこと | |
_enemyConfigs = | |
{ | |
{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, | |
{2, 8}, {2, 9}, {2, 10}, {2, 11}, {2, 12} | |
}; | |
} | |
//敵を表示する | |
void GameLayer::showEnemy() | |
{ | |
//敵を生成する | |
auto type = static_cast<Enemy::EnemyType>(_enemyConfigs[0].enemyType); | |
auto enemy = Enemy::create(type); | |
addChild(enemy, Z_Enemy, T_Enemy); | |
//配列から表示済みの敵を削除する | |
_enemyConfigs.erase(_enemyConfigs.begin()); | |
} | |
//弾を表示する | |
void GameLayer::showBullet() | |
{ | |
//弾を生成する | |
auto bullet = Bullet::create(); | |
bullet->setPosition(_player->getPosition()); | |
addChild(bullet, Z_Bullet, T_Bullet); | |
//弾発射の時間を保持 | |
_lastBulletTime = _time; | |
} | |
//毎フレーム処理 | |
void GameLayer::update(float dt) | |
{ | |
//トータル時間を加算する | |
_time += dt; | |
//敵の表示判定 | |
if (_enemyConfigs.size() > 0 && | |
_enemyConfigs[0].appearanceTime <= _time) | |
{ | |
//まだ出現していない敵が存在する | |
showEnemy(); | |
} | |
//弾の発射判定 | |
if (_lastBulletTime + BULLET_INTERVAL < _time) | |
{ | |
//弾発射のタイミング | |
showBullet(); | |
} | |
} | |
#define mark - タップイベント | |
bool GameLayer::onTouchBegan(Touch* touch, Event* event) | |
{ | |
return true; | |
} | |
void GameLayer::onTouchMoved(Touch* touch, Event* event) | |
{ | |
//スワイプした移動距離分、プレーヤーを移動する | |
_player->setPosition(_player->getPosition() + touch->getDelta()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __NyanSky__GameLayer__ | |
#define __NyanSky__GameLayer__ | |
#include "cocos2d.h" | |
class Player; | |
class GameLayer : public cocos2d::Layer | |
{ | |
protected: | |
//Zオーダー | |
enum ZOrder | |
{ | |
Z_Bg = 0, | |
Z_Enemy, | |
Z_Bullet, | |
Z_Player, | |
}; | |
//タグ | |
enum Tag | |
{ | |
T_Enemy = 1, | |
T_Bullet, | |
}; | |
//敵の設定 | |
struct EnemyConfig | |
{ | |
int enemyType; //敵のタイプ | |
float appearanceTime; //出現時間 | |
}; | |
Player* _player; //プレーヤー | |
float _time; //ゲーム時間 | |
std::vector<EnemyConfig> _enemyConfigs; //敵の設定 | |
float _lastBulletTime; //弾を発射した時間 | |
void initBackground(); //背景を表示 | |
void initPlayer(); //プレーヤーを表示 | |
void initTouchEvent(); //タップイベントの初期化 | |
void initEnemyConfig(); //敵設定の初期化 | |
void showEnemy(); //敵を表示する | |
void showBullet(); //弾を表示する | |
public: | |
static cocos2d::Scene* createScene(); //シーンを作成 | |
virtual bool init(); //初期化 | |
CREATE_FUNC(GameLayer); //create関数作成 | |
virtual void update(float dt); //毎フレーム処理 | |
//タップイベント | |
virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); | |
virtual void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); | |
}; | |
#endif /* defined(__NyanSky__GameLayer__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment