2014. 11. 26. 17:32
[D_012] 종합 예제_01 DATA_OLD/Cocos2d-x 기능성 게임 개발과정2014. 11. 26. 17:32
HelloWorldScene.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "SimpleAudioEngine.h" #define TAG_SPRITE_PLAYER 1 #define TAG_SPRITE_ENEMY 2 #define TAG_ACTION_MOVE 10 #define TAG_SPRITE_HP 100 #define TAG_SPRITE_SP 101 #define TAG_LABEL_GAMEOVER 200 #define TAG_ACTION_MOVE 10 USING_NS_CC; using namespace CocosDenshion; class SpriteEnemy : public Sprite { public: CREATE_FUNC(SpriteEnemy); bool isAttacked; bool isAttack; int hp; // ::create()메소드를 재정의한것. //클래스를 선언해주면 무조건 이부분을 public영역 내에 추가해줘야한다. }; class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); bool isJump; Vector<SpriteEnemy*> enemies; EventListenerTouchOneByOne* listener; Size winSize; int hp; int sp; bool isOver; virtual bool init(); void initBG(); //배경 초기화 void initPlayer(); //주인공 초기화 void initEnemy(float delta); //적 초기화 void initUI(); //UI 초기화 void initGameover(); void setAttack(Ref* sender, bool atk); //공격받는지 여부 void setDamage(Ref* sender, bool atk); //적이 공격받았는지 여부 void setHP(); void setSP(); void setGameover(); void setJump(bool flag); void update(float delta); bool onTouchBegan(Touch* touch, Event* unused_event); CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__ |
HelloWorldScene.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | #include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !Layer::init() ) { return false; } //브금 재생 SimpleAudioEngine::getInstance()->playBackgroundMusic("Sound/bgm.mp3", true); winSize = Director::getInstance()->getWinSize(); isOver = false; enemies.clear(); //벡터 초기화 listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); initBG(); initPlayer(); initUI(); initGameover(); this->scheduleUpdate(); this->schedule(schedule_selector(HelloWorld::initEnemy), 1); return true; } void HelloWorld::initBG() { auto node = ParallaxNode::create(); this->addChild(node); node->runAction(RepeatForever::create(Sequence::create( MoveBy::create(20.0f, Vec2(-1280, 0)), Place::create(Vec2::ZERO), NULL))); //하늘 - 정지 auto bg0_1 = Sprite::create("BG/bg_0.png"); bg0_1->setAnchorPoint(Vec2::ZERO); node->addChild(bg0_1, 0, Vec2(0, 0), Vec2::ZERO); //산 - 천천히 for (int i = 0; i < 2; i++) { auto bg2 = Sprite::create("BG/bg_1.png"); bg2->setAnchorPoint(Vec2::ZERO); node->addChild(bg2, 0, Vec2(1.0f, 0), Vec2(1280 * i, 0)); } //땅 - 평범히 for (int i = 0; i < 3; i++) { auto bg2 = Sprite::create("BG/bg_3.png"); bg2->setAnchorPoint(Vec2::ZERO); node->addChild(bg2, 0, Vec2(2, 0), Vec2(1280*i, 0)); } //강 for (int i = 0; i < 3; i++) { auto bg3 = Sprite::create("BG/bg_2.png"); bg3->setAnchorPoint(Vec2::ZERO); node->addChild(bg3, 0, Vec2(2.5, 0), Vec2(1280*i, 0)); } } void HelloWorld::initPlayer() { CCLOG("initHero()"); isJump = false; SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Player/player.plist"); //캐시에 plist 등록 auto player = Sprite::createWithSpriteFrameName("walk01.png"); player->setTag(TAG_SPRITE_PLAYER); player->setAnchorPoint(Vec2(0.5, 0)); player->setPosition(Vec2(100, 140)); this->addChild(player); auto walking = Animation::create(); walking->setDelayPerUnit(0.05f); for (int i = 0; i < 10; i++) { walking->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(StringUtils::format("walk%02d.png", i+1))); } player->runAction( RepeatForever::create( Animate::create(walking)) ); } void HelloWorld::initEnemy(float delta) { CCLOG("initEnemy()"); //auto enemy = SpriteEnemy::create("Enemy/flyFly1.png"); auto enemy = SpriteEnemy::create(); enemy->isAttack = false; enemy->isAttacked = false; enemy->hp = 100; enemy->setTag(TAG_SPRITE_ENEMY); enemy->setAnchorPoint(Vec2(0.5, 0)); enemy->setPosition(Vec2(680, 140)); enemies.pushBack(enemy); //벡터에 추가해줌 this->addChild(enemy); auto ani = Animation::create(); ani->setDelayPerUnit(0.1f); ani->addSpriteFrameWithFile("Enemy/flyFly1.png"); ani->addSpriteFrameWithFile("Enemy/flyFly2.png"); auto walking = Animate::create(ani); enemy->runAction(RepeatForever::create(walking)); auto action = Sequence::create( MoveBy::create(10, Vec2(-700, 0)), RemoveSelf::create(), NULL); action->setTag(TAG_ACTION_MOVE); enemy->runAction(action); } void HelloWorld::update(float delta) { CCLOG("update()"); auto player = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); for (SpriteEnemy* enemy : enemies) { if ( !enemy->isAttack && hp>0 && enemy->getBoundingBox().intersectsRect(player->getBoundingBox())) { CCLOG("충돌함"); enemy->isAttack = true; hp = hp - 20; setHP(); enemy->stopAction(enemy->getActionByTag(TAG_ACTION_MOVE)); enemy->runAction(Sequence::create( DelayTime::create(1.0f), CallFuncN::create(CC_CALLBACK_1(HelloWorld::setAttack, this, false)), NULL)); return; } else if ( hp <= 0 ) { auto label = (Label*)this->getChildByTag(TAG_LABEL_GAMEOVER); label->runAction(Sequence::create( FadeIn::create(0.5f), CallFunc::create( CC_CALLBACK_0(HelloWorld::setGameover, this) ), NULL)); return; // Director::getInstance()->pause(); } } } bool HelloWorld::onTouchBegan(Touch* touch, Event* unused_event) { if ( !isOver) //끝났으면 터치 안댐ㅎ { auto player = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); auto pos = touch->getLocation(); for (SpriteEnemy* enemy : enemies) { if ( !enemy->isAttacked && enemy->getBoundingBox().containsPoint(pos)) { enemy->isAttacked = true; SimpleAudioEngine::getInstance()->playEffect("Sound/shoot.wav"); auto particle = ParticleSystemQuad::create("ParticleNoteBomb.plist"); particle->setPosition(enemy->getPosition()); particle->setScale(0.3f); this->addChild(particle); particle->runAction(Sequence::create( DelayTime::create(0.5f), RemoveSelf::create(), NULL)); enemy->hp -= 50; enemy->stopActionByTag(TAG_ACTION_MOVE); //멈추고 auto action = Sequence::create( //밀려남 JumpBy::create(0.5, Vec2(30, 0), 30, 1), MoveBy::create(10.0f, Vec2(-700, 0)), NULL); action->setTag(TAG_ACTION_MOVE); enemy->runAction(action); enemy->runAction(Sequence::create( DelayTime::create(1.0f), CallFuncN::create(CC_CALLBACK_1(HelloWorld::setDamage, this, false)), NULL)); if (enemy->hp <= 0) //클릭성공 후 애 피가 0이하면 { SimpleAudioEngine::getInstance()->playEffect("Sound/boom.wav"); auto particle = ParticleSystemQuad::create("Particle_texture.plist"); particle->setPosition(enemy->getPosition()); particle->setScale(0.3f); this->addChild(particle); enemy->runAction(Sequence::create( //사라지고 지워짐 FadeOut::create(0.3f), RemoveSelf::create(), NULL)); enemies.eraseObject(enemy); return true; } if ( sp< 100) { sp += 10; setSP(); } return true; } } if ( !isJump ) { setJump(true); SimpleAudioEngine::getInstance()->playEffect("Sound/shoot.wav"); player->runAction( Sequence::create( JumpBy::create(0.5f, Vec2(0, 0), 100, 1), CallFunc::create(CC_CALLBACK_0(HelloWorld::setJump, this, false)), NULL)); } else if (Director::getInstance()->isPaused()) { CCLOG("game is over"); } return true; } } void HelloWorld::setJump(bool flag) { isJump = flag; } void HelloWorld::initUI() { hp = 100; sp = 0; auto hpBack = Sprite::create("UI/gauge.png"); hpBack->setAnchorPoint(Vec2(0, 0.5)); hpBack->setPosition(Vec2(30, winSize.height - 30)); hpBack->setColor(Color3B::GRAY); hpBack->setOpacity(128); this->addChild(hpBack); auto hpBar = Sprite::create("UI/gauge.png"); hpBar->setAnchorPoint(Vec2(0, 0.5)); hpBar->setPosition(Vec2(30, winSize.height - 30)); hpBar->setColor(Color3B::RED); hpBar->setOpacity(128); hpBar->setTag(TAG_SPRITE_HP); this->addChild(hpBar); auto hp = Sprite::create("UI/hp.png"); hp->setPosition(Vec2(30, winSize.height - 30)); hp->setZOrder(1); this->addChild(hp); auto spBack = Sprite::create("UI/gauge.png"); spBack->setAnchorPoint(Vec2(1, 0.5)); spBack->setPosition(Vec2(winSize.width - 30, winSize.height - 30)); spBack->setColor(Color3B::GRAY); spBack->setOpacity(128); this->addChild(spBack); auto spBar = Sprite::create("UI/gauge.png"); spBar->setAnchorPoint(Vec2(1, 0.5)); spBar->setPosition(winSize.width - 30, winSize.height - 30); spBar->setColor(Color3B::BLUE); spBar->setOpacity(128); spBar->setTag(TAG_SPRITE_SP); this->addChild(spBar); auto sp = Sprite::create("UI/sp.png"); sp->setPosition(Vec2(winSize.width - 30, winSize.height - 30)); this->addChild(sp); setHP(); setSP(); } void HelloWorld::setHP() { CCLOG("setHP"); auto bar = (Sprite*)this->getChildByTag(TAG_SPRITE_HP); bar->setScaleX(0.01*hp); } void HelloWorld::setSP() { CCLOG("setSP"); auto bar = (Sprite*)this->getChildByTag(TAG_SPRITE_SP); bar->setScaleX(0.01*sp); } void HelloWorld::initGameover() { auto label = Label::createWithTTF("GAME OVER", "Font/Abduction.ttf", 48); label->setPosition(winSize.width / 2, winSize.height / 2); label->setOpacity(0); label->enableOutline(Color4B(0, 0, 255, 128), 3); label->setColor(Color3B::BLUE); label->setTag(TAG_LABEL_GAMEOVER); this->addChild(label); CCLOG("initGameover"); } void HelloWorld::setGameover() { isOver = true; Director::getInstance()->pause(); } void HelloWorld::setAttack(Ref* sender, bool atk) { CCLOG("HelloWorld::setAttack"); auto enemy = (SpriteEnemy*)sender; enemy->isAttack = atk; } void HelloWorld::setDamage(Ref* sender, bool atk) { auto enemy = (SpriteEnemy*)sender; enemy->isAttacked = atk; } |
코드 파일
'DATA_OLD > Cocos2d-x 기능성 게임 개발과정' 카테고리의 다른 글
[D_014] 묶인 스프라이트(Texture) 사용하기 (0) | 2014.11.28 |
---|---|
[D_013] 간단한 데이터 저장방법 UserDefault (0) | 2014.11.27 |
[D_012] Vector를 이용한 데이터 관리 예제 (0) | 2014.11.26 |
[D_012] Sound Effect (0) | 2014.11.26 |
[D_011] 애니메이션/스크롤/커스텀 파티클 예제 (0) | 2014.11.25 |