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