//------------------------------------------------------------------------------ void Game::checkBorders() { checkLeft(); checkRight(); checkTop(); checkBottom(); }
bool BoundingBox::pointInBounds(int xp, int yp) { // Check if the point is in between the top bottom and side lines (all return 0 or negative) if(checkTop(xp,yp)<= 0 && checkBottom(xp,yp) <= 0 && checkLeft(xp,yp)<=0 && checkRight(xp,yp) <= 0) return true; return false; }
void BoundingBox::calculateResize(int xp, int yp,bool locked, int corner, double *ans) { double xn = 0, yn = 0, dh = 0, dw = 0, xO1 = 0, yO1 = 0; if(corner == CBL) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xO - yp + yO) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( pow((xO - xn),2) + pow((yO - yn),2) ); dw = sqrt( pow((xp - xn),2) + pow((yp - yn),2) ); // If the point is inside the box (negative value from checkLeft/Bottom) // The size is getting smaller if(checkLeft(xp, yp) < 0) dw = dw * -1; if(checkBottom(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // New origin is (xp,yp) xO1 = xp; yO1 = yp; } if(corner == CBR) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xBR - yp + yBR) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( (xBR - xn)*(xBR - xn) + (yBR - yn)*(yBR - yn) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // If the point is inside the right or bottom then the shape is getting smaller dw/dh should be negative if(checkRight(xp, yp) < 0) dw = dw * -1; if(checkBottom(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Calculate the new origin (xO1, yO1) xO1 = xO + dh * cos(angle - PI/2); // angle minus 90 degrees yO1 = yO + dh * sin(angle - PI/2); // angle minus 90 degrees } if(corner == CTR) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (mainSlope * xp - sideSlope * xTR - yp + yTR) / (mainSlope - sideSlope); yn = mainSlope * (xn - xp) + yp; // Calculate the change in width and height (dw, dh) dh = sqrt( (xTR - xn)*(xTR - xn) + (yTR - yn)*(yTR - yn) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // Retain the direction of the change // Acts opposite if angle is below the x axis if(checkRight(xp, yp) < 0)// && angle > 0 && angle < 180 || checkRight(xp, yp) > 0 && angle >180) dw = dw * -1; if(checkTop(xp, yp) < 0 )//&& angle > 0 && angle < 180) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Origin remains the same xO1 = xO; yO1 = yO; } if(corner == CTL) { // Find intersection (xn, yn) of two lines // bottom of element and perpendicular line going through (xp, yp) xn = (sideSlope * xTL - mainSlope * xp + yp - yTL) / (sideSlope - mainSlope); yn = yp - mainSlope * (xp - xn); // Calculate the change in width and height (dw, dh) dh = sqrt( (xn - xTL)*(xn - xTL) + (yn - yTL)*(yn - yTL) ); dw = sqrt( (xp - xn)*(xp - xn) + (yp - yn)*(yp - yn) ); // If the point is inside the box (negative value from checkLeft/Top) // The size is getting smaller if(checkLeft(xp, yp) < 0) dw = dw * -1; if(checkTop(xp, yp) < 0) dh = dh * -1; // if width/ height is negative invert the dw/dh if(width < 0) dw = dw*-1; if(height < 0) dh = dh*-1; // If the aspect ratio is locked choose the bigger change and scale the other appropriately if(locked) { if(fabs(dw) > fabs(dh)) { // Use dw, scale height proportionaly to find dh double scale = (double)(dw+width)/width; double h = height*scale; // The new dh dh = h - height; }else { double scale = (double)(dh+height)/height; double w = width*scale; // The new dw dw = w - width; } } // Calculate the new origin (xO1, yO1) xO1 = xO + dw * cos(angle + PI); // 180 plus angle yO1 = yO + dw * sin(angle + PI); // 180 plus angle } /// final new origin width and height ans[0] = xO1; ans[1] = yO1; ans[2] = width + dw; ans[3] = height + dh; }
//------------------------------------------------------------------------------ bool Game::update( float dt ) { const Controller & pad( Engine::instance()->getController() ); HGE * hge( Engine::hge() ); ViewPort * vp( Engine::vp() ); if ( Engine::instance()->isPaused() ) { return false; } if ( m_replaying ) { m_timer += dt; if ( m_timer < 0.2f ) { return false; } if ( m_row == 1 ) { if ( m_num == 0 ) { m_replaying = false; } else { --m_num; m_index = m_replay[m_num][0]; m_rotate = m_replay[m_num][1]; m_col = m_replay[m_num][3]; ++m_row; } } else { ++m_row; } } else if ( pad.isConnected() ) { if ( pad.buttonDown( XPAD_BUTTON_BUTTON_Y ) ) { m_replaying = true; m_row = 1; clearArena(); } if ( pad.buttonDown( XPAD_BUTTON_X ) ) { m_index = ( m_index + 6 ) % 7; } if ( pad.buttonDown( XPAD_BUTTON_B ) ) { m_index = ( m_index + 1 ) % 7; } if ( pad.buttonDown( XPAD_BUTTON_LEFT_SHOULDER ) ) { m_rotate = ( m_rotate + 3 ) % 4; } if ( pad.buttonDown( XPAD_BUTTON_RIGHT_SHOULDER ) ) { m_rotate = ( m_rotate + 1 ) % 4; } if ( pad.buttonDown( XPAD_BUTTON_DPAD_UP ) ) { m_row = ( m_row + 22 ) % 23; } if ( pad.buttonDown( XPAD_BUTTON_DPAD_DOWN ) ) { m_row = ( m_row + 1 ) % 23; } if ( pad.buttonDown( XPAD_BUTTON_DPAD_LEFT ) ) { m_col = ( m_col + 12 ) % 13; } if ( pad.buttonDown( XPAD_BUTTON_DPAD_RIGHT ) ) { m_col = ( m_col + 1 ) % 13; } } else { if ( Engine::hge()->Input_KeyDown( HGEK_R ) ) { m_replaying = true; m_row = 1; clearArena(); } if ( Engine::hge()->Input_KeyDown( HGEK_A ) ) { m_index = ( m_index + 6 ) % 7; } if ( Engine::hge()->Input_KeyDown( HGEK_D ) ) { m_index = ( m_index + 1 ) % 7; } if ( Engine::hge()->Input_KeyDown( HGEK_W ) ) { m_rotate = ( m_rotate + 3 ) % 4; } if ( Engine::hge()->Input_KeyDown( HGEK_S ) ) { m_rotate = ( m_rotate + 1 ) % 4; } if ( Engine::hge()->Input_KeyDown( HGEK_UP ) ) { m_row = ( m_row + 22 ) % 23; } if ( Engine::hge()->Input_KeyDown( HGEK_DOWN ) ) { m_row = ( m_row + 1 ) % 23; } if ( Engine::hge()->Input_KeyDown( HGEK_LEFT ) ) { m_col = ( m_col + 12 ) % 13; } if ( Engine::hge()->Input_KeyDown( HGEK_RIGHT ) ) { m_col = ( m_col + 1 ) % 13; } } clearPiece(); for ( int i = 0; i < 8; i += 2 ) { int x( PIECE[m_index][i] ), y( PIECE[m_index][i+1] + 1 ); for ( int j = 0; j < m_rotate; ++j ) { b2Swap( x, y ); y = 3 - y; } m_piece[x][y]= true; } for ( int x = 0; x < 10; ++x ) { for ( int y = 0; y < 20; ++y ) { m_buffer[x][y] = m_arena[x][y]; } } if ( m_replaying ) { if ( m_row == m_replay[m_num][2] ) { m_row = 1; for ( int y = 0; y < 4; ++y ) { for ( int x = 0; x < 4; ++x ) { if ( ! m_piece[x][y] ) { continue; } m_arena[m_col + x - 3][m_row + y - 3] = true; } } } return false; } checkBorders(); // If the piece has space beneath it, then fall down until we either hit the // bottom or there is something below us. while( ! checkBottom() && blankBelow() ) { m_row += 1; } // Add rows, starting at the bottom, to accomodate the piece addRows(); // If the user presses the button, check if the piece has nothing blocking // it above. If it doesn't, remove it. if ( pad.isConnected() ) { if ( pad.buttonDown( XPAD_BUTTON_A ) ) { removePiece(); } } else { if ( Engine::hge()->Input_KeyDown( HGEK_SPACE ) ) { removePiece(); } } return false; }