HelloWorldScene.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "HelloWorldScene.h"
  2. #include "GameScene.h"
  3. #include "SimpleAudioEngine.h"
  4. #include "Ball.h"
  5. #include "params.h"
  6. USING_NS_CC;
  7. Scene* HelloWorld::createScene() {
  8. auto scene = Scene::createWithPhysics();
  9. Size visibleSize = Director::getInstance()->getVisibleSize();
  10. //choose whitch part need to draw, Joint, Shape, Contact, None or All
  11. //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
  12. auto layer = HelloWorld::create();
  13. scene->addChild(layer);
  14. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  15. material.density = 1.0f;
  16. material.restitution = 1.0f;
  17. material.friction = 0.0f;
  18. // the edge of the screen
  19. auto body = PhysicsBody::createEdgeBox(visibleSize, material);
  20. auto edgeNode = Node::create();
  21. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  22. body->setDynamic(false);
  23. edgeNode->setPhysicsBody(body);
  24. scene->addChild(edgeNode);
  25. return scene;
  26. }
  27. // on "init" you need to initialize your instance
  28. bool HelloWorld::init() {
  29. //////////////////////////////
  30. // 1. super init first
  31. if ( !Layer::init() )
  32. {
  33. return false;
  34. }
  35. auto visibleSize = Director::getInstance()->getVisibleSize();
  36. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  37. // Adiona o fundo
  38. auto bg = cocos2d::LayerColor::create(COLOR_back);
  39. this->addChild(bg);
  40. auto menu_item_start = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::Play, this));
  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("RedCore", "fonts/Marker Felt.ttf", 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 * 12 + last - sec;
  68. while (diff < 0 && level > 0) {
  69. last += 3600.0*12.0;
  70. level--;
  71. diff = 3600 * 12 + 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,"You are at level %d. In %.1f hour(s) you will regrow a level.", level, diff/3600.0);
  79. auto textlevel = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 20);
  80. textlevel->setPosition(menu_item_start->getPositionX(), menu_item_start->getPositionY()-60);
  81. this->addChild(textlevel);
  82. }
  83. return true;
  84. }
  85. void HelloWorld::Play(cocos2d::Ref *pSender) {
  86. auto scene = GameScene::createScene(this->levels);
  87. Director::getInstance()->replaceScene(scene);
  88. //Director::getInstance()->end();
  89. }