GameScene.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "GameScene/BlocksLayer.h"
  11. #include "GameScene/Ball.h"
  12. #include "GameScene/Paddle.h"
  13. #include "params.h"
  14. USING_NS_CC;
  15. Scene* GameScene::createScene(int level) {
  16. auto scene = Scene::createWithPhysics();
  17. Size visibleSize = Director::getInstance()->getVisibleSize();
  18. #if DEBUG
  19. scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  20. #endif
  21. GameScene* layer = GameScene::create();
  22. scene->addChild(layer);
  23. // the edge of the screen
  24. auto body = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  25. auto edgeNode = Node::create();
  26. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  27. body->setDynamic(false);
  28. edgeNode->setPhysicsBody(body);
  29. scene->addChild(edgeNode);
  30. // bottom edge
  31. auto bottom_body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  32. auto bottomNode = Node::create();
  33. bottomNode->setPosition(Point(visibleSize.width/2, 0));
  34. bottom_body->setDynamic(false);
  35. bottomNode->setTag(BOTTOM_TAG);
  36. bottom_body->setContactTestBitmask(0xFFFFFFFF);
  37. bottomNode->setPhysicsBody(bottom_body);
  38. layer->addChild(bottomNode);
  39. auto blocks = BlocksLayer::create();
  40. blocks->setLevel(level);
  41. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  42. layer->addChild(blocks, 20);
  43. layer->setLevel(level);
  44. // inicializa os callbacks dos power ups/downs
  45. auto delay_save_level = DelayTime::create(10.0);
  46. CallFunc *runCallback_save_level = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
  47. auto seq_save_level = Sequence::create(delay_save_level, runCallback_save_level, nullptr);
  48. CallFunc *runCallback_triple_balls = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBallsAppearance, layer));
  49. CallFunc *runCallback_paddle_ball = CallFunc::create(CC_CALLBACK_0(GameScene::paddleBallAppearance, layer));
  50. CallFunc *runCallback_superball = CallFunc::create(CC_CALLBACK_0(GameScene::superBallAppearance, layer));
  51. layer->runAction(seq_save_level);
  52. layer->runAction(runCallback_triple_balls);
  53. layer->runAction(runCallback_paddle_ball);
  54. layer->runAction(runCallback_superball);
  55. layer->addAndThrowBall();
  56. return scene;
  57. }
  58. // on "init" you need to initialize your instance
  59. bool GameScene::init() {
  60. //////////////////////////////
  61. // 1. super init first
  62. if ( !Layer::init() ) {
  63. return false;
  64. }
  65. over = false;
  66. last_touch = time(NULL);
  67. // https://www.freesound.org/people/schademans/sounds/13290/
  68. FileUtils::getInstance()->addSearchPath("res");
  69. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  70. audio->preloadEffect("pipe.wav");
  71. audio->preloadEffect("metal.wav");
  72. audio->preloadEffect("bomb.wav");
  73. audio->preloadEffect("win.wav");
  74. auto visibleSize = Director::getInstance()->getVisibleSize();
  75. width = visibleSize.width;
  76. height = visibleSize.height;
  77. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  78. // Adiona o fundo
  79. auto bg = LayerColor::create(COLOR_back);
  80. addChild(bg);
  81. paddle = Paddle::create();
  82. paddle->setPositionX(width/2);
  83. paddle->listen(width);
  84. addChild(paddle);
  85. // Registra evento de contato de objetos
  86. auto contactListener = EventListenerPhysicsContact::create();
  87. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  88. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  89. balls = Node::create();
  90. addChild(balls, 21);
  91. return true;
  92. }
  93. void GameScene::addAndThrowBall() {
  94. Ball* ball = Ball::create();
  95. ball->setPosition(paddle->getPositionX(), PADDLE_ALTURA+BALL_SIZE);
  96. ball->throwBall();
  97. balls->addChild(ball, 21);
  98. }
  99. bool GameScene::onContactBegin(PhysicsContact& contact) {
  100. auto nodeA = contact.getShapeA()->getBody()->getNode();
  101. auto nodeB = contact.getShapeB()->getBody()->getNode();
  102. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
  103. if (nodeB->getTag() > nodeA->getTag()) {
  104. auto tmp = nodeB;
  105. nodeB = nodeA;
  106. nodeA = tmp;
  107. }
  108. if (nodeA->getTag() == BALL_TAG) {
  109. last_touch = time(NULL);
  110. }
  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. } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  117. if (nodeA->getTag() == BALL_TAG) {
  118. Ball* ball = (Ball*) nodeA;
  119. if (ball->superball) {
  120. nodeB->removeFromParentAndCleanup(true);
  121. ball->resetNormalball();
  122. }
  123. }
  124. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  125. audio->playEffect("metal.wav");
  126. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  127. caseBallCollision(nodeA);
  128. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  129. caseBallCore(nodeB, nodeA);
  130. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  131. caseBallCore(nodeA, nodeB);
  132. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == RACKET_TAG) {
  133. caseSaveLevel(nodeB);
  134. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == RACKET_TAG) {
  135. caseTripleBalls(nodeB);
  136. } else if (nodeB->getTag() == SUPERBALL_TAG && nodeA->getTag() == RACKET_TAG) {
  137. caseSuperBall(nodeB);
  138. } else if (nodeB->getTag() == RACKET_BALL_TAG && nodeA->getTag() == RACKET_TAG) {
  139. caseRaqueteBall(nodeB);
  140. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == BOTTOM_TAG) {
  141. nodeB->removeFromParentAndCleanup(true);
  142. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == BOTTOM_TAG) {
  143. nodeB->removeFromParentAndCleanup(true);
  144. } else if (nodeB->getTag() == RACKET_BALL_TAG && nodeA->getTag() == BOTTOM_TAG) {
  145. nodeB->removeFromParentAndCleanup(true);
  146. } else if (nodeB->getTag() == SUPERBALL_TAG && nodeA->getTag() == BOTTOM_TAG) {
  147. nodeB->removeFromParentAndCleanup(true);
  148. }
  149. }
  150. return true;
  151. }
  152. void GameScene::caseBallCollision (Node *ball) {
  153. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  154. audio->playEffect("bomb.wav");
  155. ParticleSun* m_emitter = ParticleSun::create();
  156. m_emitter->setPosition(ball->getPosition());
  157. m_emitter->setDuration(1);
  158. addChild(m_emitter);
  159. ball->removeFromParentAndCleanup(true);
  160. if (balls->getChildrenCount() == 0) {
  161. over = true;
  162. auto text = Label::createWithTTF(MSG_OVER, FONT, 40);
  163. text->setPosition(width/2, height/2);
  164. addChild(text);
  165. auto menu_item_start = MenuItemFont::create(MSG_RESTART, CC_CALLBACK_1(GameScene::NextLevel, this));
  166. menu_item_start->setFontNameObj(FONT);
  167. menu_item_start->setPosition(text->getPosition());
  168. menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
  169. auto *menu = Menu::create(menu_item_start, NULL);
  170. menu->setPosition(Point(0, 0));
  171. addChild(menu, 30);
  172. balls->removeFromParentAndCleanup(true);
  173. }
  174. }
  175. void GameScene::caseBallCore (Node *core, Node *ball) {
  176. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  177. core->getPhysicsBody()->setEnabled(false);
  178. core->runAction(scaleBy);
  179. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  180. audio->playEffect("win.wav");
  181. auto callbackRotate = CallFunc::create([=](){
  182. level = level + 1;
  183. auto menu_item_start = MenuItemFont::create(MSG_NEXT_LEVEL, CC_CALLBACK_1(GameScene::NextLevel, this));
  184. menu_item_start->setPosition(Point(width / 2, (height / 2)));
  185. auto *menu = Menu::create(menu_item_start, NULL);
  186. menu->setPosition(Point(0, 0));
  187. addChild(menu, 30);
  188. });
  189. // create a sequence with the actions and callbacks
  190. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  191. core->runAction(seq);
  192. ball->removeFromParentAndCleanup(true);
  193. }
  194. void GameScene::caseSaveLevel(Node *powerup_ball) {
  195. UserDefault *userdata = UserDefault::getInstance();
  196. userdata->setIntegerForKey("level", level);
  197. userdata->setDoubleForKey("time", (double) time(NULL));
  198. userdata->flush();
  199. alert(MSG_LEVEL_SAVED);
  200. powerup_ball->removeFromParentAndCleanup(true);
  201. }
  202. void GameScene::NextLevel(Ref *pSender) {
  203. auto scene = GameScene::createScene(level);
  204. Director::getInstance()->replaceScene(scene);
  205. }
  206. void GameScene::setLevel(int level) {
  207. this->level = level;
  208. char level_text[256];
  209. sprintf(level_text,"Level %d", level);
  210. auto text = Label::createWithTTF(level_text, FONT, 30);
  211. text->setAnchorPoint(Vec2());
  212. text->setPosition(10, 10);
  213. addChild(text);
  214. }
  215. // Power-up: salva o nível atual
  216. void GameScene::saveLevel() {
  217. if (over) return;
  218. if (rand_0_1() < DROP_LEVEL_SAVE){
  219. auto ball_draw = DrawNode::create();
  220. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_green));
  221. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  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(width * rand_0_1(), height);
  231. addChild(ball_draw, 20);
  232. }
  233. auto delay = DelayTime::create(1.0);
  234. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, this));
  235. auto seq = Sequence::create(delay, runCallback, nullptr);
  236. runAction(seq);
  237. }
  238. // Power-up: deixa 3 bolas na tela ao invés de 1
  239. void GameScene::tripleBallsAppearance() {
  240. if (over) return;
  241. double delta = (time(NULL)-last_touch);
  242. if (rand_0_1() < delta*DROP_TRIPLE_BALL){
  243. last_touch = time(NULL);
  244. auto ball_draw = DrawNode::create();
  245. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_pink));
  246. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  247. physicsBody->setGravityEnable(true);
  248. physicsBody->setVelocity(Vec2(0,0));
  249. physicsBody->setLinearDamping(0.0);
  250. physicsBody->setMass(1.0f);
  251. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  252. physicsBody->setGroup(-1);
  253. ball_draw->addComponent(physicsBody);
  254. ball_draw->setTag(THREE_BALLS_TAG);
  255. ball_draw->setPosition(width * rand_0_1(), height);
  256. addChild(ball_draw, 20);
  257. }
  258. auto delay = DelayTime::create(1.0);
  259. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBallsAppearance, this));
  260. auto seq = Sequence::create(delay, runCallback, nullptr);
  261. runAction(seq);
  262. }
  263. void GameScene::caseTripleBalls(Node *powerup_ball) {
  264. alert(MSG_TRIPE_BALLS);
  265. while (!over && balls->getChildrenCount() < 3)
  266. addAndThrowBall();
  267. powerup_ball->removeFromParentAndCleanup(true);
  268. }
  269. void GameScene::paddleBallAppearance() {
  270. if (over) return;
  271. if (rand_0_1() < DROP_RACKET_BALL && over == false){
  272. auto ball_draw = DrawNode::create();
  273. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_blue));
  274. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  275. physicsBody->setGravityEnable(true);
  276. physicsBody->setVelocity(Vec2(0,0));
  277. physicsBody->setLinearDamping(0.0);
  278. physicsBody->setMass(1.0f);
  279. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  280. physicsBody->setGroup(-1);
  281. ball_draw->addComponent(physicsBody);
  282. ball_draw->setTag(RACKET_BALL_TAG);
  283. ball_draw->setPosition(width * rand_0_1(), height);
  284. addChild(ball_draw, 20);
  285. }
  286. auto delay = DelayTime::create(1.0);
  287. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::paddleBallAppearance, this));
  288. auto seq = Sequence::create(delay, runCallback, nullptr);
  289. runAction(seq);
  290. }
  291. /* Power-up/down: 50% de chance de dobrar o tamanho da paddle,
  292. e 50% de chance de diminuir seu tamanho pela metade.
  293. Para não ficar muito fácil/difícil, impusemos tamanho mínimo/máximo da paddle.
  294. */
  295. void GameScene::caseRaqueteBall(Node *powerup_ball) {
  296. if (!over) {
  297. if (rand_0_1() < 0.5) {
  298. alert(MSG_DOUBLE_PADDLE);
  299. paddle->doubleSize();
  300. } else {
  301. alert(MSG_HALF_PADDLE);
  302. paddle->halfSize();
  303. }
  304. }
  305. powerup_ball->removeFromParentAndCleanup(true);
  306. }
  307. void GameScene::alert(std::string text) {
  308. if (!over) {
  309. auto display_label = Label::createWithTTF(text, FONT, 40);
  310. display_label->setPosition(width/2, height/2);
  311. addChild(display_label);
  312. display_label->runAction(FadeOut::create(3));
  313. }
  314. }
  315. // Power-up: deixa 3 bolas na tela ao invés de 1
  316. void GameScene::superBallAppearance() {
  317. if (over) return;
  318. if (rand_0_1() < DROP_SUPER_BALL){
  319. last_touch = time(NULL);
  320. auto ball_draw = DrawNode::create();
  321. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_orange));
  322. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  323. physicsBody->setGravityEnable(true);
  324. physicsBody->setVelocity(Vec2(0,0));
  325. physicsBody->setLinearDamping(0.0);
  326. physicsBody->setMass(1.0f);
  327. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  328. physicsBody->setGroup(-1);
  329. ball_draw->addComponent(physicsBody);
  330. ball_draw->setTag(SUPERBALL_TAG);
  331. ball_draw->setPosition(width * rand_0_1(), height);
  332. addChild(ball_draw, 20);
  333. }
  334. auto delay = DelayTime::create(1.0);
  335. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::superBallAppearance, this));
  336. auto seq = Sequence::create(delay, runCallback, nullptr);
  337. runAction(seq);
  338. }
  339. void GameScene::caseSuperBall(Node *powerup_ball) {
  340. if (over) return;
  341. alert(MSG_SUPER_BALL);
  342. Vector<Node *> balls_vector = balls->getChildren();
  343. Ball* ball = (Ball*) balls_vector.at(0);
  344. ball->setSuperball();
  345. powerup_ball->removeFromParentAndCleanup(true);
  346. }