GameScene.cpp 14 KB

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