GameScene.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_save_level = DelayTime::create(10.0);
  38. CallFunc *runCallback_save_level = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
  39. auto seq_save_level = Sequence::create(delay_save_level, runCallback_save_level, nullptr);
  40. auto delay_triple_balls = DelayTime::create(10.0);
  41. CallFunc *runCallback_triple_balls = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBalls, layer));
  42. auto seq_triple_balls = Sequence::create(delay_triple_balls, runCallback_triple_balls, nullptr);
  43. layer->runAction(seq_save_level);
  44. layer->runAction(seq_triple_balls);
  45. return scene;
  46. }
  47. // on "init" you need to initialize your instance
  48. bool GameScene::init() {
  49. //////////////////////////////
  50. // 1. super init first
  51. if ( !Layer::init() ) {
  52. return false;
  53. }
  54. this->over = false;
  55. // https://www.freesound.org/people/schademans/sounds/13290/
  56. FileUtils::getInstance()->addSearchPath("res");
  57. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  58. audio->preloadEffect("pipe.wav");
  59. audio->preloadEffect("metal.wav");
  60. audio->preloadEffect("bomb.wav");
  61. audio->preloadEffect("win.wav");
  62. auto visibleSize = Director::getInstance()->getVisibleSize();
  63. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  64. // Adiona o fundo
  65. auto bg = LayerColor::create(COLOR_back);
  66. this->addChild(bg);
  67. auto ball = Ball::create();
  68. ball->setPosition(visibleSize.width / 2, RAQUETE_ALTURA+BALL_SIZE);
  69. ball->throwBall();
  70. this->addChild(ball, 21);
  71. UserDefault *userdata = UserDefault::getInstance();
  72. userdata->setIntegerForKey("ball_count", 1);
  73. userdata->flush();
  74. auto raquete = DrawNode::create();
  75. float py = RAQUETE_HEIGHT/2.0;
  76. float pxl = - RAQUETE_WIDTH/2;
  77. float pxr = RAQUETE_WIDTH/2;
  78. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  79. raquete->setPositionX(visibleSize.width/2);
  80. raquete->setPositionY(RAQUETE_ALTURA);
  81. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  82. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  83. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  84. physicsBody->setGravityEnable(false);
  85. physicsBody->setDynamic(false);
  86. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  87. raquete->setTag(RACKET_TAG);
  88. raquete->addComponent(physicsBody);
  89. this->addChild(raquete);
  90. auto listener1 = EventListenerTouchOneByOne::create();
  91. // trigger when you push down
  92. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  93. raquete->setPositionX(touch->getLocation().x);
  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. return true; // if you are consuming it
  98. };
  99. // trigger when moving touch
  100. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  101. float px = touch->getLocation().x;
  102. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  103. raquete->setPositionX(touch->getLocation().x);
  104. };
  105. // Add listener
  106. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  107. // the edge of bottom
  108. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  109. auto edgeNode = Node::create();
  110. edgeNode->setPosition(Point(visibleSize.width/2, 0));
  111. body->setDynamic(false);
  112. edgeNode->setTag(BOTTOM_TAG);
  113. body->setContactTestBitmask(0xFFFFFFFF);
  114. edgeNode->setPhysicsBody(body);
  115. this->addChild(edgeNode);
  116. // Evento no contato das bolas
  117. auto contactListener = EventListenerPhysicsContact::create();
  118. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  119. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  120. return true;
  121. }
  122. bool GameScene::onContactBegin(PhysicsContact& contact) {
  123. auto nodeA = contact.getShapeA()->getBody()->getNode();
  124. auto nodeB = contact.getShapeB()->getBody()->getNode();
  125. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag() && !this->over) {
  126. //CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  127. if (nodeB->getTag() > nodeA->getTag()) {
  128. auto tmp = nodeB;
  129. nodeB = nodeA;
  130. nodeA = tmp;
  131. }
  132. // sempre B < A
  133. if (nodeB->getTag() == BLOCK_TAG) {
  134. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  135. audio->playEffect("pipe.wav");
  136. nodeB->removeFromParentAndCleanup(true);
  137. } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  138. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  139. audio->playEffect("metal.wav");
  140. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  141. ball_collision(nodeA);
  142. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  143. ball_core(nodeB, nodeA);
  144. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  145. ball_core(nodeA, nodeB);
  146. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == RACKET_TAG) {
  147. CCLOG(">>> %d", this->level);
  148. Size visibleSize = Director::getInstance()->getVisibleSize();
  149. // Salva o nivel!
  150. UserDefault *userdata = UserDefault::getInstance();
  151. userdata->setIntegerForKey("level", this->level);
  152. userdata->setDoubleForKey("time", (double) time(NULL));
  153. userdata->flush();
  154. nodeB->removeFromParentAndCleanup(true);
  155. auto text = Label::createWithTTF("Level Saved!", "fonts/Marker Felt.ttf", 40);
  156. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  157. this->addChild(text);
  158. text->runAction(FadeOut::create(3));
  159. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == RACKET_TAG) {
  160. Size visibleSize = Director::getInstance()->getVisibleSize();
  161. // Salva na "database" o número de bolas na tela
  162. UserDefault *userdata = UserDefault::getInstance();
  163. int ball_count = userdata->getIntegerForKey("ball_count");
  164. userdata->setIntegerForKey("ball_count", ball_count * 3);
  165. userdata->flush();
  166. nodeB->removeFromParentAndCleanup(true);
  167. auto text = Label::createWithTTF("Triple Balls!", "fonts/Marker Felt.ttf", 40);
  168. text->setPosition(visibleSize.width/2, visibleSize.height/2 + 2);
  169. this->addChild(text);
  170. text->runAction(FadeOut::create(3));
  171. // cria as bolas e as adiciona no cenário
  172. for (int i = ball_count; i < (ball_count * 3); i++) {
  173. auto ball = Ball::create();
  174. ball->setPosition(nodeB->getPositionX(), RAQUETE_ALTURA+BALL_SIZE);
  175. ball->throwBall();
  176. this->addChild(ball, 21);
  177. }
  178. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == BOTTOM_TAG) {
  179. nodeB->removeFromParentAndCleanup(true);
  180. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == BOTTOM_TAG) {
  181. nodeB->removeFromParentAndCleanup(true);
  182. }
  183. }
  184. return true;
  185. }
  186. void GameScene::ball_collision (Node *ball) {
  187. UserDefault *userdata = UserDefault::getInstance();
  188. int ball_count = userdata->getIntegerForKey("ball_count");
  189. if (ball_count <= 1) {
  190. this->over = true;
  191. auto visibleSize = Director::getInstance()->getVisibleSize();
  192. auto text = Label::createWithTTF("Game Over...", "fonts/Marker Felt.ttf", 40);
  193. text->setPosition(visibleSize.width/2, visibleSize.height/2);
  194. this->addChild(text);
  195. UserDefault *userdata = UserDefault::getInstance();
  196. level = userdata->getIntegerForKey("level", 0);
  197. auto menu_item_start = MenuItemFont::create("Restart", CC_CALLBACK_1(GameScene::NextLevel, this));
  198. menu_item_start->setPosition(text->getPosition());
  199. menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
  200. auto *menu = Menu::create(menu_item_start, NULL);
  201. menu->setPosition(Point(0, 0));
  202. this->addChild(menu, 30);
  203. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  204. audio->playEffect("bomb.wav");
  205. ParticleSun* m_emitter = ParticleSun::create();
  206. m_emitter->setPosition(ball->getPosition());
  207. m_emitter->setDuration(1);
  208. this->getScene()->addChild(m_emitter);
  209. ball->removeFromParentAndCleanup(true);
  210. }
  211. else {
  212. // Diminui o contador de bolas do jogo
  213. userdata->setIntegerForKey("ball_count", (ball_count-1));
  214. userdata->flush();
  215. // Destrói a bola que tocou o fundo
  216. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  217. audio->playEffect("bomb.wav");
  218. ParticleSun* m_emitter = ParticleSun::create();
  219. m_emitter->setPosition(ball->getPosition());
  220. m_emitter->setDuration(1);
  221. this->getScene()->addChild(m_emitter);
  222. ball->removeFromParentAndCleanup(true);
  223. }
  224. }
  225. void GameScene::ball_core (Node *core, Node *ball) {
  226. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  227. core->getPhysicsBody()->setEnabled(false);
  228. core->runAction(scaleBy);
  229. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  230. audio->playEffect("win.wav");
  231. auto callbackRotate = CallFunc::create([=](){
  232. auto visibleSize = Director::getInstance()->getVisibleSize();
  233. auto menu_item_start = MenuItemFont::create("Next Level", CC_CALLBACK_1(GameScene::NextLevel, this));
  234. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
  235. auto *menu = Menu::create(menu_item_start, NULL);
  236. menu->setPosition(Point(0, 0));
  237. this->addChild(menu, 30);
  238. });
  239. // create a sequence with the actions and callbacks
  240. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  241. core->runAction(seq);
  242. ball->removeFromParentAndCleanup(true);
  243. }
  244. void GameScene::NextLevel(Ref *pSender) {
  245. auto scene = GameScene::createScene(this->level + 1);
  246. Director::getInstance()->replaceScene(scene);
  247. //Director::getInstance()->end();
  248. }
  249. void GameScene::setLevel(int level) {
  250. this->level = level;
  251. char level_text[256];
  252. sprintf(level_text,"Level %d", this->level);
  253. auto text = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 30);
  254. text->setAnchorPoint(Vec2());
  255. text->setPosition(10, 10);
  256. this->addChild(text);
  257. }
  258. // Power-up: salva o nível atual
  259. void GameScene::saveLevel() {
  260. if (rand_0_1() < DROP_LEVEL_SAVE && this->over == false){
  261. Size visibleSize = Director::getInstance()->getVisibleSize();
  262. auto ball_draw = DrawNode::create();
  263. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_green));
  264. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  265. material.density = 0.0f;
  266. material.restitution = 1.0f;
  267. material.friction = 0.0f; //set friction here
  268. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, material);
  269. physicsBody->setGravityEnable(true);
  270. physicsBody->setVelocity(Vec2(0,0));
  271. physicsBody->setLinearDamping(0.0);
  272. physicsBody->setMass(1.0f);
  273. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  274. physicsBody->setGroup(-1);
  275. ball_draw->addComponent(physicsBody);
  276. ball_draw->setTag(SAVE_TAG);
  277. ball_draw->setPosition(visibleSize.width * rand_0_1(), visibleSize.height);
  278. this->addChild(ball_draw);
  279. }
  280. auto delay = DelayTime::create(rand_0_1()*10.0+3.0);
  281. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, this));
  282. auto seq = Sequence::create(delay, runCallback, nullptr);
  283. //this->runAction(seq);
  284. }
  285. // Power-up: triplica o número de bolas na tela
  286. void GameScene::tripleBalls() {
  287. if (rand_0_1() < DROP_TRIPLE_BALL && this->over == false){
  288. Size visibleSize = Director::getInstance()->getVisibleSize();
  289. auto ball_draw = DrawNode::create();
  290. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_pink));
  291. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  292. material.density = 0.0f;
  293. material.restitution = 1.0f;
  294. material.friction = 0.0f;
  295. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, material);
  296. physicsBody->setGravityEnable(true);
  297. physicsBody->setVelocity(Vec2(0,0));
  298. physicsBody->setLinearDamping(0.0);
  299. physicsBody->setMass(1.0f);
  300. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  301. physicsBody->setGroup(-1);
  302. ball_draw->addComponent(physicsBody);
  303. ball_draw->setTag(THREE_BALLS_TAG);
  304. ball_draw->setPosition(visibleSize.width * rand_0_1(), visibleSize.height);
  305. this->addChild(ball_draw);
  306. }
  307. auto delay = DelayTime::create(rand_0_1()*50.0+3.0);
  308. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBalls, this));
  309. auto seq = Sequence::create(delay, runCallback, nullptr);
  310. this->runAction(seq);
  311. }