Browse Source

Adionando gerador de níveis aleatorios

capellaresumo 6 years ago
parent
commit
3af042a180

+ 4 - 0
Classes/AppDelegate.cpp

@@ -85,6 +85,10 @@ bool AppDelegate::applicationDidFinishLaunching() {
     // {        
     //     director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
     // }
+    
+//    if (!UserDefault::getInstance()->isXMLFileExist()) {
+//        //CCLOG(UserDefault::getInstance()->getXMLFilePath());
+//    }
 
     register_all_packages();
 

+ 1 - 1
Classes/Ball.cpp

@@ -25,7 +25,7 @@ bool Ball::init() {
     
     auto material = PHYSICSBODY_MATERIAL_DEFAULT;
     material.density = 0.0f;
-    material.restitution = 1.001f;
+    material.restitution = 1.005f;
     material.friction = 0.0f; //set friction here
     
     auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);

+ 29 - 8
Classes/BlocksLayer.cpp

@@ -11,19 +11,37 @@
 
 USING_NS_CC;
 
-// on "init" you need to initialize your instance
-bool BlocksLayer::init() {
-    //////////////////////////////
-    // 1. super init first
-    
+
+
+void BlocksLayer::setLevel(int level) {
     auto visibleSize = Director::getInstance()->getVisibleSize();
     Vec2 center = Vec2(visibleSize.width/2, visibleSize.height/2);
+    int count = 0, i, j;
+    int circles_n;
+    double p_indus = 0;
+    double size = CORE_RADIUS*0.75;
+    int delta = 15;
+    
+    for (i = 0; count <= level; i++) {
+        for (j = 0; count <= level && j <= i; j++) {
+            if (i + 1 > delta) {
+                delta *= 1.5;
+                size /= 1.5;
+                i = 0;
+            }
+            circles_n = i + 1;
+            p_indus = j * 0.035;
+            
+            CCLOG("> %d %d", i+1, j);
+            count++;
+        }
+    }
     
     this->setPosition(center);
     
     // aumentar aqui
-    for (int i = 5; i >= 0; i--) {
-        auto circle = createCircle(CORE_RADIUS*0.75, i, 10, 0.2);
+    for (int i = circles_n - 1; i >= 0; i--) {
+        auto circle = createCircle(size, i, 10, p_indus);
         this->addChild(circle, 9);
     }
     
@@ -43,7 +61,10 @@ bool BlocksLayer::init() {
     core->setTag(CORE_TAG);
     this->addChild(core, 40);
     
-    
+}
+
+// on "init" you need to initialize your instance
+bool BlocksLayer::init() {
     return true;
 }
 

+ 2 - 5
Classes/BlocksLayer.h

@@ -13,15 +13,12 @@
 
 class BlocksLayer : public cocos2d::Node {
     public:
-    
     virtual bool init();
-    
-    // implement the "static create()" method manually
+    void setLevel(int level);
     CREATE_FUNC(BlocksLayer);
-    
+
     private:
     Node* createCircle (double size, int number, int number_of_segments, double p);
-    
     Node* createSegment (double r_internal, double r_externa, double begin, double end, bool especial);
 };
 

+ 38 - 10
Classes/GameScene.cpp

@@ -14,14 +14,14 @@
 
 USING_NS_CC;
 
-Scene* GameScene::createScene() {
+Scene* GameScene::createScene(int level) {
     auto scene = Scene::createWithPhysics();
     Size visibleSize = Director::getInstance()->getVisibleSize();
     
     //choose whitch part need to draw, Joint, Shape, Contact, None or All
-    //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
+    // scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
     
-    auto layer = GameScene::create();
+    GameScene* layer = GameScene::create();
     scene->addChild(layer);
     
     auto material = PHYSICSBODY_MATERIAL_DEFAULT;
@@ -37,6 +37,12 @@ Scene* GameScene::createScene() {
     edgeNode->setPhysicsBody(body);
     scene->addChild(edgeNode);
     
+    auto blocks = BlocksLayer::create();
+    blocks->setLevel(level);
+    blocks->setPosition(visibleSize.width/2, visibleSize.height);
+    layer->addChild(blocks, 20);
+    
+    layer->setLevel(level);
     return scene;
 }
 
@@ -54,6 +60,7 @@ bool GameScene::init() {
     audio->preloadEffect("pipe.wav");
     audio->preloadEffect("metal.wav");
     audio->preloadEffect("bomb.wav");
+    audio->preloadEffect("win.wav");
     
     auto visibleSize = Director::getInstance()->getVisibleSize();
     Vec2 origin = Director::getInstance()->getVisibleOrigin();
@@ -68,10 +75,6 @@ bool GameScene::init() {
     ball->throwBall();
     this->addChild(ball, 21);
     
-    auto blocks = BlocksLayer::create();
-    blocks->setPosition(visibleSize.width/2, visibleSize.height);
-    this->addChild(blocks, 20);
-    
     auto raquete = DrawNode::create();
     float py = RAQUETE_HEIGHT/2.0;
     float pxl = - RAQUETE_WIDTH/2;
@@ -127,6 +130,11 @@ bool GameScene::init() {
     contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this);
     _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
     
+    UserDefault *userdata = UserDefault::getInstance();
+    //int level = userdata->getIntegerForKey("level");
+    userdata->setIntegerForKey("level", 0);
+    userdata->flush();
+    
     return true;
 }
 
@@ -135,14 +143,14 @@ bool GameScene::onContactBegin(PhysicsContact& contact) {
     auto nodeB = contact.getShapeB()->getBody()->getNode();
     
     if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
-        CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
+        //CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
         
         // sempre B < A
         if (nodeB->getTag() == BLOCK_TAG) {
             auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
             audio->playEffect("pipe.wav");
             nodeB->removeFromParentAndCleanup(true);
-        } if (nodeB->getTag() == INDESTRUCTIBLE_BLOCK_TAG) {
+        } 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) {
@@ -162,6 +170,14 @@ void GameScene::ball_collision (Node *ball) {
     text->setPosition(visibleSize.width/2, visibleSize.height/2);
     this->addChild(text);
     
+    this->level = -1;
+    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));
+    this->addChild(menu, 30);
+    
     auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
     audio->playEffect("bomb.wav");
     ParticleSun* m_emitter = ParticleSun::create();
@@ -175,6 +191,8 @@ void GameScene::ball_core (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([=](){
         auto visibleSize = Director::getInstance()->getVisibleSize();
@@ -192,7 +210,17 @@ void GameScene::ball_core (Node *core, Node *ball) {
 }
 
 void GameScene::NextLevel(cocos2d::Ref *pSender) {
-    auto scene = GameScene::createScene();
+    auto scene = GameScene::createScene(this->level + 1);
     Director::getInstance()->replaceScene(scene);
     //Director::getInstance()->end();
 }
+
+void GameScene::setLevel(int level) {
+    this->level = level;
+    char level_text[256];
+    sprintf(level_text,"Level %d", this->level);
+    auto text = Label::createWithTTF(level_text, "fonts/Marker Felt.ttf", 30);
+    text->setAnchorPoint(Vec2());
+    text->setPosition(10, 10);
+    this->addChild(text);
+}

+ 10 - 12
Classes/GameScene.h

@@ -13,18 +13,16 @@
 
 class GameScene : public cocos2d::Layer {
     public:
-    static cocos2d::Scene* createScene();
-    
-    virtual bool init();
-    
-    bool onContactBegin(cocos2d::PhysicsContact& contact);
-        
-    // implement the "static create()" method manually
-    CREATE_FUNC(GameScene);
-private:
-    void ball_collision (cocos2d::Node *ball);
-    void ball_core (cocos2d::Node *core, cocos2d::Node *ball);
-    void NextLevel(Ref *pSender);
+        static cocos2d::Scene* createScene(int level);
+        virtual bool init();
+        bool onContactBegin(cocos2d::PhysicsContact& contact);
+        CREATE_FUNC(GameScene);
+        void setLevel(int level);
+    private:
+        void ball_collision (cocos2d::Node *ball);
+        void ball_core (cocos2d::Node *core, cocos2d::Node *ball);
+        void NextLevel(Ref *pSender);
+        int level;
 };
 
 

+ 1 - 2
Classes/HelloWorldScene.cpp

@@ -87,12 +87,11 @@ bool HelloWorld::init() {
         this->addChild(ball);
     }
 
-    
     return true;
 }
 
 void HelloWorld::Play(cocos2d::Ref *pSender) {
-    auto scene = GameScene::createScene();
+    auto scene = GameScene::createScene(0);
     Director::getInstance()->replaceScene(scene);
     //Director::getInstance()->end();
 }

BIN
Resources/res/win.wav


+ 1 - 1
proj.android-studio/app/AndroidManifest.xml

@@ -12,7 +12,7 @@
         
         <!-- Tell Cocos2dxActivity the name of our .so -->
         <meta-data android:name="android.app.lib_name"
-                   android:value="RedCOre" />
+                   android:value="MyGame" />
 
         <activity
             android:name="org.cocos2dx.cpp.AppActivity"

+ 1 - 1
proj.android-studio/app/res/values/strings.xml

@@ -1,3 +1,3 @@
 <resources>
-    <string name="app_name">RedCore2</string>
+    <string name="app_name">RedCore</string>
 </resources>

+ 2 - 2
proj.android-studio/settings.gradle

@@ -1,4 +1,4 @@
 include ':libcocos2dx'
 project(':libcocos2dx').projectDir = new File(settingsDir, '../cocos2d/cocos/platform/android/libcocos2dx')
-include ':RedCore'
-project(':RedCore').projectDir = new File(settingsDir, 'app')
+include ':RedCore2'
+project(':RedCore2').projectDir = new File(settingsDir, 'app')

BIN
proj.ios_mac/RedCore2.xcodeproj/project.xcworkspace/xcuserdata/capella.xcuserdatad/UserInterfaceState.xcuserstate


+ 3 - 3
proj.ios_mac/RedCore2.xcodeproj/xcuserdata/capella.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -26,11 +26,11 @@
             ignoreCount = "0"
             continueAfterRunningActions = "No"
             filePath = "../Classes/BlocksLayer.cpp"
-            timestampString = "518157078.037242"
+            timestampString = "518213096.585336"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "137"
-            endingLineNumber = "137"
+            startingLineNumber = "158"
+            endingLineNumber = "158"
             landmarkName = "BlocksLayer::createSegment (double r_internal, double r_externa, double begin, double end, bool especial)"
             landmarkType = "7">
          </BreakpointContent>