GameScene.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. auto visibleSize = Director::getInstance()->getVisibleSize();
  42. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  43. // Adiona o fundo
  44. auto bg = LayerColor::create(COLOR_back);
  45. this->addChild(bg);
  46. auto ball = Ball::create();
  47. ball->setPosition(visibleSize.width / 2, RAQUETE_ALTURA+BALL_SIZE);
  48. ball->throwBall();
  49. this->addChild(ball, 21);
  50. auto blocks = BlocksLayer::create();
  51. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  52. this->addChild(blocks, 20);
  53. auto raquete = DrawNode::create();
  54. float py = RAQUETE_HEIGHT/2.0;
  55. float pxl = - RAQUETE_WIDTH/2;
  56. float pxr = RAQUETE_WIDTH/2;
  57. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  58. raquete->setPositionX(visibleSize.width/2);
  59. raquete->setPositionY(RAQUETE_ALTURA);
  60. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  61. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  62. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  63. physicsBody->setGravityEnable(false);
  64. physicsBody->setDynamic(false);
  65. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  66. raquete->addComponent(physicsBody);
  67. this->addChild(raquete);
  68. auto listener1 = EventListenerTouchOneByOne::create();
  69. // trigger when you push down
  70. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  71. raquete->setPositionX(touch->getLocation().x);
  72. float px = touch->getLocation().x;
  73. if (px >= RAQUETE_WIDTH/2 && px < visibleSize.width - RAQUETE_WIDTH/2)
  74. raquete->setPositionX(touch->getLocation().x);
  75. return true; // if you are consuming it
  76. };
  77. // trigger when moving touch
  78. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  79. float px = touch->getLocation().x;
  80. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  81. raquete->setPositionX(touch->getLocation().x);
  82. };
  83. // Add listener
  84. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  85. // the edge of bottom
  86. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  87. auto edgeNode = Node::create();
  88. edgeNode->setPosition(Point(visibleSize.width/2, 0));
  89. body->setDynamic(false);
  90. edgeNode->setTag(BOTTOM_TAG);
  91. body->setContactTestBitmask(0xFFFFFFFF);
  92. edgeNode->setPhysicsBody(body);
  93. this->addChild(edgeNode);
  94. // Evento no contato das bolas
  95. auto contactListener = EventListenerPhysicsContact::create();
  96. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  97. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  98. return true;
  99. }
  100. bool GameScene::onContactBegin(PhysicsContact& contact) {
  101. auto nodeA = contact.getShapeA()->getBody()->getNode();
  102. auto nodeB = contact.getShapeB()->getBody()->getNode();
  103. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
  104. CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  105. if (nodeA->getTag() == BLOCK_TAG) {
  106. nodeA->removeFromParentAndCleanup(true);
  107. } else if (nodeB->getTag() == BLOCK_TAG) {
  108. nodeB->removeFromParentAndCleanup(true);
  109. } else if (nodeA->getTag() == BOTTOM_TAG && nodeB->getTag() == BALL_TAG) {
  110. ball_collision(nodeB);
  111. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  112. ball_collision(nodeA);
  113. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  114. ball_core(nodeB, nodeA);
  115. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  116. ball_core(nodeA, nodeB);
  117. }
  118. }
  119. //bodies can collide
  120. return true;
  121. }
  122. void GameScene::ball_collision (Node *ball) {
  123. ParticleSun* m_emitter = ParticleSun::create();
  124. m_emitter->setPosition(ball->getPosition());
  125. m_emitter->setDuration(1);
  126. this->getScene()->addChild(m_emitter);
  127. ball->removeFromParentAndCleanup(true);
  128. }
  129. void GameScene::ball_core (Node *core, Node *ball) {
  130. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  131. core->getPhysicsBody()->setEnabled(false);
  132. core->runAction(scaleBy);
  133. auto callbackRotate = CallFunc::create([=](){
  134. auto visibleSize = Director::getInstance()->getVisibleSize();
  135. auto menu_item_start = MenuItemFont::create("Next Level");
  136. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  137. auto *menu = Menu::create(menu_item_start, NULL);
  138. menu->setPosition(Point(0, 0));
  139. this->addChild(menu, 30);
  140. });
  141. // create a sequence with the actions and callbacks
  142. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  143. core->runAction(seq);
  144. ball->removeFromParentAndCleanup(true);
  145. }