Guest User

MainLayer

a guest
Oct 21st, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. #include "MainScene.h"
  2. #include "GoalLayer.h"
  3. #include "ClockLayer.h"
  4. #include "StartScene.h"
  5. #include "ScrollLayer.h"
  6. #include "SimpleAudioEngine.h"
  7. #include "cocos2d.h"
  8. #include "util/SaveData.h"
  9. #include "util/WebView.h"
  10. #include "util/NativeLauncher.h"
  11. #include "util/LocalizedString.h"
  12.  
  13. using namespace CocosDenshion;
  14. using namespace cocos2d;
  15. using namespace std;
  16.  
  17. #define WH Director::getInstance()->getWinSize().height
  18. #define WW Director::getInstance()->getWinSize().width
  19. #define VW Director::getInstance()->getVisibleSize().width
  20. #define VH Director::getInstance()->getVisibleSize().height
  21. #define VX Director::getInstance()->getVisibleOrigin().x
  22. #define VY Director::getInstance()->getVisibleOrigin().y
  23.  
  24.  
  25. Scene* Main::createScene() {
  26. auto scene = Scene::create();
  27. auto layer = Main::create();
  28.  
  29. scene->addChild(layer);
  30. return scene;
  31. }
  32.  
  33.  
  34. bool Main::init() {
  35. if (!Layer::init()) {
  36. return false;
  37. }
  38.  
  39.  
  40. FIXRATE=VH/320;
  41. log("fix:%f",FIXRATE);
  42. CENTERPOINT=Point(VX+VW/2,VY+VH/2);
  43. selectedCharaNum=0;
  44. collectCount=0;
  45.  
  46. makeInterface();
  47.  
  48.  
  49. goalLayer=GoalLayer::create();
  50. goalLayer->setPosition(Point(VX,VY-40));
  51. this->addChild(goalLayer,42);
  52. goalLayer->setVisible(false);
  53.  
  54. clockLayer=ClockLayer::create();
  55. clockLayer->setPosition(Point(VX + 100,VY +17));
  56. this->addChild(clockLayer,41);
  57.  
  58. log("clocklayer = %f , %f", clockLayer->getPosition().x , clockLayer->getPosition().y);
  59.  
  60. scrollLayer=ScrollLayer::create();
  61. scrollLayer->setDelegete(this);
  62. scrollLayer->makeInterface();
  63. scrollLayer->setContentSize(Size(1250*FIXRATE,768*FIXRATE));
  64. scrollLayer->setAnchorPoint(Point::ZERO);
  65. scrollLayer->setPosition(Point(0,0));
  66.  
  67. scrollView = ScrollView::create(Size(VW,VH));
  68. scrollView->setViewSize(Size(VW,VH));
  69. scrollView->setContentSize(Size(VW,VH));
  70. scrollView->setPosition(Point(VX,VY));
  71. scrollView->setTag(501);
  72. scrollView->setDirection(ScrollView::Direction::BOTH);
  73. scrollView->setContainer(scrollLayer);
  74. scrollView->setContentOffset(Point(-300,-200),false);
  75. scrollView->setBounceable(false);
  76.  
  77.  
  78. this->addChild(scrollView,11);
  79.  
  80.  
  81. NativeLauncher::showAd(true);
  82. NativeLauncher::showCenterAd(false);
  83. auto listener = EventListenerTouchOneByOne::create();
  84. listener->onTouchBegan = CC_CALLBACK_2(Main::touchBegan, this);
  85. listener->onTouchEnded = CC_CALLBACK_2(Main::touchEnded, this);
  86. listener->onTouchMoved = CC_CALLBACK_2(Main::touchMoved, this);
  87. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(
  88. listener, this);
  89. SimpleAudioEngine::getInstance()->playBackgroundMusic("bgm/bgm1.mp3",false);
  90. log("fin:%d",SaveData::loadInt("fin"));
  91. if(SaveData::loadInt("fin")==1){
  92. auto delayAction = DelayTime::create(0.8f);
  93. auto callSelectorAction = CallFunc::create(this,
  94. callfunc_selector(Main::goal));
  95. this->runAction(Sequence::create(delayAction, callSelectorAction, nullptr));
  96. }
  97. return true;
  98. }
  99.  
  100.  
  101. ////////////////////////////////////////////////////////////////////
  102. // TOUCHES RELATED EVENT
  103.  
  104. void Main::isLockLayer(bool isLockTmp){
  105. isLock=isLockTmp;
  106. }
  107.  
  108. bool Main::touchBegan(Touch* touch, Event* event) {
  109. log("touchBegan Main");
  110. if(isLock){
  111. scrollView->setTouchEnabled(false);
  112. }
  113. return true;
  114. }
  115.  
  116. void Main::touchMoved(Touch *touch, Event *event) {
  117. log("touchMoved Main");
  118.  
  119. }
  120.  
  121. void Main::touchEnded(Touch *touch, Event *event) {
  122. log("touchEnded Main");
  123. scrollView->setTouchEnabled(true);
  124. }
  125.  
  126.  
  127. void Main::reset(Ref* pSender){
  128. SaveData::saveInt("fin", 0);
  129. SaveData::saveInt("curentScoreTime", 0);
  130. playSound("action/flip.mp3");
  131. SimpleAudioEngine::getInstance()->playBackgroundMusic("bgm/bgm1.mp3",false);
  132. scrollLayer->reset();
  133. lastNumText->setString("47");
  134. goalLayer->setVisible(false);
  135. goalLayer->clearAnimation();
  136. clearObject(200);
  137. clearObject(2000);
  138. clockLayer->clockNeedle->runAction(RotateTo::create(0.2f,0.0));
  139. clockLayer->restartNeedle();
  140.  
  141. }
  142.  
  143.  
  144. void Main::back(Ref* pSender){
  145. // SaveData::saveInt("back",1);
  146. SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  147. auto pScene = Start::createScene();
  148. auto transition = TransitionFade::create(0.5f, pScene);
  149. Director::getInstance()->replaceScene(transition);
  150. }
  151.  
  152.  
  153. void Main::makeInterface(){
  154. auto titleImg=Sprite::create("title.png");
  155. titleImg->setPosition(Point(VX+titleImg->getContentSize().width/2,VY+VH - (titleImg->getContentSize().height /2)));
  156. this->addChild(titleImg,41);
  157.  
  158.  
  159. auto lastNumBox=Sprite::create("after-number.png");
  160. lastNumBox->setPosition(Point(CENTERPOINT.x-15,VY+VH-2-lastNumBox->getContentSize().height/2));
  161. this->addChild(lastNumBox,41);
  162.  
  163. lastNumText=Label::createWithSystemFont("47","Arial Black",22*FIXRATE,Size::ZERO,TextHAlignment::CENTER,TextVAlignment::CENTER);
  164. lastNumText->setPosition(Point(CENTERPOINT.x-15,VY+VH-4-lastNumBox->getContentSize().height/2));
  165. lastNumText->setColor(Color3B::WHITE);
  166. this->addChild(lastNumText,41);
  167.  
  168. auto titleLabel=Label::createWithSystemFont("パズル","Doroid Sans",22*FIXRATE,Size::ZERO,TextHAlignment::CENTER,TextVAlignment::CENTER);
  169. titleLabel->setPosition(Point(CENTERPOINT.x-55,VY+VH-lastNumBox->getContentSize().height/2));
  170. titleLabel->setColor(Color3B::ORANGE);
  171. this->addChild(titleLabel,41);
  172.  
  173. auto resetBtn=MenuItemImage::create("icon-reset.png","icon-reset.png",CC_CALLBACK_1(Main::reset,this));
  174. resetBtn->setPosition(Point((VW-VX) - (resetBtn->getContentSize().width/2),VY+VH - (resetBtn->getContentSize().width/2)));
  175.  
  176. auto btnMenu=Menu::create(resetBtn,nullptr);
  177. btnMenu->setPosition(Point::ZERO);
  178. this->addChild(btnMenu,41,1);
  179.  
  180. auto backBtn=MenuItemImage::create("icon-home.png","icon-home.png",CC_CALLBACK_1(Main::back,this));
  181. backBtn->setPosition(Point(VX+10+backBtn->getContentSize().width/2,VY+10+backBtn->getContentSize().height/2));
  182. btnMenu->addChild(backBtn,41);
  183.  
  184.  
  185. }
  186.  
  187. void Main::touchBackGround(Ref* pSender) {
  188. log("touch back");
  189. }
  190.  
  191. void Main::clearObject(int num) {
  192. auto oldNum = this->getChildByTag(num);
  193. if (oldNum != nullptr) {
  194. oldNum->removeFromParentAndCleanup(true);
  195. }
  196. }
  197. void Main::showParticle(float dt){
  198. ParticleSystemQuad *particle;
  199. particle = ParticleSystemQuad::create("particle.plist");
  200. particle->resetSystem();
  201. particle->setPosition(this->getContentSize() / 2);
  202. particle->setTag(2000);
  203. this->addChild(particle, 40);
  204. }
  205. ////////////////////////////////////////////////////////////////////
  206. //サウンド
  207. void Main::prepareSound(){
  208. SimpleAudioEngine::getInstance()->preloadEffect("action/ending.mp3");
  209. SimpleAudioEngine::getInstance()->preloadEffect("action/flip.mp3");
  210. }
  211.  
  212. void Main::playSound(const char *name) {
  213. SimpleAudioEngine::getInstance()->playEffect(name,false);
  214. }
  215.  
  216. ///////////////////////////////////
  217. //DELEGATE
  218. void Main::updateScore(int count){
  219. log("count:%d",count);
  220. auto text=String::createWithFormat("%d",47-count);
  221. lastNumText->setString(text->getCString());
  222.  
  223. }
  224.  
  225. void Main::goal(){
  226. log("goal");
  227. SaveData::saveInt("fin",1);
  228. SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  229. clockLayer->stopNeedle();
  230.  
  231. int scoretime=clockLayer->scoreTime();
  232. if(SaveData::loadInt("scoreTime")==0 || SaveData::loadInt("scoreTime")>scoretime){
  233. SaveData::saveInt("scoreTime",scoretime);
  234. clockLayer->setHighScore(scoretime);
  235. }
  236.  
  237. auto mask = LayerColor::create(Color4B(40, 40, 40, 180),
  238. WW, WH);
  239. this->addChild(mask, 41,300);
  240. bgBtn=MenuItemImage::create("",
  241. "", CC_CALLBACK_1(Main::touchBackGround,this));
  242. bgBtn->setContentSize(Size(VW,VH));
  243. bgBtn->setPosition(Point(VX+VW/2,VY+VH/2));
  244. menu=Menu::create(bgBtn,nullptr);
  245. menu->setPosition(Point::ZERO);
  246. this->addChild(menu,41,200);
  247. playSound("action/ending.mp3");
  248. goalLayer->makeScore(scoretime);
  249. goalLayer->setVisible(true);
  250. goalLayer->animation();
  251.  
  252. this->scheduleOnce(schedule_selector(Main::showParticle), 1.0f);
  253. auto fileName=String::create("btn_close2.png");
  254. if(SaveData::loadInt("notJapanese")==1){
  255. fileName=String::create("btn_close2_en.png");
  256. }
  257.  
  258.  
  259. NativeLauncher::showCenterAd(true);
  260. goalCloseBtn=MenuItemImage::create(fileName->getCString(),fileName->getCString(), CC_CALLBACK_1(Main::removeGoal,this));
  261. goalCloseBtn->setPosition(CENTERPOINT.x, VY+50);
  262. // goalCloseBtn->setPosition(CENTERPOINT.x, VY+30);
  263. goalCloseBtn->setTag(301);
  264. auto goalCloseMenu=Menu::create(goalCloseBtn,nullptr);
  265. goalCloseMenu->setPosition(Point::ZERO);
  266. this->addChild(goalCloseMenu,42,301);
  267. }
  268.  
  269. void Main::removeGoal(Ref* pSender) {
  270. clearObject(200);
  271. clearObject(300);
  272. clearObject(301);
  273. goalLayer->setVisible(false);
  274. goalLayer->clearAnimation();
  275. clearObject(2000);
  276. NativeLauncher::showAd(true);
  277. NativeLauncher::showCenterAd(false);
  278. // NativeLauncher::showInterAd(true);
  279. }
Add Comment
Please, Sign In to add comment