BeginScene.cpp 3.4 KB

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