#include "GameScene.h" #include "PauseScene.h" #include "GameOverScene.h" #include "SimpleAudioEngine.h" USING_NS_CC; Scene* GameScreen::createScene() { auto scene = GameScreen::createWithPhysics(); scene->getPhysicsWorld()->setGravity(Vec2(0, 0)); auto layer = GameScreen::create(); scene->addChild(layer); return scene; } bool GameScreen::init() { if ( !Scene::init() ) { return false; } auto backgroundSprite = Sprite::create("background.png"); backgroundSprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); addChild(backgroundSprite, -1); player = std::make_unique(visibleSize, origin); addChild(player->getSprite(),-1); spawnNewApple(true); auto menuPause = MenuItemImage::create("pause.png", "pause.png", CC_CALLBACK_1(GameScreen::GoToPauseScene, this)); menuPause->setPosition(Point(menuPause->getContentSize().width - (menuPause->getContentSize().width / 4) + origin.x, visibleSize.height - menuPause->getContentSize().height + (menuPause->getContentSize().width / 4) + origin.y)); auto hpLabel = Label::createWithSystemFont("HP: " + std::to_string(player->get_hp()), "Arial", 20); hpLabel->setName("hpLabel"); addChild(hpLabel); hpLabel->setPosition(menuPause->getPosition().x, menuPause->getPosition().y / 2); auto menu = Menu::create(menuPause, NULL); menu->setPosition(Point::ZERO); addChild(menu); // this->schedule(schedule_selector(GameScreen::spawnNumber), 1.0); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2 (GameScreen::onTouchBegan, this); getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameScreen::onContactBegin, this); getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; } void GameScreen::GoToGameOverScene(Ref* pSender) { auto scene = GameOver::createScene(); Director::getInstance()->replaceScene(scene); } void GameScreen::GoToPauseScene(Ref* pSender) { auto scene = PauseMenu::createScene(); Director::getInstance()->pushScene(scene); } bool GameScreen::onTouchBegan(Touch* touch,Event* event) { auto action = MoveTo::create(2.0, Point(touch->getLocation().x, touch->getLocation().y)); player->getSprite()->runAction(action); return true; } bool GameScreen::onContactBegin(PhysicsContact& contact) { if (!apple->getIsGood()) { player->decrease_hp(); if (player->get_hp() <= 0) GoToGameOverScene(this); else dynamic_cast(getChildByName("hpLabel"))->setString("HP: " + std::to_string(player->get_hp())); } spawnNewApple(false); return true; } void GameScreen::spawnNewApple(bool firstapple) { srand(unsigned(time(NULL))); auto randx = rand() % (int)visibleSize.width; auto randy = rand() % (int)visibleSize.height; cocos2d::Vec2 spawnPosition(randx, randy); if (firstapple) { apple = std::make_unique(spawnPosition, true); addChild(apple->getSprite(), -1); } else apple->getSprite()->setPosition(spawnPosition); apple->setIsGood(randx % 2 == 0); }