bool HighscoreLayer::init() { if(!LayerColor::init()) { return false; } Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); // setContentSize(visibleSize); Widget* root = GUIReader::getInstance()->widgetFromJsonFile("ui/connect/high_score.json"); addChild(root); root->setContentSize(visibleSize); Node* content = root->getChildByName("content"); content->setPositionY((visibleSize.height-content->getContentSize().height)/2); Text* endlessHighscoreLabel = dynamic_cast<Text*>(content->getChildByName("endlessHighScoreLabel")); endlessHighscoreLabel->setString(LANG->getLang("high_score_endless_label")); Text* endlessHighscoreText = dynamic_cast<Text*>(content->getChildByName("endlessHighscoreText")); endlessHighscoreText->setString(StringUtils::format("%d", UserData::getInstance()->getEndlessHighscore())); Text* classicHighscoreLabel = dynamic_cast<Text*>(content->getChildByName("classicHighScoreLabel")); classicHighscoreLabel->setString(LANG->getLang("high_score_classic_label")); Text* classicHighscoreText = dynamic_cast<Text*>(content->getChildByName("classicHighscoreText")); classicHighscoreText->setString(StringUtils::format("%d", UserData::getInstance()->getClassicHighscore())); Util::registerButton(root, "back", [=](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { Director::getInstance()->replaceScene(MainMenuLayer::createScene()); } break; default: break; } }, ""); Node* backButton = root->getChildByName("back"); auto backButtonSize = backButton->getContentSize(); backButton->setPosition(backButtonSize.width/2+20, visibleSize.height-backButtonSize.height/2 - 15); Text* title = dynamic_cast<Text*>(content->getChildByName("title")); title->setString(LANG->getLang("high_score_title")); return true; }
bool TimeUpMenuLayer::init() { if(!LayerColor::init()) { return false; } Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); // setContentSize(visibleSize); Widget* mainMenuLayer = GUIReader::getInstance()->widgetFromJsonFile("ui/connect/time_up_menu.json"); addChild(mainMenuLayer); mainMenuLayer->setContentSize(visibleSize); Node* mainMenu = mainMenuLayer->getChildByName("content"); mainMenu->setPositionY((visibleSize.height-mainMenu->getContentSize().height)/2); Util::registerButton(mainMenu, "tryAgain", [&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { m_gameLayer->reset(); removeFromParent(); } break; default: break; } }, "time_up_menu_try_again"); Util::registerButton(mainMenu, "toMainMenu", [=](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { auto scene = MainMenuLayer::createScene(); director->replaceScene(scene); } break; default: break; } }, "pause_menu_back_to_main_menu"); Text* title = dynamic_cast<Text*>(mainMenu->getChildByName("title")); title->setString(LANG->getLang("time_up_menu_title")); return true; }
bool IdleLayer::init() { if(!LayerColor::init()) { return false; } Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); setContentSize(visibleSize); GameLayer* gameLayer = GameLayer::create(IDLE); gameLayer->getUiLayer()->setVisible(false); addChild(gameLayer); // gameLayer->setColor(Color3B::RED); Widget* idleForeground = GUIReader::getInstance()->widgetFromJsonFile("ui/connect/idle_foreground.json"); Node* background = idleForeground->getChildByName("background"); background->setContentSize(visibleSize); Text* touchToStart = dynamic_cast<Text*>(idleForeground->getChildByName("touchToStart")); touchToStart->setString(LANG->getLang("touch_to_start")); touchToStart->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2)); touchToStart->runAction(RepeatForever::create(Blink::create(1, 1))); addChild(idleForeground); EventDispatcher* eventDispatcher = director->getEventDispatcher(); EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create(); eventListener->setSwallowTouches(true); eventListener->onTouchEnded = [=](Touch* touch, Event* event){ auto scene = MainMenuLayer::createScene(); director->replaceScene(scene); }; eventListener->onTouchBegan = [](Touch* touch, Event* event){ return true; }; eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, touchToStart); CallFunc* connectAction = CallFunc::create([=](){ auto game = gameLayer->getGame(); if (!game->isEmpty() && gameLayer->getAcceptInput()) { auto path = game->getRandomPath(); if(path) { auto front = path->getPath().front(); auto back = path->getPath().back(); gameLayer->doConnect(front->getX(), front->getY(), back->getX(), back->getY()); } } }); CallFunc* shuffleAction = CallFunc::create([=](){ auto game = gameLayer->getGame(); if (!game->isEmpty()) { if (rand()%10 == 0) { gameLayer->doShuffle(); } } }); runAction(RepeatForever::create(Sequence::create(DelayTime::create(1.5), connectAction, shuffleAction, NULL))); return true; }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director Director* pDirector = gDirector; GLView* pGLView = pDirector->getOpenGLView(); if( NULL == pGLView ) { pGLView = GLView::create("game(v1.0.0.0)---test for inner"); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) pGLView->setFrameSize(1334, 750); #endif pDirector->setOpenGLView(pGLView); } // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); // resolution information Size size; size= pDirector->getWinSize(); log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height); size = pDirector->getWinSizeInPixels(); log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height); size = pDirector->getVisibleSize(); log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height); Point point = pDirector->getVisibleOrigin(); log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y); log("***IDONG: Director BS: getContentScaleFactor: scaleFactor=%f",pDirector->getContentScaleFactor()); auto framsize = pGLView->getFrameSize(); auto dwinsize = pDirector->getWinSize(); auto designsize = Size(SCREEN_WIDTH, SCREEN_HEIGHT); auto widthRate = framsize.width/designsize.width; auto heightRate = framsize.height/designsize.height; auto resolutionRate = 1.f; if(widthRate > heightRate) { pGLView->setDesignResolutionSize(designsize.width, designsize.height*heightRate/widthRate, ResolutionPolicy::NO_BORDER); resolutionRate = heightRate/widthRate; } else { pGLView->setDesignResolutionSize(designsize.width*widthRate/heightRate, designsize.height, ResolutionPolicy::NO_BORDER); resolutionRate = widthRate/heightRate; } //pGLView->setDesignResolutionSize(SCREEN_WIDTH, SCREEN_HEIGHT, ResolutionPolicy::FIXED_HEIGHT); log("***IDONG:/n"); log("***IDONG: Director AS: getContentScaleFactor: scaleFactor=%f",pDirector->getContentScaleFactor()); size= pDirector->getWinSize(); log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height); size = pDirector->getWinSizeInPixels(); log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height); size = pDirector->getVisibleSize(); log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height); point = pDirector->getVisibleOrigin(); log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y); // ‘ˆº”À—À˜¬∑æ∂ gFileUtils->addSearchPath("assets"); // …Ë÷√◊ ‘¥ƒø¬�? // ≥ı ºªØ◊ ‘¥ƒø¬�?dumpŒƒº˛…˙≥…ƒø¬�? string logfile = ""; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) gGameManager->SetResourceRoot("/mnt/sdcard/com.zm.mszb/"); gGameManager->CreateDirectory(gGameManager->GetResourceRoot()); gGameManager->CreateDirectory(gGameManager->GetLogPath()); logfile = gGameManager->GetLogPath()+"/log.txt"; gLog->Open(logfile.c_str()); #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) gGameManager->SetResourceRoot(""); gGameManager->CreateDirectory(gGameManager->GetResourceRoot()); gGameManager->CreateDirectory(gGameManager->GetLogPath()); logfile = gGameManager->GetLogPath()+"/log.txt"; gLog->Open(logfile.c_str()); #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) gGameManager->SetResourceRoot(gFileUtils->getWritablePath()); gGameManager->CreateDirectory(gGameManager->GetResourceRoot()); gGameManager->CreateDirectory(gGameManager->GetLogPath()); logfile = gGameManager->GetLogPath()+"/log.txt"; gLog->Open(logfile.c_str()); #endif gGameManager->LogMachineInfo(); // ø™ º∏¸–�? gGameManager->Start(); return true; }
Scene* BattleArena::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Director* dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Scene* scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Node* nodeItems = Node::create(); nodeItems->setName("nodeItems"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Node* menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; MenuItemFont* menuItem3 = MenuItemFont::create("Summon Pikachu"); menuItem3->setFontNameObj("fonts/Marker Felt.ttf"); menuItem3->setFontSizeObj(32); menuItem3->setName("summon_pikachu"); menuItem3->setVisible(true); menuItem3->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2).x, (Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height).y - (index) * 40)); menuItem3->setCallback([&](cocos2d::Ref *sender) { Director* dirs = Director::getInstance(); Scene* runningScene = dirs->getRunningScene(); SpriteFrameCache* spritecache = SpriteFrameCache::getInstance(); spritecache->addSpriteFramesWithFile("learning/PIKACHU.plist"); Sprite* newSprite = Sprite::createWithSpriteFrameName("0.gif"); int xRandom = random(0, int(dirs->getVisibleSize().width)); int yRandom = random(246, 266); newSprite->setPosition(xRandom, yRandom); newSprite->setName("sprite3"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); runningScene->addChild(newSprite,1); Vector<SpriteFrame*> animFrames; int frames = 112; animFrames.reserve(frames); char gif[8] = {0}; for(int index_frames = 0 ; index_frames < frames ; index_frames++){ sprintf(gif, "%d.gif", index_frames); animFrames.pushBack(spritecache->getSpriteFrameByName(gif)); } // create the animation out of the frames Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.05f); Animate* animate = Animate::create(animation); // run it and repeat it forever newSprite->runAction(RepeatForever::create(animate)); }); Menu* menu = Menu::create(menuItem3, NULL); menu->setName("menu"); menuNode->addChild(menu, 1); menu->setPosition(Vec2::ZERO); scene->addChild(menuNode, 2); // return the scene return scene; }
bool GameLayer::init(){ if(!LayerColor::initWithColor(Color4B(80, 220, 200, 200))) { return false; } setTargetScore(0); setCurrentScore(0); initSound(); initIconTexture(); Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); // GLView* glView = director->getOpenGLView(); // const Size& designResolution = glView->getDesignResolutionSize(); setGame(Game::create(m_gameMode)); setUiLayer(createLayer(visibleSize, Color4B(), Vec2(visibleSize.width/2, visibleSize.height/2))); setupGui(); Size topPanelSize = m_uiTopPanel->getContentSize(); Size bottomPanelSize = m_uiBottomPanel->getContentSize(); float verticalSize = (visibleSize.height - topPanelSize.height - bottomPanelSize.height - MARGIN_TOP - MARGIN_BOTTOM)/m_game->getRowCount(); float horizontalSize = (visibleSize.width - MARGIN_LEFT - MARGIN_RIGHT)/m_game->getColumnCount(); float tileSize = min(verticalSize, horizontalSize); setTileWidth(tileSize); setTileHeight(tileSize); Size mapSize(tileSize*m_game->getColumnCount(), tileSize*m_game->getRowCount()); MARGIN_TOP = (visibleSize.height-mapSize.height - topPanelSize.height - bottomPanelSize.height)/2; MARGIN_BOTTOM = MARGIN_TOP; MARGIN_LEFT = (visibleSize.width-mapSize.width)/2; MARGIN_RIGHT = MARGIN_LEFT; Vec2 mapOffset(MARGIN_LEFT, MARGIN_BOTTOM); ignoreAnchorPointForPosition(false); setContentSize(visibleSize); setAnchorPoint(Vec2()); setPosition(visibleOrigin); setBackgroundLayer(createLayer(mapSize, Color4B(), Vec2(mapSize.width/2+MARGIN_LEFT, mapSize.height/2+MARGIN_BOTTOM+bottomPanelSize.height))); setTileLayer(createLayer(mapSize, Color4B(), Vec2(mapSize.width/2 + MARGIN_LEFT, mapSize.height/2+MARGIN_BOTTOM+bottomPanelSize.height))); addChild(m_backgroundLayer); addChild(m_tileLayer); addChild(m_uiLayer); setGameMapSize(mapSize); setGameMapOffset(mapOffset); registerEventHandlers(); CallFunc* updateScore = CallFunc::create([&](){ int diff = m_targetScore - m_currentScore; if (diff != 0) { if (diff <= 1) { m_currentScore = m_targetScore; } else { m_currentScore += diff/2; } } Text* label = dynamic_cast<Text*>(m_uiTopPanel->getChildByName("points")); label->setString(StringUtils::format("%d", m_currentScore)); }); runAction(RepeatForever::create(Sequence::create(updateScore, NULL))); // CallFunc* lockThemAll = CallFunc::create([&](){ // auto path = m_game->getRandomPath(); // if (path) { // lockPath(path); // } // }); // runAction(lockThemAll); reset(); return true; }
void GameLayer::setupGui() { Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); Widget* gameUiLayer = GUIReader::getInstance()->widgetFromJsonFile("ui/connect/game_ui.json"); gameUiLayer->setContentSize(Size()); m_uiTopPanel = gameUiLayer->getChildByName("top"); m_uiTopPanel->setPosition(0, visibleSize.height-m_uiTopPanel->getContentSize().height - 5); m_uiComboPanel = m_uiTopPanel->getChildByName("combo"); m_uiComboCount = dynamic_cast<Text*>(m_uiComboPanel->getChildByName("count")); m_uiComboProgress = dynamic_cast<LoadingBar*>(m_uiComboPanel->getChildByName("progress")); m_uiComboPanel->setVisible(false); m_uiEmitterPanel = m_uiComboPanel->getChildByName("emitter"); auto particle = ParticleSystemQuad::create("particles/sun.plist"); particle->setStartColor(Color4F(1,0,0,1)); particle->setBlendAdditive(false); auto& size = m_uiComboPanel->getContentSize(); particle->setPosition(Vec2(size.width/2, size.height/2)); m_uiEmitterPanel->addChild(particle); m_uiTimerPanel = m_uiTopPanel->getChildByName("timer"); // static void shake(Node* node, float time=0.1f, float angle=25, float deltaAngle=5, int count=4) { m_uiTimerText = dynamic_cast<Text*>(m_uiTimerPanel->getChildByName("time")); m_uiTimerText->setString(StringUtils::format("%d", (int)m_timeLeft)); if (m_gameMode != CLASSIC) { m_uiTimerPanel->setVisible(false); } // GameUiClock* clock = GameUiClock::create(); // clock->setCurrent(0); // clock->setTotal(1); // clock->setTag(TAG_CLOCK); // m_uiTopPanel->addChild(clock); m_uiBottomPanel = gameUiLayer->getChildByName("bottom"); // Button* backButton = dynamic_cast<Button*>(m_uiTopPanel->getChildByName("back")); // backButton->addTouchEventListener([&](Ref* ref,Widget::TouchEventType event){ // switch (event) { // case cocos2d::ui::Widget::TouchEventType::ENDED: // { // playSound("click1"); // // auto layer = TwoButtonWithTitleLayer::create("pause_menu_title", ButtonConfig::create("pause_menu_back_to_game", [&](){ // scheduleUpdate(); // }) , ButtonConfig::create("pause_menu_back_to_main_menu", [&](){ // auto director = Director::getInstance(); // auto scene = MainMenuLayer::createScene(); // director->replaceScene(scene); // })); // // addChild(layer); // // unscheduleUpdate(); // } // break; // default: // break; // } // }); Util::registerButton(m_uiTopPanel, "back", [&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { // playSound("click1"); auto layer = TwoButtonWithTitleLayer::create("pause_menu_title", ButtonConfig::create("pause_menu_back_to_game", [&](){ scheduleUpdate(); }) , ButtonConfig::create("pause_menu_back_to_main_menu", [&](){ auto director = Director::getInstance(); auto scene = MainMenuLayer::createScene(); director->replaceScene(scene); })); addChild(layer); unscheduleUpdate(); } break; default: break; } }, ""); Button* settingButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("setting")); settingButton->addTouchEventListener([&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { // playSound("click1"); // CallFunc* connectAction = CallFunc::create([&](){ // auto children = m_tileLayer->getChildren(); // if (getAcceptInput() && children.size() > 0) { // int index = rand()%children.size(); // auto child = children.at(index); // GameTileView* gameTileView = dynamic_cast<GameTileView*>(child); // doSelectGameTileView(gameTileView); // } // // }); // runAction(RepeatForever::create(Sequence::create(connectAction, NULL))); } break; default: break; } }); settingButton->setVisible(false); Button* soundOnButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("soundOn")); Button* soundOffButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("soundOff")); soundOnButton->addTouchEventListener([=](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { // playSound("click1"); // reset(); setSoundOn(true); soundOnButton->setVisible(false); soundOffButton->setVisible(true); } break; default: break; } }); soundOnButton->setVisible(!m_soundOn); soundOffButton->addTouchEventListener([=](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: { // playSound("click1"); setSoundOn(false); soundOnButton->setVisible(true); soundOffButton->setVisible(false); } break; default: break; } }); soundOffButton->setVisible(m_soundOn); soundOnButton->setVisible(false); soundOffButton->setVisible(false); Button* resetButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("reset")); resetButton->addTouchEventListener([&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: if (getAcceptInput()) { setAcceptInput(false); playSound("reset"); reset(); } break; default: break; } }); resetButton->setVisible(false); // Button* hintButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("hint")); // hintButton->addTouchEventListener([&](Ref* ref,Widget::TouchEventType event){ // switch (event) { // case cocos2d::ui::Widget::TouchEventType::ENDED: // if (getAcceptInput()) { // onHintClick(); // } // // break; // default: // break; // } // }); Button* hintButton = Util::registerButton(m_uiBottomPanel, "hint", [&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: if (getAcceptInput()) { onHintClick(); } break; default: break; } }, "", false); m_uiHintCountText = dynamic_cast<Text*>(hintButton->getChildByName("count")); m_uiHintCountText->setVisible(m_gameMode == CLASSIC); m_uiHintCountText->setString(StringUtils::format("%d", m_game->getHintCount())); // Button* shuffleButton = dynamic_cast<Button*>(m_uiBottomPanel->getChildByName("shuffle")); // shuffleButton->addTouchEventListener([&](Ref* ref,Widget::TouchEventType event){ // switch (event) { // case cocos2d::ui::Widget::TouchEventType::ENDED: // if (getAcceptInput()) { // onShuffleClick(); // } // // break; // default: // break; // } // }); Button* shuffleButton = Util::registerButton(m_uiBottomPanel, "shuffle", [&](Ref* ref,Widget::TouchEventType event){ switch (event) { case cocos2d::ui::Widget::TouchEventType::ENDED: if (getAcceptInput()) { onShuffleClick(); } break; default: break; } }, "", false); m_uiShuffleCountText = dynamic_cast<Text*>(shuffleButton->getChildByName("count")); m_uiShuffleCountText->setVisible(m_gameMode == CLASSIC); m_uiShuffleCountText->setString(StringUtils::format("%d", m_game->getShuffleCount())); m_uiLayer->addChild(gameUiLayer); }