1
0

BeginScene.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /********************************************************************
  2. * Nomes: Gabriel Capella Números USP: 8962078
  3. * João Herique Luciano 8535957
  4. *
  5. * Tarefa: RedCore - EP2 MAC0463
  6. * Arquivo: BeginScene.cpp
  7. * Descrição: Classe para tela inicial do jogo.
  8. ********************************************************************/
  9. #include "BeginScene.h"
  10. #include "GameScene.h"
  11. #include "SimpleAudioEngine.h"
  12. #include "GameScene/Ball.h"
  13. #include "params.h"
  14. USING_NS_CC;
  15. Scene* BeginScene::createScene() {
  16. auto scene = Scene::createWithPhysics();
  17. Size visibleSize = Director::getInstance()->getVisibleSize();
  18. #if DEBUG
  19. scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  20. #endif
  21. auto layer = BeginScene::create();
  22. scene->addChild(layer);
  23. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  24. material.density = 1.0f;
  25. material.restitution = 1.0f;
  26. material.friction = 0.0f;
  27. // the edge of the screen
  28. auto body = PhysicsBody::createEdgeBox(visibleSize, material);
  29. auto edgeNode = Node::create();
  30. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  31. body->setDynamic(false);
  32. edgeNode->setPhysicsBody(body);
  33. scene->addChild(edgeNode);
  34. return scene;
  35. }
  36. bool BeginScene::init() {
  37. if (!Layer::init()) {
  38. return false;
  39. }
  40. auto visibleSize = Director::getInstance()->getVisibleSize();
  41. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  42. // Adiona o fundo
  43. auto bg = cocos2d::LayerColor::create(COLOR_back);
  44. this->addChild(bg);
  45. auto menu_item_start = MenuItemFont::create(MSG_START, CC_CALLBACK_0(BeginScene::Play, this));
  46. menu_item_start->setFontNameObj(FONT);
  47. menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.25)));
  48. auto *menu = Menu::create(menu_item_start, NULL);
  49. menu->setPosition(Point(0, 0));
  50. this->addChild(menu, 10);
  51. for (int i = 0; i < 10; i++) {
  52. auto ball = Ball::create();
  53. ball->setPosition(visibleSize.width / 2, BALL_SIZE*2);
  54. ball->throwBall();
  55. this->addChild(ball);
  56. }
  57. auto core = DrawNode::create();
  58. core->drawDot(Vec2(0, 0), CORE_RADIUS*4, Color4F(COLOR_red));
  59. core->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.75)));
  60. this->addChild(core);
  61. auto physicsBody = PhysicsBody::createCircle(CORE_RADIUS*4, PhysicsMaterial(0.1f, 0.9f, 0.0f));
  62. physicsBody->setDynamic(false);
  63. core->addComponent(physicsBody);
  64. auto text = Label::createWithTTF(MSG_TITLE, FONT, 100);
  65. text->setPosition(core->getPosition());
  66. this->addChild(text);
  67. double last, diff;
  68. int level;
  69. UserDefault *userdata = UserDefault::getInstance();
  70. level = userdata->getIntegerForKey("level");
  71. last = userdata->getDoubleForKey("time");
  72. unsigned long int sec = time(NULL);
  73. diff = 3600 * LOSE_IS + last - sec;
  74. while (diff < 0 && level > 0) {
  75. last += 3600.0* ((double) LOSE_IS);
  76. level--;
  77. diff = 3600 * LOSE_IS + last - sec;
  78. }
  79. userdata->setDoubleForKey("time", last);
  80. userdata->setIntegerForKey("level", level);
  81. this->level_game = level;
  82. char level_text[256];
  83. if (level != 0) {
  84. sprintf(level_text,MSG_LEVEL_BACK, level, diff/3600.0);
  85. auto textlevel = Label::createWithTTF(level_text, FONT, 20);
  86. textlevel->setPosition(menu_item_start->getPositionX(), menu_item_start->getPositionY()-60);
  87. this->addChild(textlevel);
  88. }
  89. return true;
  90. }
  91. void BeginScene::Play() {
  92. auto scene = GameScene::createScene(this->level_game);
  93. Director::getInstance()->replaceScene(scene);
  94. }