GameScene.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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() {
  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. auto 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. visibleSize.height += BALL_SIZE;
  27. auto body = PhysicsBody::createEdgeBox(visibleSize, material);
  28. auto edgeNode = Node::create();
  29. edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2-BALL_SIZE));
  30. body->setDynamic(false);
  31. edgeNode->setPhysicsBody(body);
  32. scene->addChild(edgeNode);
  33. return scene;
  34. }
  35. // on "init" you need to initialize your instance
  36. bool GameScene::init() {
  37. //////////////////////////////
  38. // 1. super init first
  39. if ( !Layer::init() ) {
  40. return false;
  41. }
  42. auto visibleSize = Director::getInstance()->getVisibleSize();
  43. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  44. // Adiona o fundo
  45. auto bg = LayerColor::create(COLOR_back);
  46. this->addChild(bg);
  47. auto ball = Ball::create();
  48. ball->setPosition(visibleSize.width / 2, BALL_SIZE*2);
  49. ball->throwBall();
  50. this->addChild(ball, 21);
  51. auto blocks = BlocksLayer::create();
  52. blocks->setPosition(visibleSize.width/2, visibleSize.height);
  53. this->addChild(blocks, 20);
  54. raquete = DrawNode::create();
  55. float py = RAQUETE_HEIGHT/2.0;
  56. float pxl = - RAQUETE_WIDTH/2;
  57. float pxr = RAQUETE_WIDTH/2;
  58. raquete->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  59. raquete->setPositionX(visibleSize.width/2);
  60. auto bsize = Size(RAQUETE_WIDTH+RAQUETE_HEIGHT, RAQUETE_HEIGHT);
  61. auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  62. physicsBody->setPositionOffset(Vec2(0, RAQUETE_HEIGHT/2));
  63. physicsBody->setGravityEnable(false);
  64. physicsBody->setDynamic(false);
  65. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  66. raquete->addComponent(physicsBody);
  67. this->addChild(raquete);
  68. auto listener1 = EventListenerTouchOneByOne::create();
  69. // trigger when you push down
  70. listener1->onTouchBegan = [=](Touch* touch, Event* event){
  71. raquete->setPositionX(touch->getLocation().x);
  72. float px = touch->getLocation().x;
  73. if (px >= RAQUETE_WIDTH/2 && px < visibleSize.width - RAQUETE_WIDTH/2)
  74. raquete->setPositionX(touch->getLocation().x);
  75. return true; // if you are consuming it
  76. };
  77. // trigger when moving touch
  78. listener1->onTouchMoved = [=](Touch* touch, Event* event){
  79. float px = touch->getLocation().x;
  80. if (px >= RAQUETE_WIDTH/2 && px <= visibleSize.width - RAQUETE_WIDTH/2)
  81. raquete->setPositionX(touch->getLocation().x);
  82. };
  83. // Add listener
  84. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
  85. // the edge of bottom
  86. auto body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
  87. auto edgeNode = Node::create();
  88. edgeNode->setPosition(Point(visibleSize.width/2, -BALL_SIZE));
  89. body->setDynamic(false);
  90. edgeNode->setTag(BOTTOM_TAG);
  91. body->setContactTestBitmask(0xFFFFFFFF);
  92. edgeNode->setPhysicsBody(body);
  93. this->addChild(edgeNode);
  94. return true;
  95. }