|
@@ -43,6 +43,12 @@ Scene* GameScene::createScene(int level) {
|
|
|
layer->addChild(blocks, 20);
|
|
|
|
|
|
layer->setLevel(level);
|
|
|
+
|
|
|
+ auto delay = DelayTime::create(10.0);
|
|
|
+ CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, layer));
|
|
|
+ auto seq = Sequence::create(delay, runCallback, nullptr);
|
|
|
+ layer->runAction(seq);
|
|
|
+
|
|
|
return scene;
|
|
|
}
|
|
|
|
|
@@ -54,6 +60,8 @@ bool GameScene::init() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ this->over = false;
|
|
|
+
|
|
|
|
|
|
FileUtils::getInstance()->addSearchPath("res");
|
|
|
auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
|
|
@@ -89,6 +97,7 @@ bool GameScene::init() {
|
|
|
physicsBody->setGravityEnable(false);
|
|
|
physicsBody->setDynamic(false);
|
|
|
physicsBody->setContactTestBitmask(0xFFFFFFFF);
|
|
|
+ raquete->setTag(RACKET_TAG);
|
|
|
raquete->addComponent(physicsBody);
|
|
|
|
|
|
this->addChild(raquete);
|
|
@@ -130,11 +139,6 @@ bool GameScene::init() {
|
|
|
contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
|
|
|
|
|
|
- UserDefault *userdata = UserDefault::getInstance();
|
|
|
-
|
|
|
- userdata->setIntegerForKey("level", 0);
|
|
|
- userdata->flush();
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -142,9 +146,15 @@ 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 (nodeA && nodeB && nodeA->getTag() != nodeB->getTag() && !this->over) {
|
|
|
|
|
|
|
|
|
+ if (nodeB->getTag() > nodeA->getTag()) {
|
|
|
+ auto tmp = nodeB;
|
|
|
+ nodeB = nodeA;
|
|
|
+ nodeA = tmp;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
if (nodeB->getTag() == BLOCK_TAG) {
|
|
|
auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
|
|
@@ -159,12 +169,28 @@ bool GameScene::onContactBegin(PhysicsContact& contact) {
|
|
|
ball_core(nodeB, nodeA);
|
|
|
} else if (nodeB->getTag() == BALL_TAG && nodeA->getTag() == CORE_TAG) {
|
|
|
ball_core(nodeA, nodeB);
|
|
|
+ } else if (nodeB->getTag() == SAVE_TAG && nodeA->getTag() == RACKET_TAG) {
|
|
|
+
|
|
|
+ CCLOG(">>> %d", this->level);
|
|
|
+
|
|
|
+ Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
+
|
|
|
+ UserDefault *userdata = UserDefault::getInstance();
|
|
|
+ userdata->setIntegerForKey("level", this->level);
|
|
|
+ userdata->setDoubleForKey("time", (double) time(NULL));
|
|
|
+ userdata->flush();
|
|
|
+ nodeB->removeFromParentAndCleanup(true);
|
|
|
+ auto text = Label::createWithTTF("Level Saved!", "fonts/Marker Felt.ttf", 40);
|
|
|
+ text->setPosition(visibleSize.width/2, visibleSize.height/2);
|
|
|
+ this->addChild(text);
|
|
|
+ text->runAction(FadeOut::create(3));
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void GameScene::ball_collision (Node *ball) {
|
|
|
+ this->over = true;
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
auto text = Label::createWithTTF("Game Over...", "fonts/Marker Felt.ttf", 40);
|
|
|
text->setPosition(visibleSize.width/2, visibleSize.height/2);
|
|
@@ -209,7 +235,7 @@ void GameScene::ball_core (Node *core, Node *ball) {
|
|
|
ball->removeFromParentAndCleanup(true);
|
|
|
}
|
|
|
|
|
|
-void GameScene::NextLevel(cocos2d::Ref *pSender) {
|
|
|
+void GameScene::NextLevel(Ref *pSender) {
|
|
|
auto scene = GameScene::createScene(this->level + 1);
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
|
@@ -224,3 +250,35 @@ void GameScene::setLevel(int level) {
|
|
|
text->setPosition(10, 10);
|
|
|
this->addChild(text);
|
|
|
}
|
|
|
+
|
|
|
+void GameScene::saveLevel() {
|
|
|
+ if (rand_0_1() < DROP_LEVEL_SAVE && this->over == false){
|
|
|
+ Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
+ auto ball_draw = DrawNode::create();
|
|
|
+ ball_draw->drawDot(Vec2(0, 0), BALL_SIZE/3.0, Color4F(COLOR_green));
|
|
|
+
|
|
|
+ auto material = PHYSICSBODY_MATERIAL_DEFAULT;
|
|
|
+ material.density = 0.0f;
|
|
|
+ material.restitution = 1.0f;
|
|
|
+ material.friction = 0.0f;
|
|
|
+
|
|
|
+ auto physicsBody = PhysicsBody::createCircle(BALL_SIZE/3.0, material);
|
|
|
+ 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(visibleSize.width * rand_0_1(), visibleSize.height);
|
|
|
+ this->addChild(ball_draw);
|
|
|
+ }
|
|
|
+
|
|
|
+ auto delay = DelayTime::create(rand_0_1()*10.0+3.0);
|
|
|
+ CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(GameScene::saveLevel, this));
|
|
|
+ auto seq = Sequence::create(delay, runCallback, nullptr);
|
|
|
+ this->runAction(seq);
|
|
|
+
|
|
|
+}
|