123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- //
- // GameScene.cpp
- // RedCore2
- //
- // Created by Gabriel Capella on 31/05/17.
- //
- //
- #include "GameScene.h"
- #include "SimpleAudioEngine.h"
- #include "GameScene/BlocksLayer.h"
- #include "GameScene/Ball.h"
- #include "params.h"
- USING_NS_CC;
- Scene* GameScene::createScene(int level) {
- auto scene = Scene::createWithPhysics();
- Size visibleSize = Director::getInstance()->getVisibleSize();
-
- #if DEBUG
- scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
- #endif
-
- GameScene* layer = GameScene::create();
- scene->addChild(layer);
-
- // the edge of the screen
- auto body = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0.0f, 1.0f, 0.0f));
- auto edgeNode = Node::create();
- edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
- body->setDynamic(false);
- edgeNode->setPhysicsBody(body);
- scene->addChild(edgeNode);
-
- // bottom edge
- auto bottom_body = PhysicsBody::createEdgeBox(Size(visibleSize.width, 0));
- auto bottomNode = Node::create();
- bottomNode->setPosition(Point(visibleSize.width/2, 0));
- bottom_body->setDynamic(false);
- bottomNode->setTag(BOTTOM_TAG);
- bottom_body->setContactTestBitmask(0xFFFFFFFF);
- bottomNode->setPhysicsBody(bottom_body);
- layer->addChild(bottomNode);
-
- auto blocks = BlocksLayer::create();
- blocks->setLevel(level);
- blocks->setPosition(visibleSize.width/2, visibleSize.height);
- layer->addChild(blocks, 20);
-
- layer->setLevel(level);
-
- // inicializa os callbacks dos power ups/downs
- auto delay_save_level = DelayTime::create(10.0);
- CallFunc *runCallback_save_level = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
- auto seq_save_level = Sequence::create(delay_save_level, runCallback_save_level, nullptr);
-
- CallFunc *runCallback_triple_balls = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBallsAppearance, layer));
- CallFunc *runCallback_paddle_ball = CallFunc::create(CC_CALLBACK_0(GameScene::paddleBallAppearance, layer));
-
- layer->runAction(seq_save_level);
- layer->runAction(runCallback_triple_balls);
- layer->runAction(runCallback_paddle_ball);
-
- layer->addAndThrowBall();
-
- return scene;
- }
- // on "init" you need to initialize your instance
- bool GameScene::init() {
- //////////////////////////////
- // 1. super init first
- if ( !Layer::init() ) {
- return false;
- }
-
- over = false;
- last_touch = 0;
-
- // https://www.freesound.org/people/schademans/sounds/13290/
- FileUtils::getInstance()->addSearchPath("res");
- auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
- audio->preloadEffect("pipe.wav");
- audio->preloadEffect("metal.wav");
- audio->preloadEffect("bomb.wav");
- audio->preloadEffect("win.wav");
-
- auto visibleSize = Director::getInstance()->getVisibleSize();
- width = visibleSize.width;
- height = visibleSize.height;
- Vec2 origin = Director::getInstance()->getVisibleOrigin();
-
- // Adiona o fundo
- auto bg = LayerColor::create(COLOR_back);
- addChild(bg);
-
- paddle = DrawNode::create();
- paddle_size = PADDLE_WIDTH;
- float py = PADDLE_HEIGHT/2.0;
- float pxl = - PADDLE_WIDTH/2;
- float pxr = PADDLE_WIDTH/2;
- paddle->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
- paddle->setPositionX(width/2);
- paddle->setPositionY(PADDLE_ALTURA);
-
- auto bsize = Size(PADDLE_WIDTH+PADDLE_HEIGHT, PADDLE_HEIGHT);
- auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
- physicsBody->setPositionOffset(Vec2(0, PADDLE_HEIGHT/2));
- physicsBody->setGravityEnable(false);
- physicsBody->setDynamic(false);
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
- paddle->setTag(RACKET_TAG);
- paddle->addComponent(physicsBody);
- paddle_size = PADDLE_WIDTH;
-
- addChild(paddle);
-
- auto listener1 = EventListenerTouchOneByOne::create();
-
- // trigger when you push down
- listener1->onTouchBegan = [=](Touch* touch, Event* event){
- paddle->setPositionX(touch->getLocation().x);
- float px = touch->getLocation().x;
- if (px >= paddle_size/2 && px < width - paddle_size/2)
- paddle->setPositionX(touch->getLocation().x);
- return true; // if you are consuming it
- };
-
- // trigger when moving touch
- listener1->onTouchMoved = [=](Touch* touch, Event* event){
- float px = touch->getLocation().x;
- if (px >= PADDLE_WIDTH/2 && px <= width - PADDLE_WIDTH/2)
- paddle->setPositionX(touch->getLocation().x);
- };
-
-
- // Add listener
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
-
- // Evento no contato das bolas
- auto contactListener = EventListenerPhysicsContact::create();
- contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
-
- balls = Node::create();
- addChild(balls, 21);
-
- return true;
- }
- void GameScene::addAndThrowBall() {
- Ball* ball = Ball::create();
- ball->setPosition(paddle->getPositionX(), PADDLE_ALTURA+BALL_SIZE);
- ball->throwBall();
- balls->addChild(ball, 21);
- }
- bool GameScene::onContactBegin(PhysicsContact& contact) {
- auto nodeA = contact.getShapeA()->getBody()->getNode();
- auto nodeB = contact.getShapeB()->getBody()->getNode();
-
- if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
-
- if (nodeB->getTag() > nodeA->getTag()) {
- auto tmp = nodeB;
- nodeB = nodeA;
- nodeA = tmp;
- }
-
- // sempre B < A
- if (nodeB->getTag() == BLOCK_TAG) {
- auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
- audio->playEffect("pipe.wav");
- nodeB->removeFromParentAndCleanup(true);
- } else if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
- auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
- audio->playEffect("metal.wav");
- } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
- caseBallCollision(nodeA);
- } else if (nodeB->getTag() == CORE_TAG && nodeA->getTag() == BALL_TAG) {
- caseBallCore(nodeB, nodeA);
- } else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
- caseBallCore(nodeA, nodeB);
- } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == RACKET_TAG) {
- caseSaveLevel(nodeB);
- } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == RACKET_TAG) {
- caseTripleBalls(nodeB);
- } else if (nodeB->getTag() == RACKET_BALL_TAG && nodeA->getTag() == RACKET_TAG) {
- caseRaqueteBall(nodeB);
- } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == BOTTOM_TAG) {
- nodeB->removeFromParentAndCleanup(true);
- } else if (nodeB->getTag() == THREE_BALLS_TAG && nodeA->getTag() == BOTTOM_TAG) {
- nodeB->removeFromParentAndCleanup(true);
- } else if (nodeB->getTag() == RACKET_BALL_TAG && nodeA->getTag() == BOTTOM_TAG) {
- nodeB->removeFromParentAndCleanup(true);
- }
-
- }
- return true;
- }
- void GameScene::caseBallCollision (Node *ball) {
-
- auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
- audio->playEffect("bomb.wav");
- ParticleSun* m_emitter = ParticleSun::create();
- m_emitter->setPosition(ball->getPosition());
- m_emitter->setDuration(1);
- addChild(m_emitter);
- ball->removeFromParentAndCleanup(true);
-
- if (balls->getChildrenCount() == 0) {
- over = true;
- auto text = Label::createWithTTF(MSG_OVER, FONT, 40);
- text->setPosition(width/2, height/2);
- addChild(text);
-
- auto menu_item_start = MenuItemFont::create("Restart", CC_CALLBACK_1(GameScene::NextLevel, this));
- menu_item_start->setPosition(text->getPosition());
- menu_item_start->setPositionY(menu_item_start->getPositionY()-50);
- auto *menu = Menu::create(menu_item_start, NULL);
- menu->setPosition(Point(0, 0));
- addChild(menu, 30);
- balls->removeFromParentAndCleanup(true);
- }
- }
- void GameScene::caseBallCore (Node *core, Node *ball) {
- auto scaleBy = ScaleBy::create(1.0f, 20.0f);
- core->getPhysicsBody()->setEnabled(false);
- core->runAction(scaleBy);
- auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
- audio->playEffect("win.wav");
-
- auto callbackRotate = CallFunc::create([=](){
- level = level + 1;
- auto menu_item_start = MenuItemFont::create("Next Level", CC_CALLBACK_1(GameScene::NextLevel, this));
- menu_item_start->setPosition(Point(width / 2, (height / 2)));
- auto *menu = Menu::create(menu_item_start, NULL);
- menu->setPosition(Point(0, 0));
- addChild(menu, 30);
- });
-
- // create a sequence with the actions and callbacks
- auto seq = Sequence::create(scaleBy, callbackRotate, nullptr);
- core->runAction(seq);
- ball->removeFromParentAndCleanup(true);
- }
- void GameScene::caseSaveLevel(Node *powerup_ball) {
- UserDefault *userdata = UserDefault::getInstance();
- userdata->setIntegerForKey("level", level);
- userdata->setDoubleForKey("time", (double) time(NULL));
- userdata->flush();
- alert(MSG_LEVEL_SAVED);
- powerup_ball->removeFromParentAndCleanup(true);
- }
- void GameScene::NextLevel(Ref *pSender) {
- auto scene = GameScene::createScene(level);
- Director::getInstance()->replaceScene(scene);
- }
- void GameScene::setLevel(int level) {
- level = level;
- char level_text[256];
- sprintf(level_text,"Level %d", level);
- auto text = Label::createWithTTF(level_text, FONT, 30);
- text->setAnchorPoint(Vec2());
- text->setPosition(10, 10);
- addChild(text);
- }
- // Power-up: salva o nível atual
- void GameScene::saveLevel() {
- if (rand_0_1() < DROP_LEVEL_SAVE && over == false){
- auto ball_draw = DrawNode::create();
- ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_green));
-
- auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
- physicsBody->setGravityEnable(true);
- physicsBody->setVelocity(Vec2(0,0));
- physicsBody->setLinearDamping(0.0);
- physicsBody->setMass(1.0f);
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
- physicsBody->setGroup(-1);
- ball_draw->addComponent(physicsBody);
- ball_draw->setTag(SAVE_TAG);
-
- ball_draw->setPosition(width * rand_0_1(), height);
- addChild(ball_draw);
- }
-
- auto delay = DelayTime::create(1.0);
- CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, this));
- auto seq = Sequence::create(delay, runCallback, nullptr);
- runAction(seq);
-
- }
- // Power-up: deixa 3 bolas na tela ao invés de 1
- void GameScene::tripleBallsAppearance() {
- if (rand_0_1() < DROP_TRIPLE_BALL && over == false){
- auto ball_draw = DrawNode::create();
- ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_pink));
-
-
- auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
- physicsBody->setGravityEnable(true);
- physicsBody->setVelocity(Vec2(0,0));
- physicsBody->setLinearDamping(0.0);
- physicsBody->setMass(1.0f);
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
- physicsBody->setGroup(-1);
- ball_draw->addComponent(physicsBody);
- ball_draw->setTag(THREE_BALLS_TAG);
-
- ball_draw->setPosition(width * rand_0_1(), height);
- addChild(ball_draw);
- }
-
- auto delay = DelayTime::create(1.0);
- CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::tripleBallsAppearance, this));
- auto seq = Sequence::create(delay, runCallback, nullptr);
- runAction(seq);
- }
- void GameScene::caseTripleBalls(Node *powerup_ball) {
- alert(MSG_TRIPE_BALLS);
- while (!over && balls->getChildrenCount() < 3)
- addAndThrowBall();
- powerup_ball->removeFromParentAndCleanup(true);
- }
- void GameScene::paddleBallAppearance() {
- if (rand_0_1() < DROP_RACKET_BALL && over == false){
- auto ball_draw = DrawNode::create();
- ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_blue));
-
- auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, PhysicsMaterial(0.0f, 1.0f, 0.0f));
- physicsBody->setGravityEnable(true);
- physicsBody->setVelocity(Vec2(0,0));
- physicsBody->setLinearDamping(0.0);
- physicsBody->setMass(1.0f);
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
- physicsBody->setGroup(-1);
- ball_draw->addComponent(physicsBody);
- ball_draw->setTag(RACKET_BALL_TAG);
-
- ball_draw->setPosition(width * rand_0_1(), height);
- addChild(ball_draw);
- }
-
- auto delay = DelayTime::create(1.0);
- CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::paddleBallAppearance, this));
- auto seq = Sequence::create(delay, runCallback, nullptr);
- runAction(seq);
- }
- /* Power-up/down: 50% de chance de dobrar o tamanho da paddle,
- e 50% de chance de diminuir seu tamanho pela metade.
- Para não ficar muito fácil/difícil, impusemos tamanho mínimo/máximo da paddle.
- */
- void GameScene::caseRaqueteBall(Node *powerup_ball) {
- if (!over) {
-
- if (rand_0_1() < 0.5) {
- if (paddle_size <= PADDLE_WIDTH * 2) {
- alert(MSG_DOUBLE_PADDLE);
- paddle_size *= 2;
- }
- } else {
- if (paddle_size >= PADDLE_WIDTH / 2) {
- alert(MSG_HALF_PADDLE);
- paddle_size /= 2;
- }
- }
-
- //cria uma nova paddle e substitui a antiga paddle
- auto new_paddle = DrawNode::create();
- float py = PADDLE_HEIGHT/2.0;
- float pxl = - paddle_size/2;
- float pxr = paddle_size/2;
- new_paddle->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
- new_paddle->setPositionX(paddle->getPositionX());
- new_paddle->setPositionY(PADDLE_ALTURA);
-
- auto bsize = Size(paddle_size+PADDLE_HEIGHT, PADDLE_HEIGHT);
- auto physicsBody = PhysicsBody::createBox(bsize, PhysicsMaterial(0.1f, 1.0f, 0.0f));
- physicsBody->setPositionOffset(Vec2(0, PADDLE_HEIGHT/2));
- physicsBody->setGravityEnable(false);
- physicsBody->setDynamic(false);
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
- new_paddle->setTag(RACKET_TAG);
- new_paddle->addComponent(physicsBody);
-
- paddle->removeFromParentAndCleanup(true);
- paddle = new_paddle;
- addChild(paddle);
- }
- powerup_ball->removeFromParentAndCleanup(true);
- }
- void GameScene::alert(std::string text) {
- if (!over) {
- auto display_label = Label::createWithTTF(text, FONT, 40);
- display_label->setPosition(width/2, height/2);
- addChild(display_label);
- display_label->runAction(FadeOut::create(3));
- }
- }
|