#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; } |