GameScene.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // GameScene.cpp
  3. // RedCore2
  4. //
  5. // Created by Gabriel Capella on 31/05/17.
  6. //
  7. //
  8. #include "GameScene.h"
  9. #include "SimpleAudioEngine.h"
  10. #include "BlocksLayer.h"
  11. #include "Ball.h"
  12. #include "params.h"
  13. USING_NS_CC;
  14. Scene* GameScene::createScene(int level) {
  15. auto scene = Scene::createWithPhysics();
  16. Size visibleSize = Director::getInstance()->getVisibleSize();
  17. //choose whitch part need to draw, Joint, Shape, Contact, None or All
  18. // scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  19. GameScene* layer = GameScene::create();
  20. scene->addChild(layer);
  21. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  22. material.density = 1.0f;
  23. material.restitution = 1.0f;
  24. material.friction = 0.0f;
  25. // the edge of the screen
  26. auto body = PhysicsBody::createEdgeBox(visibleSize, material);
  27. auto edgeNode = Node::create();
  28. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  29. body->setDynamic(false);
  30. edgeNode->setPhysicsBody(body);
  31. scene->addChild(edgeNode);
  32. auto blocks = BlocksLayer::create();
  33. blocks->setLevel(level);
  34. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  35. layer->addChild(blocks, 20);
  36. layer->setLevel(level);
  37. return scene;
  38. }
  39. // on "init" you need to initialize your instance
  40. bool GameScene::init() {
  41. //////////////////////////////
  42. // 1. super init first
  43. if ( !Layer::init() ) {
  44. return false;
  45. }
  46. // https://www.freesound.org/people/schademans/sounds/13290/
  47. FileUtils::getInstance()->addSearchPath("res");
  48. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  49. audio->preloadEffect("pipe.wav");
  50. audio->preloadEffect("metal.wav");
  51. audio->preloadEffect("bomb.wav");
  52. audio->preloadEffect("win.wav");
  53. auto visibleSize = Director::getInstance()->getVisibleSize();
  54. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  55. // Adiona o fundo
  56. auto bg = LayerColor::create(COLOR_back);
  57. this->addChild(bg);
  58. auto ball = Ball::create();
  59. ball->setPosition(visibleSize.width / 2, RAQUETE_ALTURA+BALL_SIZE);
  60. ball->throwBall();
  61. this->addChild(ball, 21);
  62. auto raquete = DrawNode::create();
  63. float py = RAQUETE_HEIGHT/2.0;
  64. float pxl = - RAQUETE_WIDTH/2;
  65. float pxr = RAQUETE_WIDTH/2;
  66. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  67. raquete->setPositionX(visibleSize.width/2);
  68. raquete->setPositionY(RAQUETE_ALTURA);
  69. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  70. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  71. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  72. physicsBody->setGravityEnable(false);
  73. physicsBody->setDynamic(false);
  74. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  75. raquete->addComponent(physicsBody);
  76. this->addChild(raquete);
  77. auto listener1 = EventListenerTouchOneByOne::create();
  78. // trigger when you push down
  79. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  80. raquete->setPositionX(touch->getLocation().x);
  81. float px = touch->getLocation().x;
  82. if (px >= RAQUETE_WIDTH/2 && px < visibleSize.width - RAQUETE_WIDTH/2)
  83. raquete->setPositionX(touch->getLocation().x);
  84. return true; // if you are consuming it
  85. };
  86. // trigger when moving touch
  87. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  88. float px = touch->getLocation().x;
  89. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  90. raquete->setPositionX(touch->getLocation().x);
  91. };
  92. // Add listener
  93. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  94. // the edge of bottom
  95. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  96. auto edgeNode = Node::create();
  97. edgeNode->setPosition(Point(visibleSize.width/2, 0));
  98. body->setDynamic(false);
  99. edgeNode->setTag(BOTTOM_TAG);
  100. body->setContactTestBitmask(0xFFFFFFFF);
  101. edgeNode->setPhysicsBody(body);
  102. this->addChild(edgeNode);
  103. // Evento no contato das bolas
  104. auto contactListener = EventListenerPhysicsContact::create();
  105. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  106. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  107. UserDefault *userdata = UserDefault::getInstance();
  108. //int level = userdata->getIntegerForKey("level");
  109. userdata->setIntegerForKey("level", 0);
  110. userdata->flush();
  111. return true;
  112. }
  113. bool GameScene::onContactBegin(PhysicsContact& contact) {
  114. auto nodeA = contact.getShapeA()->getBody()->getNode();
  115. auto nodeB = contact.getShapeB()->getBody()->getNode();
  116. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
  117. //CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  118. // sempre B < A
  119. if (nodeB->getTag() == BLOCK_TAG) {
  120. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  121. audio->playEffect("pipe.wav");
  122. nodeB->removeFromParentAndCleanup(true);
  123. } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  124. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  125. audio->playEffect("metal.wav");
  126. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  127. ball_collision(nodeA);
  128. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  129. ball_core(nodeB, nodeA);
  130. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  131. ball_core(nodeA, nodeB);
  132. }
  133. }
  134. return true;
  135. }
  136. void GameScene::ball_collision (Node *ball) {
  137. auto visibleSize = Director::getInstance()->getVisibleSize();
  138. auto text = Label::createWithTTF("Game Over...", "fonts/Marker Felt.ttf", 40);
  139. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  140. this->addChild(text);
  141. this->level = -1;
  142. auto menu_item_start = MenuItemFont::create("Restart", CC_CALLBACK_1(GameScene::NextLevel, this));
  143. menu_item_start->setPosition(text->getPosition());
  144. menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
  145. auto *menu = Menu::create(menu_item_start, NULL);
  146. menu->setPosition(Point(0, 0));
  147. this->addChild(menu, 30);
  148. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  149. audio->playEffect("bomb.wav");
  150. ParticleSun* m_emitter = ParticleSun::create();
  151. m_emitter->setPosition(ball->getPosition());
  152. m_emitter->setDuration(1);
  153. this->getScene()->addChild(m_emitter);
  154. ball->removeFromParentAndCleanup(true);
  155. }
  156. void GameScene::ball_core (Node *core, Node *ball) {
  157. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  158. core->getPhysicsBody()->setEnabled(false);
  159. core->runAction(scaleBy);
  160. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  161. audio->playEffect("win.wav");
  162. auto callbackRotate = CallFunc::create([=](){
  163. auto visibleSize = Director::getInstance()->getVisibleSize();
  164. auto menu_item_start = MenuItemFont::create("Next Level", CC_CALLBACK_1(GameScene::NextLevel, this));
  165. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  166. auto *menu = Menu::create(menu_item_start, NULL);
  167. menu->setPosition(Point(0, 0));
  168. this->addChild(menu, 30);
  169. });
  170. // create a sequence with the actions and callbacks
  171. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  172. core->runAction(seq);
  173. ball->removeFromParentAndCleanup(true);
  174. }
  175. void GameScene::NextLevel(cocos2d::Ref *pSender) {
  176. auto scene = GameScene::createScene(this->level + 1);
  177. Director::getInstance()->replaceScene(scene);
  178. //Director::getInstance()->end();
  179. }
  180. void GameScene::setLevel(int level) {
  181. this->level = level;
  182. char level_text[256];
  183. sprintf(level_text,"Level %d", this->level);
  184. auto text = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 30);
  185. text->setAnchorPoint(Vec2());
  186. text->setPosition(10, 10);
  187. this->addChild(text);
  188. }