GameScene.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /********************************************************************
  2. * Nomes: Gabriel Capella Números USP: 8962078
  3. * João Herique Luciano 8535957
  4. *
  5. * Tarefa: RedCore - EP2 MAC0463
  6. * Arquivo: GameScene.cpp
  7. * Descrição: Implementação do cenário e do mecanismo de jogo
  8. ********************************************************************/
  9. #include "GameScene.h"
  10. #include "SimpleAudioEngine.h"
  11. #include "GameScene/BlocksLayer.h"
  12. #include "GameScene/Ball.h"
  13. #include "GameScene/Paddle.h"
  14. #include "params.h"
  15. USING_NS_CC;
  16. Scene* GameScene::createScene(int level) {
  17. auto scene = Scene::createWithPhysics();
  18. Size visibleSize = Director::getInstance()->getVisibleSize();
  19. #if DEBUG
  20. scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  21. #endif
  22. GameScene* layer = GameScene::create();
  23. scene->addChild(layer);
  24. // parede da tela
  25. auto body = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0.0f, 1.0f, 0.0f));
  26. auto edgeNode = Node::create();
  27. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  28. body->setDynamic(false);
  29. edgeNode->setPhysicsBody(body);
  30. scene->addChild(edgeNode);
  31. // parede de baixo
  32. auto bottom_body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  33. auto bottomNode = Node::create();
  34. bottomNode->setPosition(Point(visibleSize.width/2, 0));
  35. bottom_body->setDynamic(false);
  36. bottomNode->setTag(BOTTOM_TAG);
  37. bottom_body->setContactTestBitmask(0xFFFFFFFF);
  38. bottomNode->setPhysicsBody(bottom_body);
  39. layer->addChild(bottomNode);
  40. auto blocks = BlocksLayer::create();
  41. blocks->setLevel(level);
  42. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  43. layer->addChild(blocks, 20);
  44. layer->setLevel(level);
  45. // inicializa os callbacks dos power ups/downs
  46. auto delay_save_level = DelayTime::create(10.0);
  47. CallFunc *runCallback_save_level = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
  48. auto seq_save_level = Sequence::create(delay_save_level, runCallback_save_level, nullptr);
  49. CallFunc *runCallback_triple_balls = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBallsAppearance, layer));
  50. CallFunc *runCallback_paddle_ball = CallFunc::create(CC_CALLBACK_0(GameScene::paddleBallAppearance, layer));
  51. CallFunc *runCallback_superball = CallFunc::create(CC_CALLBACK_0(GameScene::superBallAppearance, layer));
  52. layer->runAction(seq_save_level);
  53. layer->runAction(runCallback_triple_balls);
  54. layer->runAction(runCallback_paddle_ball);
  55. layer->runAction(runCallback_superball);
  56. layer->addAndThrowBall();
  57. return scene;
  58. }
  59. bool GameScene::init() {
  60. // 1. super init first
  61. if ( !Layer::init() ) {
  62. return false;
  63. }
  64. over = false;
  65. last_touch = time(NULL);
  66. FileUtils::getInstance()->addSearchPath("res");
  67. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  68. audio->preloadEffect("pipe.wav");
  69. audio->preloadEffect("metal.wav");
  70. audio->preloadEffect("bomb.wav");
  71. audio->preloadEffect("win.wav");
  72. auto visibleSize = Director::getInstance()->getVisibleSize();
  73. width = visibleSize.width;
  74. height = visibleSize.height;
  75. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  76. // Adiciona o fundo
  77. auto bg = LayerColor::create(COLOR_back);
  78. addChild(bg);
  79. paddle = Paddle::create();
  80. paddle->setPositionX(width/2);
  81. paddle->listen(width);
  82. addChild(paddle);
  83. // Registra evento de contato de objetos
  84. auto contactListener = EventListenerPhysicsContact::create();
  85. contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
  86. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  87. balls = Node::create();
  88. addChild(balls, 21);
  89. power_ups = Node::create();
  90. addChild(power_ups, 20);
  91. return true;
  92. }
  93. void GameScene::addAndThrowBall() {
  94. Ball* ball = Ball::create();
  95. while (paddle->getPositionX() == 0.0);
  96. ball->setPosition(paddle->getPositionX(), PADDLE_ALTURA+BALL_SIZE+0.1);
  97. ball->throwBall();
  98. balls->addChild(ball, 21);
  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() && !over) {
  104. if (nodeB->getTag() > nodeA->getTag()) {
  105. auto tmp = nodeB;
  106. nodeB = nodeA;
  107. nodeA = tmp;
  108. }
  109. if (nodeA->getTag() == BALL_TAG) {
  110. last_touch = time(NULL);
  111. }
  112. // sempre B < A
  113. if (nodeB->getTag() == BLOCK_TAG) {
  114. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  115. audio->playEffect("pipe.wav");
  116. nodeB->removeFromParentAndCleanup(true);
  117. } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
  118. if (nodeA->getTag() == BALL_TAG) {
  119. Ball* ball = (Ball*) nodeA;
  120. if (ball->superball) {
  121. nodeB->removeFromParentAndCleanup(true);
  122. ball->resetNormalball();
  123. }
  124. }
  125. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  126. audio->playEffect("metal.wav");
  127. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  128. caseBallCollision(nodeA);
  129. } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
  130. caseBallCore(nodeB, nodeA);
  131. } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
  132. caseBallCore(nodeA, nodeB);
  133. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == PADDLE_TAG) {
  134. caseSaveLevel(nodeB);
  135. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == PADDLE_TAG) {
  136. caseTripleBalls(nodeB);
  137. } else if (nodeB->getTag() == SUPERBALL_TAG && nodeA->getTag() == PADDLE_TAG) {
  138. caseSuperBall(nodeB);
  139. } else if (nodeB->getTag() == PADDLE_BALL_TAG && nodeA->getTag() == PADDLE_TAG) {
  140. caseRaqueteBall(nodeB);
  141. } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == BOTTOM_TAG) {
  142. nodeB->removeFromParentAndCleanup(true);
  143. } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == BOTTOM_TAG) {
  144. nodeB->removeFromParentAndCleanup(true);
  145. } else if (nodeB->getTag() == PADDLE_BALL_TAG && nodeA->getTag() == BOTTOM_TAG) {
  146. nodeB->removeFromParentAndCleanup(true);
  147. } else if (nodeB->getTag() == SUPERBALL_TAG && nodeA->getTag() == BOTTOM_TAG) {
  148. nodeB->removeFromParentAndCleanup(true);
  149. }
  150. }
  151. return true;
  152. }
  153. void GameScene::caseBallCollision (Node *ball) {
  154. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  155. audio->playEffect("bomb.wav");
  156. ParticleSun* m_emitter = ParticleSun::create();
  157. m_emitter->setPosition(ball->getPosition());
  158. m_emitter->setDuration(1);
  159. addChild(m_emitter);
  160. ball->removeFromParentAndCleanup(true);
  161. if (balls->getChildrenCount() == 0) {
  162. over = true;
  163. removeBallsAndPowersUP ();
  164. auto text = Label::createWithTTF(MSG_OVER, FONT, 40);
  165. text->setPosition(width/2, height/2);
  166. addChild(text);
  167. auto menu_item_start = MenuItemFont::create(MSG_RESTART, CC_CALLBACK_0(GameScene::nextLevel, this));
  168. menu_item_start->setFontNameObj(FONT);
  169. menu_item_start->setPosition(text->getPosition());
  170. menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
  171. auto *menu = Menu::create(menu_item_start, NULL);
  172. menu->setPosition(Point(0, 0));
  173. addChild(menu, 30);
  174. }
  175. }
  176. void GameScene::caseBallCore (Node *core, Node *ball) {
  177. auto scaleBy = ScaleBy::create(1.0f, 20.0f);
  178. core->getPhysicsBody()->setEnabled(false);
  179. core->runAction(scaleBy);
  180. auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
  181. audio->playEffect("win.wav");
  182. auto callbackRotate = CallFunc::create([=](){
  183. level = level + 1;
  184. auto menu_item_start = MenuItemFont::create(MSG_NEXT_LEVEL, CC_CALLBACK_0(GameScene::nextLevel, this));
  185. menu_item_start->setFontNameObj(FONT);
  186. menu_item_start->setPosition(Point(width / 2, (height / 2)));
  187. auto *menu = Menu::create(menu_item_start, NULL);
  188. menu->setPosition(Point(0, 0));
  189. addChild(menu, 30);
  190. });
  191. // cria uma sequencia com as ações e callbacks
  192. auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
  193. core->runAction(seq);
  194. removeBallsAndPowersUP();
  195. }
  196. void GameScene::caseSaveLevel(Node *powerup_ball) {
  197. UserDefault *userdata = UserDefault::getInstance();
  198. userdata->setIntegerForKey("level", level);
  199. userdata->setDoubleForKey("time", (double) time(NULL));
  200. userdata->flush();
  201. alert(MSG_LEVEL_SAVED);
  202. powerup_ball->removeFromParentAndCleanup(true);
  203. }
  204. void GameScene::nextLevel() {
  205. auto scene = GameScene::createScene(level);
  206. Director::getInstance()->replaceScene(scene);
  207. }
  208. void GameScene::setLevel(int level) {
  209. this->level = level;
  210. char level_text[256];
  211. sprintf(level_text,"Level %d", level);
  212. auto text = Label::createWithTTF(level_text, FONT, 30);
  213. text->setAnchorPoint(Vec2());
  214. text->setPosition(10, 10);
  215. addChild(text);
  216. }
  217. void GameScene::saveLevel() {
  218. if (over) return;
  219. if (rand_0_1() < DROP_LEVEL_SAVE)
  220. createPowerUpBody(COLOR_green, SAVE_TAG);
  221. delayCallback(CC_CALLBACK_0(GameScene::saveLevel, this));
  222. }
  223. void GameScene::tripleBallsAppearance() {
  224. if (over) return;
  225. double delta = (time(NULL)-last_touch);
  226. if (rand_0_1() < delta*DROP_TRIPLE_BALL ){
  227. last_touch = time(NULL);
  228. if (balls->getChildrenCount() < 3)
  229. createPowerUpBody(COLOR_pink, THREE_BALLS_TAG);
  230. }
  231. delayCallback(CC_CALLBACK_0(GameScene::tripleBallsAppearance, this));
  232. }
  233. void GameScene::caseTripleBalls(Node *powerup_ball) {
  234. alert(MSG_TRIPE_BALLS);
  235. while (!over && balls->getChildrenCount() < 3)
  236. addAndThrowBall();
  237. powerup_ball->removeFromParentAndCleanup(true);
  238. }
  239. void GameScene::paddleBallAppearance() {
  240. if (over) return;
  241. if (rand_0_1() < DROP_PADDLE_BALL && over == false)
  242. createPowerUpBody(COLOR_blue, PADDLE_BALL_TAG);
  243. delayCallback(CC_CALLBACK_0(GameScene::paddleBallAppearance, this));
  244. }
  245. void GameScene::casePaddleBall(Node *powerup_ball) {
  246. if (!over) {
  247. if (rand_0_1() < 0.5) {
  248. alert(MSG_DOUBLE_PADDLE);
  249. paddle->doubleSize();
  250. } else {
  251. alert(MSG_HALF_PADDLE);
  252. paddle->halfSize();
  253. }
  254. }
  255. powerup_ball->removeFromParentAndCleanup(true);
  256. }
  257. void GameScene::alert(std::string text) {
  258. if (!over) {
  259. auto display_label = Label::createWithTTF(text, FONT, 40);
  260. display_label->setPosition(width/2, height/2);
  261. addChild(display_label);
  262. display_label->runAction(FadeOut::create(3));
  263. }
  264. }
  265. void GameScene::superBallAppearance() {
  266. if (over) return;
  267. if (rand_0_1() < DROP_SUPER_BALL)
  268. createPowerUpBody(COLOR_orange, SUPERBALL_TAG);
  269. delayCallback(CC_CALLBACK_0(GameScene::superBallAppearance, this));
  270. }
  271. void GameScene::caseSuperBall(Node *powerup_ball) {
  272. if (over) return;
  273. alert(MSG_SUPER_BALL);
  274. Vector<Node *> balls_vector = balls->getChildren();
  275. Ball* ball = (Ball*) balls_vector.at(0);
  276. ball->setSuperball();
  277. powerup_ball->removeFromParentAndCleanup(true);
  278. }
  279. void GameScene::removeBallsAndPowersUP () {
  280. balls->removeAllChildrenWithCleanup(true);
  281. power_ups->removeAllChildrenWithCleanup(true);
  282. paddle->removeFromParentAndCleanup(true);
  283. }
  284. void GameScene::createPowerUpBody(Color4B color, int tag) {
  285. auto ball_draw = DrawNode::create();
  286. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(color));
  287. auto material = PhysicsMaterial(0.0f, 1.0f, 0.0f);
  288. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, material);
  289. physicsBody->setGravityEnable(true);
  290. physicsBody->setVelocity(Vec2(0,0));
  291. physicsBody->setLinearDamping(0.0);
  292. physicsBody->setMass(1.0f);
  293. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  294. physicsBody->setGroup(-1);
  295. ball_draw->addComponent(physicsBody);
  296. ball_draw->setTag(tag);
  297. ball_draw->setPosition(width * rand_0_1(), height);
  298. power_ups->addChild(ball_draw, 20);
  299. }
  300. void GameScene::delayCallback(const std::function<void ()> &func) {
  301. auto delta = POWER_UP_INTERVAL/4.0 * rand_minus1_1();
  302. auto delay = DelayTime::create(POWER_UP_INTERVAL+delta);
  303. CallFunc *runCallback = CallFunc::create(func);
  304. auto seq = Sequence::create(delay, runCallback, nullptr);
  305. runAction(seq);
  306. }