GameScene.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. auto delay = DelayTime::create(10.0);
  38. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
  39. auto seq = Sequence::create(delay, runCallback, nullptr);
  40. layer->runAction(seq);
  41. return scene;
  42. }
  43. // on "init" you need to initialize your instance
  44. bool GameScene::init() {
  45. //////////////////////////////
  46. // 1. super init first
  47. if ( !Layer::init() ) {
  48. return false;
  49. }
  50. this->over = false;
  51. // https://www.freesound.org/people/schademans/sounds/13290/
  52. FileUtils::getInstance()->addSearchPath("res");
  53. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  54. audio->preloadEffect("pipe.wav");
  55. audio->preloadEffect("metal.wav");
  56. audio->preloadEffect("bomb.wav");
  57. audio->preloadEffect("win.wav");
  58. auto visibleSize = Director::getInstance()->getVisibleSize();
  59. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  60. // Adiona o fundo
  61. auto bg = LayerColor::create(COLOR_back);
  62. this->addChild(bg);
  63. auto ball = Ball::create();
  64. ball->setPosition(visibleSize.width / 2, RAQUETE_ALTURA+BALL_SIZE);
  65. ball->throwBall();
  66. this->addChild(ball, 21);
  67. auto raquete = DrawNode::create();
  68. float py = RAQUETE_HEIGHT/2.0;
  69. float pxl = - RAQUETE_WIDTH/2;
  70. float pxr = RAQUETE_WIDTH/2;
  71. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  72. raquete->setPositionX(visibleSize.width/2);
  73. raquete->setPositionY(RAQUETE_ALTURA);
  74. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  75. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  76. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  77. physicsBody->setGravityEnable(false);
  78. physicsBody->setDynamic(false);
  79. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  80. raquete->setTag(RACKET_TAG);
  81. raquete->addComponent(physicsBody);
  82. this->addChild(raquete);
  83. auto listener1 = EventListenerTouchOneByOne::create();
  84. // trigger when you push down
  85. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  86. raquete->setPositionX(touch->getLocation().x);
  87. float px = touch->getLocation().x;
  88. if (px >= RAQUETE_WIDTH/2 && px < visibleSize.width - RAQUETE_WIDTH/2)
  89. raquete->setPositionX(touch->getLocation().x);
  90. return true; // if you are consuming it
  91. };
  92. // trigger when moving touch
  93. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  94. float px = touch->getLocation().x;
  95. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  96. raquete->setPositionX(touch->getLocation().x);
  97. };
  98. // Add listener
  99. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  100. // the edge of bottom
  101. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  102. auto edgeNode = Node::create();
  103. edgeNode->setPosition(Point(visibleSize.width/2, 0));
  104. body->setDynamic(false);
  105. edgeNode->setTag(BOTTOM_TAG);
  106. body->setContactTestBitmask(0xFFFFFFFF);
  107. edgeNode->setPhysicsBody(body);
  108. this->addChild(edgeNode);
  109. // Evento no contato das bolas
  110. auto contactListener = EventListenerPhysicsContact::create();
  111. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  112. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  113. return true;
  114. }
  115. bool GameScene::onContactBegin(PhysicsContact& contact) {
  116. auto nodeA = contact.getShapeA()->getBody()->getNode();
  117. auto nodeB = contact.getShapeB()->getBody()->getNode();
  118. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag() && !this->over) {
  119. //CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  120. if (nodeB->getTag() > nodeA->getTag()) {
  121. auto tmp = nodeB;
  122. nodeB = nodeA;
  123. nodeA = tmp;
  124. }
  125. // sempre B < A
  126. if (nodeB->getTag() == BLOCK_TAG) {
  127. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  128. audio->playEffect("pipe.wav");
  129. nodeB->removeFromParentAndCleanup(true);
  130. } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  131. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  132. audio->playEffect("metal.wav");
  133. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  134. ball_collision(nodeA);
  135. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  136. ball_core(nodeB, nodeA);
  137. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  138. ball_core(nodeA, nodeB);
  139. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == RACKET_TAG) {
  140. CCLOG(">>> %d", this->level);
  141. Size visibleSize = Director::getInstance()->getVisibleSize();
  142. // Salva o nivel!
  143. UserDefault *userdata = UserDefault::getInstance();
  144. userdata->setIntegerForKey("level", this->level);
  145. userdata->setDoubleForKey("time", (double) time(NULL));
  146. userdata->flush();
  147. nodeB->removeFromParentAndCleanup(true);
  148. auto text = Label::createWithTTF("Level Saved!", "fonts/Marker Felt.ttf", 40);
  149. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  150. this->addChild(text);
  151. text->runAction(FadeOut::create(3));
  152. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == BOTTOM_TAG) {
  153. nodeB->removeFromParentAndCleanup(true);
  154. }
  155. }
  156. return true;
  157. }
  158. void GameScene::ball_collision (Node *ball) {
  159. this->over = true;
  160. auto visibleSize = Director::getInstance()->getVisibleSize();
  161. auto text = Label::createWithTTF("Game Over...", "fonts/Marker Felt.ttf", 40);
  162. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  163. this->addChild(text);
  164. this->level = -1;
  165. auto menu_item_start = MenuItemFont::create("Restart", CC_CALLBACK_1(GameScene::NextLevel, this));
  166. menu_item_start->setPosition(text->getPosition());
  167. menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
  168. auto *menu = Menu::create(menu_item_start, NULL);
  169. menu->setPosition(Point(0, 0));
  170. this->addChild(menu, 30);
  171. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  172. audio->playEffect("bomb.wav");
  173. ParticleSun* m_emitter = ParticleSun::create();
  174. m_emitter->setPosition(ball->getPosition());
  175. m_emitter->setDuration(1);
  176. this->getScene()->addChild(m_emitter);
  177. ball->removeFromParentAndCleanup(true);
  178. }
  179. void GameScene::ball_core (Node *core, Node *ball) {
  180. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  181. core->getPhysicsBody()->setEnabled(false);
  182. core->runAction(scaleBy);
  183. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  184. audio->playEffect("win.wav");
  185. auto callbackRotate = CallFunc::create([=](){
  186. auto visibleSize = Director::getInstance()->getVisibleSize();
  187. auto menu_item_start = MenuItemFont::create("Next Level", CC_CALLBACK_1(GameScene::NextLevel, this));
  188. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  189. auto *menu = Menu::create(menu_item_start, NULL);
  190. menu->setPosition(Point(0, 0));
  191. this->addChild(menu, 30);
  192. });
  193. // create a sequence with the actions and callbacks
  194. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  195. core->runAction(seq);
  196. ball->removeFromParentAndCleanup(true);
  197. }
  198. void GameScene::NextLevel(Ref *pSender) {
  199. auto scene = GameScene::createScene(this->level + 1);
  200. Director::getInstance()->replaceScene(scene);
  201. //Director::getInstance()->end();
  202. }
  203. void GameScene::setLevel(int level) {
  204. this->level = level;
  205. char level_text[256];
  206. sprintf(level_text,"Level %d", this->level);
  207. auto text = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 30);
  208. text->setAnchorPoint(Vec2());
  209. text->setPosition(10, 10);
  210. this->addChild(text);
  211. }
  212. void GameScene::saveLevel() {
  213. if (rand_0_1() < DROP_LEVEL_SAVE && this->over == false){
  214. Size visibleSize = Director::getInstance()->getVisibleSize();
  215. auto ball_draw = DrawNode::create();
  216. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_green));
  217. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  218. material.density = 0.0f;
  219. material.restitution = 1.0f;
  220. material.friction = 0.0f; //set friction here
  221. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, material);
  222. physicsBody->setGravityEnable(true);
  223. physicsBody->setVelocity(Vec2(0,0));
  224. physicsBody->setLinearDamping(0.0);
  225. physicsBody->setMass(1.0f);
  226. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  227. physicsBody->setGroup(-1);
  228. ball_draw->addComponent(physicsBody);
  229. ball_draw->setTag(SAVE_TAG);
  230. ball_draw->setPosition(visibleSize.width * rand_0_1(), visibleSize.height);
  231. this->addChild(ball_draw);
  232. }
  233. auto delay = DelayTime::create(rand_0_1()*10.0+3.0);
  234. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, this));
  235. auto seq = Sequence::create(delay, runCallback, nullptr);
  236. this->runAction(seq);
  237. }