/**************************************************************************** Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "HelloWorldScene.h" #include "game_food/GameConfigParser.h" #include "game_food/ResourcesConfig.h" #include "game_food/MiscUtils.h" #include "game_food/SoundsRepo.h" #include "game_food/ScalingUtils.h" #include "game_halloween/HSubGameSceneSpaceInvaders.h" #include "game_halloween/HGameConfigParser.h" #include "game_halloween/HResourcesConfig.h" #include "game_toy/ToyMainMenuScene.h" #include "game_toy/ToyResourcesConfig.h" #include "game_toy/ToySoundsRepo.h" #include "game_animal/AniSoundsRepo.h" #include "game_animal/AniResourcesConfig.h" #include "game_animal/AniPickLevelScene.h" USING_NS_CC; static cocos2d::Size designResolutionSize = cocos2d::Size(2732, 2048); static cocos2d::Size tinyResolutionSize = cocos2d::Size(1366, 1024); Scene* HelloWorld::createScene() { return HelloWorld::create(); } // Print useful error message instead of segfaulting when files are not there. static void problemLoading(const char* filename) { printf("Error while loading: %s\n", filename); printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n"); } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Scene::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png","CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); if (closeItem == nullptr || closeItem->getContentSize().width <= 0 || closeItem->getContentSize().height <= 0) { problemLoading("'CloseNormal.png' and 'CloseSelected.png'"); } else { float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2; float y = origin.y + closeItem->getContentSize().height/2; closeItem->setPosition(Vec2(x,y)); } auto lbl_game1 = Label::createWithTTF("food", "fonts/ComicSansMSBold.ttf", 48); auto lbl_game2 = Label::createWithTTF("halloween", "fonts/ComicSansMSBold.ttf", 48); auto lbl_game3 = Label::createWithTTF("toygamerepo", "fonts/ComicSansMSBold.ttf", 48); auto lbl_game4 = Label::createWithTTF("animal", "fonts/ComicSansMSBold.ttf", 48); auto lbl_game5 = Label::createWithTTF("exit ", "fonts/ComicSansMSBold.ttf", 48); auto btn_game1 = MenuItemLabel::create(lbl_game1, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); auto btn_game2 = MenuItemLabel::create(lbl_game2, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); auto btn_game3 = MenuItemLabel::create(lbl_game3, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); auto btn_game4 = MenuItemLabel::create(lbl_game4, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); auto btn_game5 = MenuItemLabel::create(lbl_game5, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); btn_game1->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5 + 400); btn_game2->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5 + 200); btn_game3->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5 - 0); btn_game4->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5 - 200); btn_game5->setPosition(visibleSize.width * 0.5, visibleSize.height * 0.5 - 400); btn_game1->setTag(101); btn_game2->setTag(102); btn_game3->setTag(103); btn_game4->setTag(104); btn_game5->setTag(105); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, btn_game1, btn_game2, btn_game3, btn_game4,btn_game5, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); CCLOG("HelloWorld init"); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { auto node = (Node*)pSender; int tag = node -> getTag(); if (tag == 101)//food { auto searchPaths = cocos2d::FileUtils::getInstance()->getSearchPaths(); searchPaths.push_back("game_food"); if(ScalingUtils::isSmallDevice()){ searchPaths.push_back("game_food/" + std::string(ResourcesConfig::RES_FOLDER_NAME_TINY)); searchPaths.push_back("game_food/" + std::string(ResourcesConfig::RES_FOLDER_NAME_XLARGE)); } else { searchPaths.push_back("game_food/" + std::string(ResourcesConfig::RES_FOLDER_NAME_XLARGE)); } searchPaths.push_back("game_food/common"); cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths); // run std::string sceneLayoutPath = "graphics/shoot_game/scene_layout.scl"; GameConfigParser parser("common/games/shoot_game/gconfig.gcf"); auto newScene = parser.createGameScene(45, sceneLayoutPath); Director::getInstance()->replaceScene(newScene); } if (tag == 102)//halloween { auto searchPaths = cocos2d::FileUtils::getInstance()->getSearchPaths(); std::string deviceSpecificFolderName; deviceSpecificFolderName = HResourcesConfig::RES_FOLDER_NAME_XLARGE;//TODO TEMP searchPaths.push_back("game_halloween/" + deviceSpecificFolderName); searchPaths.push_back("game_halloween/common"); cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths); std::string configFilePath = "game_halloween/common/gconfig.gcf"; std::string layoutFilePath = "game_halloween/xlarge/scene_layout.scl"; HGameConfigParser parser(configFilePath); auto gameScene = dynamic_cast(parser.createGameScene(layoutFilePath)); // run Director::getInstance()->replaceScene(gameScene); } if (tag == 103)//toygamerepo { auto searchPaths = cocos2d::FileUtils::getInstance()->getSearchPaths(); std::string deviceSpecificFolderName; deviceSpecificFolderName = ToyResourcesConfig::RES_FOLDER_NAME_XLARGE;//TODO TEMP searchPaths.push_back("game_toy"); searchPaths.push_back("game_toy/" + deviceSpecificFolderName); searchPaths.push_back("game_toy/common"); cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths); ToySoundsRepo::loadSoundsList(); srand(time(0)); // run auto _mainScene = ToyMainMenuScene::create("graphics/main_menu/scene_layout.scl"); Director::getInstance()->replaceScene(_mainScene); } if (tag == 104)//animal { auto searchPaths = cocos2d::FileUtils::getInstance()->getSearchPaths(); std::string deviceSpecificFolderName; deviceSpecificFolderName = ToyResourcesConfig::RES_FOLDER_NAME_XLARGE;//TODO TEMP searchPaths.push_back("game_animal"); searchPaths.push_back("game_animal/" + deviceSpecificFolderName); searchPaths.push_back("game_animal/common"); cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths); AniSoundsRepo::loadSoundsList(); srand(time(0)); auto _mainScene = AniPickLevelScene::create(""); Director::getInstance()->replaceScene(_mainScene); } if (tag == 105)//exit { Director::getInstance()->end(); } }