bool BubbleGrid::checkForCollision(Bubble* playerBubble)
{
	bool collided = false;
	int collidedX = 0;
	int collidedY = 0;

	CCPoint playerBubblePosition = playerBubble->getPosition();

	// Breadth first Search through the array starting from the bottom left
	for(int y = GRID_HEIGHT - 1; y >= 0; y--)
	{
		if(collided)
			break;

		int maxWidth = GRID_WIDTH;
		if(y % 2 != 0) // odd rows have GRID_WIDTH -1 bubbles
		{
			maxWidth -= 1;
		}

		for(int x = 0; x < maxWidth; x++)
		{
			Bubble* bubble = m_pBubbles[x][y];

			if((y != 0 && bubble->getType() == EMPTY) || bubble->getFalling())
				continue;

			// NOT BUBBLE_RADIUS * 2 because it would be two accurate - allows better gameplay
			if(ccpDistance(bubble->getPosition(), playerBubblePosition) < (BUBBLE_RADIUS * 1.5f)) 
			{
				collidedX = x;
				collidedY = y;
				collided = true;
				break;
			}
		}
	}

	if(collided)
	{
		int selectedIndexX = 0;
		int selectedIndexY = 0;

		// Find a place to put the bubble
		// selectedIndexX & selectedIndexY hold the selected position
		placeInClosestEmptySlot(playerBubble, collidedX, collidedY, selectedIndexX, selectedIndexY);

		int count = 0;
		// check for matching bubbles
		checkMatchingBubbles(selectedIndexX, selectedIndexY, playerBubble->getType(), count);

		// If we found 3 or more matches
		if(count > 2)
		{
			// Mark all connected bubbles starting from [0][0]
			checkConnectedBubbles();

			// Drop any bubbles that are not marked
			dropBubbles();
		}

		return true;
	}

	return false;
}
Exemple #2
0
void GameBoard::drawSights(float rad){
    CCArray * points = CCArray::create();
    float startX = currentBubble->x;
    float startY = currentBubble->y;
    points->addObject(new CCPoint(startX, startY));
    
    float step = 30;
    float dx = step * sin(rad);
    float dy = step * cos(rad);
    
    CCPoint * p;
    bool available;
    do {
        p = new CCPoint(startX + dx, startY + dy);
        if (p->x >= BUBBLE_RIGHT_EAGE) {
            dx *= -1;
            p->x = 2*BUBBLE_RIGHT_EAGE-p->x;
            if(p->x+Bubble::RADIUS >= BUBBLE_RIGHT_EAGE) p->x = BUBBLE_RIGHT_EAGE-Bubble::RADIUS;
        }
        if (p->x <= BUBBLE_LEFT_EAGE) {
            dx *= -1;
            p->x = BUBBLE_LEFT_EAGE - p->x;
            if(p->x+Bubble::RADIUS >= BUBBLE_LEFT_EAGE) p->x = BUBBLE_LEFT_EAGE + Bubble::RADIUS;
        }
        
        startX = p->x;
        startY = p->y;
        available = true;
        Bubble * bubble;
        CCPoint bubblePos;
        float dis;
        
        if (p->y >= BUBBLE_TOP_EAGE) {
            p->y = BUBBLE_TOP_EAGE - Bubble::RADIUS;
            available = false;
        }
        
        if(available){
            for (int i=0; i<bubblesData->count(); i++) {
                bubble = (Bubble*) bubblesData->objectAtIndex(bubblesData->count() - 1 - i);
                bubblePos = bubble->getPosition();
                dis = sqrtf(powf(bubblePos.x - p->x, 2)+powf(bubblePos.y - p->y,2));
                if(dis <= Bubble::RADIUS){
                    available = false;
                    break;
                }
            }
        }
        
        if(available)   points->addObject(p);
        
    } while (available);
    
    CCPoint *pp;
    CCSprite *sp;
    CCTexture2D *pTexture2D  = sightsBatch->getTexture();
    sightsBatch->removeAllChildrenWithCleanup(true);
    for (int i=0; i<points->count(); i++) {
        pp = (CCPoint*) points->objectAtIndex(i);
        sp = CCSprite::createWithTexture(pTexture2D);
        sp->setScale(0.2);
        sp->setPosition(CCPointMake(pp->x, pp->y));
        sightsBatch->addChild(sp);
    }
}