Beispiel #1
0
void MainScene::update(float dt)
{
	//srand((unsigned int)time(0));
	if(CCRANDOM_0_1() < 0.02)
	{
		int type = CCRANDOM_0_1()*7;
		std::string s = "0.png";
		s[0] = type+'0';
		Gem* gem = new Gem();
		CCSprite* sprt = CCSprite::createWithSpriteFrameName(s.c_str());
		int x = CCRANDOM_0_1()*size.width;
		int y = size.height+(size.height/(ROW+2))/2;
		float scale = CCRANDOM_0_1()*0.8+0.2;
		int speed = CCRANDOM_0_1()*5;
		sprt->setPosition(ccp(x, y));
		sprt->setScale(scale);
		node->addChild(sprt);
		gem->setSprite(sprt);
		gem->setType((GemType)type);
		gem->setSpeed(speed);
		fallingGems.push_back(gem);
	}
	
	for(int i = fallingGems.size()-1; i >= 0; i--)
	{
		Gem* gem = fallingGems.at(i);
		CCPoint pos = gem->getSprite()->getPosition();
		pos.y -= gem->getSpeed();
		gem->getSprite()->setPosition(pos);
		if(pos.y < -size.height/(ROW+2)/2)
		{
			node->removeChild(gem->getSprite(), true);
			vector<Gem*>::iterator it = fallingGems.begin()+i;
			fallingGems.erase(it);
		}
	}
}
Beispiel #2
0
// Handle falling gems
void Grid::gemsFalling(float dt)
{
    bool falling = false;
    for (int x = 0; x < Width; x++)
    {
        for (int y = Height - 1; y >= 0; y--)
        {
            Gem* g = Gems[y * Width + x];
            Gem* gem_below = Gems[(y + 1) * Width + x];
            if (g != 0 && gem_below == 0)
            {
                int old_x, old_y;
                screenToGrid((int)g->m_X, (int)g->m_Y, old_x, old_y);
                float dy = Gem::gemFallSpeed * g->m_ScaleY * dt;         // Scale fall speed to match different height displays
                if (dy > g->m_H - 1)
                    dy = g->m_H - 1;
                g->m_Y += dy;

                int new_x, new_y;
                screenToGrid((int)g->m_X, (int)g->m_Y, new_x, new_y);
                if (new_y == y)
                {
                    // Still in same block
                    falling = true;
                }
                else
                {
                    // Moved into new block below
                    if (Gems[new_y * Width + new_x] == 0)
                    {
                        // Remove from previous grid cell and insert into new empty cell below
                        Gems[old_y * Width + old_x] = 0;
                        Gems[new_y * Width + new_x] = g;
                        falling = true;
                        if (Gems[(new_y + 1) * Width + new_x] != 0)
                        {
                            // Position sprite because it may have gone over boundary
                            g->m_Y = (float)new_y * GemSize + GridOriginY;
                        }
                    }
                }
            }
        }
    }

    // Traverse removed gems and drop them back into the grid
    std::list<Gem*> removals;
    bool new_gem = false;
    if (falling == false)
    {
        if (MatchedGems.size() > 0)
        {
            Game* game = (Game*)g_pSceneManager->Find("game");
            falling = true;
            for (std::list<Gem*>::iterator it = MatchedGems.begin(); it != MatchedGems.end(); ++it)
            {
                Gem* g = *it;
                int x, y;
                screenToGrid((int)g->m_X, (int)g->m_Y, x, y);
                if (Gems[x] == 0)
                {
                    // Add new gem
                    Gems[x] = g;
                    int type = rand() % MAX_GEM_TYPES;
                    g->setType(type);
                    g->m_Y = (float)GridOriginY;

                    game->GetTweener().Tween(0.5f,
                                                FLOAT, &g->m_Alpha, 1.0f,
                                                EASING, Ease::sineOut,
                                                END);
                    removals.push_back(*it);
                    new_gem = true;
                }
            }
        }
    }
    // Cleanup removed gems in mathed gems list
    for (std::list<Gem*>::iterator it = removals.begin(); it != removals.end(); ++it)
        MatchedGems.remove(*it);

    if (new_gem == true)
    {
        g_pAudio->PlaySound("audio/gem_land.wav");
    }

    if (falling == false)
    {
        if (!checkMatches())
        {
            // Switch back to waiting for gem selection state
            Game* game = (Game*)g_pSceneManager->Find("game");
            game->changeGameState(waitingFirstGem);
        }
    }
}