/// <summary> /// Calculate what cell a position belongs to. /// </summary> /// <param name="position">The position to find the cell for.</param> /// <returns>The cell the position belongs to.</returns> private Point CalculateCell(Vector2 position) { // First subtract the offset from the position, then divide this 0-based position by ball height. float row = position.Y - mOffset.Y; row /= Ball.Size.Y; // Divide by 0.85 (the rows overlap slightly) and use integer part row = (float)Math.Floor(row / 0.85f); // Make sure the row is between 0 and the last row + 1 (a new row may be added) row = MathHelper.Clamp(row, 0, mBalls.Count); // Find out what type this row is (for x-offset) BoardRow.Type rowType = CalculateRowType((int)row); // Subtract the board offset from position (want to translate the position to be based on (0, 0) float column = position.X - mOffset.X; // If the row is further offset (RowType.Half = 1), subtract this offset as well column -= ((int)rowType) * Ball.Size.X * 0.5f; // Divide by width, then use only integer part column /= Ball.Size.X; column = (float)Math.Floor(column); // Make sure the column is a valid one column = MathHelper.Clamp(column, 0, mColumns[(int)rowType] - 1); return(new Point((int)column, (int)row)); }
/// <summary> /// Get the row type for an arbitrary row from index 0 to the number of rows plus one (a new row). /// </summary> /// <param name="row">The row for which the type is requested.</param> /// <returns>The typ for the requested row.</returns> private BoardRow.Type CalculateRowType(int row) { // Initialize the type to Whole (to use if there are no rows) BoardRow.Type rowType = BoardRow.Type.Whole; // If there are rows and row is an existing index, get the rowtype, else get next bottom type if (mBalls.Count > 0 && row < mBalls.Count) { rowType = mBalls[row].RowType; } else if (row == mBalls.Count) { rowType = NextTypeBottom; } return(rowType); }