void GameScene::step(float dt)
{
//	CCLog("Game::step");

	// Return if game suspended
	if(gameSuspended) return;

	// Get the bird sprite
	CCSprite *bird = (CCSprite*)getChildByTag(kBird);
	
	// Update the player x position based on velocity and delta time
	bird_pos.x += bird_vel.x * dt;
	
	// Flip the player based on it's x velocity and current looking direction
	if(bird_vel.x < -30.0f && birdLookingRight) 
	{
		birdLookingRight = false;
		bird->setScaleX(-1.0f);
	}
	else if (bird_vel.x > 30.0f && !birdLookingRight) 
	{
		birdLookingRight = true;
		bird->setScaleX(1.0f);
	}

	// Calculate the max and min x values for the bird
	// based on the screen and bird widths
	CCSize bird_size = bird->getContentSize();
	float max_x = (float)CCDirector::sharedDirector()->getWinSize().width - bird_size.width/2;
	float min_x = bird_size.width/2;
	
	// Limit the bird position based on max and min values allowed
	if(bird_pos.x>max_x) bird_pos.x = max_x;
	if(bird_pos.x<min_x) bird_pos.x = min_x;

	// Update the bird velocity based on acceleration and time
	bird_vel.y += bird_acc.y * dt;

	// Update the bird y positin based on velocity and time
	bird_pos.y += bird_vel.y * dt;
	
	////////////////////////////////////////////////////////////////////////////
	// Handle the bonus scoring
	CCSprite *bonus = (CCSprite*)getChildByTag(kBonusStartTag+currentBonusType);

	// If bonus is visible then see if the bird is within range to get the bonus
	if(bonus->isVisible() )
	{
		CCPoint bonus_pos = bonus->getPosition();
		float range = 20.0f;

		// If the player is within range of the bonus value then award the prize
		if(bird_pos.x > bonus_pos.x - range &&
		   bird_pos.x < bonus_pos.x + range &&
		   bird_pos.y > bonus_pos.y - range &&
		   bird_pos.y < bonus_pos.y + range ) 
		{
			// Update score based on bonus
			switch(currentBonusType) 
			{
				case kBonus5:   score += 5000;   break;
				case kBonus10:  score += 10000;  break;
				case kBonus50:  score += 50000;  break;
				case kBonus100: score += 100000; break;
			}

			// Build the score string to display
			char scoreStr[10] = {0};
			sprintf(scoreStr, "%d", score);
			CCLabelBMFont* scoreLabel = (CCLabelBMFont*) getChildByTag(kScoreLabel);
			scoreLabel->setString(scoreStr);

			// Highlight the score with some actions to celebrate the bonus win
			CCActionInterval* a1 = CCScaleTo::create(0.2f, 1.5f, 0.8f);
			CCActionInterval* a2 = CCScaleTo::create(0.2f, 1.0f, 1.0f);
			scoreLabel->runAction(CCSequence::create(a1, a2, a1, a2, a1, a2, NULL));

			// Reset the bonus to another platform
			resetBonus();
		}
	}

	// If the bird has stopped moving then make it jump from the platform it is on
	int t;
	if(bird_vel.y < 0) 
	{
		t = kPlatformsStartTag;

		// Search through all the platforms and compare the birds position with the platfor position
		for(t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);

			CCSize platform_size = platform->getContentSize();
			CCPoint platform_pos = platform->getPosition();
			
			max_x = platform_pos.x - platform_size.width/2 - 10;
			min_x = platform_pos.x + platform_size.width/2 + 10;

			float min_y = platform_pos.y + (platform_size.height+bird_size.height)/2 - kPlatformTopPadding;
			
			if(bird_pos.x > max_x &&
			   bird_pos.x < min_x &&
			   bird_pos.y > platform_pos.y &&
			   bird_pos.y < min_y) 
			{
				jump();
				break;	// Can only jump from one platform at a time to break out of the loop
			}
		}
	
		// If the bird has fallen below the screen then game over
		if(bird_pos.y < - bird_size.height/2) 
		{
			// [self showHighscores];   <== NEED TO IMPLEMENT THE HIGHTSCORE
			resetBird();	// Highscore not implmented yet so just keep on going.
		}
	} 
	else if ( bird_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2)) 
	{
		// If bird position is greater than the middle of the screen then move the platforms
		// the difference between the bird y position and middle point of the screen
		float delta = bird_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);

		// Set the bird y position to the middle of the screen
		bird_pos.y = (float)CCDirector::sharedDirector()->getWinSize().height / 2;

		// Move the current platform y by the delta amount
		currentPlatformY -= delta;

		// Move the clouds vertically and reset if necessary
		t = kCloudsStartTag;
		for (t; t < kCloudsStartTag + kNumClouds; t++) 
		{
			CCSprite *cloud = (CCSprite*) getChildByTag(t);

			CCPoint pos = cloud->getPosition();

			// Calculate new position for cloud
			pos.y -= delta * cloud->getScaleY() * 0.8f;

			// If the cloud is off the screen then need to reset this cloud else set its new position
			if (pos.y < -cloud->getContentSize().height/2) 
			{
				currentCloudTag = t;
				resetCloud();
			} 
			else 
			{	// Update the new y position for the cloud.
				cloud->setPosition(pos);
			}
		}

		// Move the platforms vertically and reset if necessary
		t = kPlatformsStartTag;
		for (t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);
			
			CCPoint pos = platform->getPosition();

			// Calculate new position for platform
			pos = ccp(pos.x, pos.y - delta);

			// If the platform is off the screen then reset the platform else set its new position
			if(pos.y < - platform->getContentSize().height/2) 
			{
				currentPlatformTag = t;
				resetPlatform();
			} 
			else 
			{
				platform->setPosition(pos);
			}
		}

		// If the bonus is visible then adjust it's y position
		if(bonus->isVisible()) 
		{
			CCPoint pos = bonus->getPosition();

			// Calculate new position of bonus
			pos.y -= delta;
			
			// If the bonus is off the screen then reset the bonus else set its new position
			if(pos.y < -bonus->getContentSize().height/2 ) 
			{
				resetBonus();
			} 
			else 
			{
				bonus->setPosition(pos);
			}
		}
		
		// Update score based on how much the bird has moved
		score += (int)delta;

		// Display the new score value
		char scoreStr[10] = {0};
		sprintf(scoreStr, "%d", score);
		CCLabelBMFont* scoreLabel = (CCLabelBMFont*) getChildByTag(kScoreLabel);
		scoreLabel->setString(scoreStr);
	}

	// Set the birds position
	bird->setPosition(bird_pos);
}
Exemple #2
0
void GameScene::step(float dt)
{
    
    particles_counter += dt;
    if (particles_counter > 0.15) {
        particles->setEmissionRate(0);
    }
    
	// Return if game suspended
	if(gameSuspended) return;

	// Get the jumper sprite
	CCSprite *jumper = (CCSprite*)getChildByTag(kjumper);
	
	// Update the player x position based on velocity and delta time
	jumper_pos.x += jumper_vel.x * dt;
	
	jumper->setRotation(jumper_vel.x);

	// Flip the player based on it's x velocity and current looking direction
	if(jumper_vel.x < -30.0f && jumperLookingRight) 
	{
		jumperLookingRight = false;
		jumper->setScaleX(-1.0f);
	}
	else if (jumper_vel.x > 30.0f && !jumperLookingRight) 
	{
		jumperLookingRight = true;
		jumper->setScaleX(1.0f);
	}

	// Calculate the max and min x values for the jumper
	// based on the screen and jumper widths
	CCSize jumper_size = jumper->getContentSize();
//	float max_x = (float)CCDirector::sharedDirector()->getWinSize().width - jumper_size.width/2;
//	float min_x = jumper_size.width/2;
	
	// Limit the jumper position based on max and min values allowed
//	if(jumper_pos.x>max_x) jumper_pos.x = max_x;
//	if(jumper_pos.x<min_x) jumper_pos.x = min_x;
    
    float max_x = (float)CCDirector::sharedDirector()->getWinSize().width + jumper_size.width/2.0;
	float min_x = -jumper_size.width/2.0;
    
    
	// Limit the character position based on max and min values allowed
	if(jumper_pos.x>max_x)
        jumper_pos.x = jumper_size.width/2.0;
	if(jumper_pos.x<min_x)
        jumper_pos.x = (float)CCDirector::sharedDirector()->getWinSize().width - jumper_size.width/2.0;

	// Update the jumper velocity based on acceleration and time
//    if (isPowerPicked)
//    {
//        jumper_vel.y += (jumper_acc.y * dt)*3;
//    }
//    else
    {
      jumper_vel.y += jumper_acc.y * dt;
    }
	

	// Update the jumper y positin based on velocity and time
	jumper_pos.y += jumper_vel.y * dt;
	
	////////////////////////////////////////////////////////////////////////////
	// Handle the bonus scoring
    
    
    
    float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
    

	// If bonus is visible then see if the jumper is within range to get the bonus
	

	// If the jumper has stopped moving then make it jump from the platform it is on
	int t;
	if(jumper_vel.y < 0) 
	{
		t = kPlatformsStartTag;

		// Search through all the platforms and compare the jumpers position with the platfor position
        
		for(t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);

			CCSize platform_size = platform->getContentSize();
			CCPoint platform_pos = platform->getPosition();
			
			max_x = platform_pos.x - platform_size.width/2 - 2;
			min_x = platform_pos.x + platform_size.width/2 + 2;

			float min_y = platform_pos.y + (platform_size.height+jumper_size.height)/2 - kPlatformTopPadding;
			
			if(jumper_pos.x > (max_x) &&
			   jumper_pos.x < (min_x) &&
			   jumper_pos.y > platform_pos.y &&
			   jumper_pos.y < min_y) 
			{
                if (platform->getTag() == kKillerPlatformTag)
                {
                    coin50Platform = 12;
                    coin100Platform = 15;
                    coinJumpPlatform = 25;
                    SharedData::getSharedInstance()->gameover_Text = "Gameover";
                    CCScene *pScene = GameOverScene::scene();
                    CCDirector::sharedDirector()->pushScene(pScene);
                    this->unscheduleAllSelectors();
                }
                else
                {
				jump();
				break;	// Can only jump from one platform at a time to break out of the loop
                }
			}
		}
	
		// If the jumper has fallen below the screen then game over
		if(jumper_pos.y < - jumper_size.height/2) 
		{
            coin50Platform = 12;
            coin100Platform = 15;
            coinJumpPlatform = 25;
            SharedData::getSharedInstance()->gameover_Text = "Gameover";
            CCScene *pScene = GameOverScene::scene();
            CCDirector::sharedDirector()->pushScene(pScene);
            this->unscheduleAllSelectors();
            
//			resetjumper();
		}
	} 
	else if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2)) 
	{
        
		// If jumper position is greater than the middle of the screen then move the platforms
		// the difference between the jumper y position and middle point of the screen
		
        resetTree();
		// Set the jumper y position to the middle of the screen
		jumper_pos.y = (float)CCDirector::sharedDirector()->getWinSize().height / 2;

		// Move the current platform y by the delta amount
		currentPlatformY -= delta;

		// Move the clouds vertically and reset if necessary
		t = kCloudsStartTag;
		for (t; t < kCloudsStartTag + kNumClouds; t++) 
		{
			CCSprite *cloud = (CCSprite*) getChildByTag(t);

			CCPoint pos = cloud->getPosition();

			// Calculate new position for cloud
			pos.y -= delta * cloud->getScaleY() * 0.8f;

			// If the cloud is off the screen then need to reset this cloud else set its new position
			if (pos.y < -cloud->getContentSize().height/2) 
			{
				currentCloudTag = t;
				resetCloud();
			} 
			else 
			{	// Update the new y position for the cloud.
				cloud->setPosition(pos);
			}
		}

		// Move the platforms vertically and reset if necessary
		t = kPlatformsStartTag;
        int t_coin = kBonusStartTag;
		for (t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);
			
			CCPoint pos = platform->getPosition();

			// Calculate new position for platform
			pos = ccp(pos.x, pos.y - delta);

			// If the platform is off the screen then reset the platform else set its new position
			if(pos.y < - platform->getContentSize().height/2) 
			{
				currentPlatformTag = t;
				resetPlatform();
                
			} 
			else 
			{
				platform->setPosition(pos);
                
			}
		}

       
        //move particle system
        if (particles) {
            CCPoint pos = particles->getPosition();
            pos.y -= delta;
            particles->setPosition(pos);
        }
        
		// If the bonus is visible then adjust it's y position
		
		
		// Update score based on how much the jumper has moved
		score += (int)delta*0.3 ;
        
