GameScene.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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() {
  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. auto 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. return scene;
  33. }
  34. // on "init" you need to initialize your instance
  35. bool GameScene::init() {
  36. //////////////////////////////
  37. // 1. super init first
  38. if ( !Layer::init() ) {
  39. return false;
  40. }
  41. // https://www.freesound.org/people/schademans/sounds/13290/
  42. FileUtils::getInstance()->addSearchPath("res");
  43. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  44. audio->preloadEffect("pipe.wav");
  45. audio->preloadEffect("metal.wav");
  46. audio->preloadEffect("bomb.wav");
  47. auto visibleSize = Director::getInstance()->getVisibleSize();
  48. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  49. // Adiona o fundo
  50. auto bg = LayerColor::create(COLOR_back);
  51. this->addChild(bg);
  52. auto ball = Ball::create();
  53. ball->setPosition(visibleSize.width / 2, RAQUETE_ALTURA+BALL_SIZE);
  54. ball->throwBall();
  55. this->addChild(ball, 21);
  56. auto blocks = BlocksLayer::create();
  57. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  58. this->addChild(blocks, 20);
  59. auto raquete = DrawNode::create();
  60. float py = RAQUETE_HEIGHT/2.0;
  61. float pxl = - RAQUETE_WIDTH/2;
  62. float pxr = RAQUETE_WIDTH/2;
  63. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  64. raquete->setPositionX(visibleSize.width/2);
  65. raquete->setPositionY(RAQUETE_ALTURA);
  66. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  67. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  68. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  69. physicsBody->setGravityEnable(false);
  70. physicsBody->setDynamic(false);
  71. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  72. raquete->addComponent(physicsBody);
  73. this->addChild(raquete);
  74. auto listener1 = EventListenerTouchOneByOne::create();
  75. // trigger when you push down
  76. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  77. raquete->setPositionX(touch->getLocation().x);
  78. float px = touch->getLocation().x;
  79. if (px >= RAQUETE_WIDTH/2 && px < visibleSize.width - RAQUETE_WIDTH/2)
  80. raquete->setPositionX(touch->getLocation().x);
  81. return true; // if you are consuming it
  82. };
  83. // trigger when moving touch
  84. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  85. float px = touch->getLocation().x;
  86. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  87. raquete->setPositionX(touch->getLocation().x);
  88. };
  89. // Add listener
  90. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  91. // the edge of bottom
  92. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  93. auto edgeNode = Node::create();
  94. edgeNode->setPosition(Point(visibleSize.width/2, 0));
  95. body->setDynamic(false);
  96. edgeNode->setTag(BOTTOM_TAG);
  97. body->setContactTestBitmask(0xFFFFFFFF);
  98. edgeNode->setPhysicsBody(body);
  99. this->addChild(edgeNode);
  100. // Evento no contato das bolas
  101. auto contactListener = EventListenerPhysicsContact::create();
  102. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  103. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  104. return true;
  105. }
  106. bool GameScene::onContactBegin(PhysicsContact& contact) {
  107. auto nodeA = contact.getShapeA()->getBody()->getNode();
  108. auto nodeB = contact.getShapeB()->getBody()->getNode();
  109. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
  110. CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  111. // sempre B < A
  112. if (nodeB->getTag() == BLOCK_TAG) {
  113. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  114. audio->playEffect("pipe.wav");
  115. nodeB->removeFromParentAndCleanup(true);
  116. } if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  117. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  118. audio->playEffect("metal.wav");
  119. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  120. ball_collision(nodeA);
  121. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  122. ball_core(nodeB, nodeA);
  123. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  124. ball_core(nodeA, nodeB);
  125. }
  126. }
  127. return true;
  128. }
  129. void GameScene::ball_collision (Node *ball) {
  130. auto visibleSize = Director::getInstance()->getVisibleSize();
  131. auto text = Label::createWithTTF("Game Over...", "fonts/Marker Felt.ttf", 40);
  132. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  133. this->addChild(text);
  134. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  135. audio->playEffect("bomb.wav");
  136. ParticleSun* m_emitter = ParticleSun::create();
  137. m_emitter->setPosition(ball->getPosition());
  138. m_emitter->setDuration(1);
  139. this->getScene()->addChild(m_emitter);
  140. ball->removeFromParentAndCleanup(true);
  141. }
  142. void GameScene::ball_core (Node *core, Node *ball) {
  143. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  144. core->getPhysicsBody()->setEnabled(false);
  145. core->runAction(scaleBy);
  146. auto callbackRotate = CallFunc::create([=](){
  147. auto visibleSize = Director::getInstance()->getVisibleSize();
  148. auto menu_item_start = MenuItemFont::create("Next Level", CC_CALLBACK_1(GameScene::NextLevel, this));
  149. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  150. auto *menu = Menu::create(menu_item_start, NULL);
  151. menu->setPosition(Point(0, 0));
  152. this->addChild(menu, 30);
  153. });
  154. // create a sequence with the actions and callbacks
  155. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  156. core->runAction(seq);
  157. ball->removeFromParentAndCleanup(true);
  158. }
  159. void GameScene::NextLevel(cocos2d::Ref *pSender) {
  160. auto scene = GameScene::createScene();
  161. Director::getInstance()->replaceScene(scene);
  162. //Director::getInstance()->end();
  163. }