//        if (score >= target_Score)
//        {
//            SharedData::getSharedInstance()->gameover_Text = "Level Cleared";
//            CCScene *pScene = GameOverScene::scene();
//            CCDirector::sharedDirector()->pushScene(pScene);
//            this->unscheduleAllSelectors();
//        }

		// Display the new score value
		char scoreStr[10] = {0};
		sprintf(scoreStr, "%d", score);
		CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
		scoreLabel->setString(scoreStr);
        
        int t_coins = kBonusStartTag;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        t_coins = kBonusStartTag+30;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag+30); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        t_coins = kBonusStartTag+60;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag+60); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        
	}
    
    int t_coins = kBonusStartTag;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
        
                // Build the score string to display
                char scoreStr[10] = {0};
                score += 50;
                sprintf(scoreStr, "%d", score);
                CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
                scoreLabel->setString(scoreStr);
                coin->setVisible(false);
            }
        }
        
    }
    t_coins = kBonusStartTag+30;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag+30); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
                
                // Build the score string to display
                char scoreStr[10] = {0};
                score += 100;
                sprintf(scoreStr, "%d", score);
                CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
                scoreLabel->setString(scoreStr);
                coin->setVisible(false);
            }
        }
        
    }
    t_coins = kBonusStartTag+60;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag+60); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
                
                // Build the score string to display
                this->powerPicked();
                coin->setVisible(false);
            }
        }
        
    }
    
    for (int i = kTree1Tag; i <= kTree3Tag; i++)
    {
        float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
        CCSprite * tree = (CCSprite *)this->getChildByTag(i);
        if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2))
        {
            tree->setPosition(ccp(tree->getPosition().x,tree->getPosition().y-delta));
        }
        
       
        
        if (tree->getPosition().y <= -0.5*CCDirector::sharedDirector()->getWinSize().height )
        {
            float posY;
            if (i == kTree1Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree3Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree2Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree1Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree3Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree2Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            
        }
    }
    for (int i = kTree11Tag; i <= kTree33Tag; i++)
    {
        float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
        CCSprite * tree = (CCSprite *)this->getChildByTag(i);
        if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2))
        {
            tree->setPosition(ccp(tree->getPosition().x,tree->getPosition().y-delta*0.1));
        }
        
        
        
        if (tree->getPosition().y <= -0.5*CCDirector::sharedDirector()->getWinSize().height )
        {
            float posY;
            if (i == kTree11Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree33Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree22Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree11Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree33Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree22Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            
        }
    }

    
	// Set the jumpers position
	jumper->setPosition(jumper_pos);
}