public void FitTo(float halfWidth, float height, SideLR side) { float destX = side == SideLR.Left ? -(halfWidth + 1f) : halfWidth + 1f; transform.position = new Vector3(destX, 0f, 0f); transform.localScale = new Vector3(1f, 2f * height, 1f); }
// Creates a set of bricks + paddle on the left or right side of the table. // A brick set consists of: // - 'numRows' x 'numCols' bricks of type 'brickPrefab' // - 1 brick of type 'powerupBrickPrefab' in the center of the back row private GameObject LayBrickSetAndPaddle(SideLR side) { float dir = (side == SideLR.Left) ? -1f : 1f; // determine brick height by fitting to the parameters: // - # bricks, spacing between bricks, & spacing to table edge float edgeLoss = 2 * wallSpacing; float inBetweenLoss = (numCols - 1) * colSpacing; float availableHeight = backgroundHeight - edgeLoss - inBetweenLoss; float brickHeight = availableHeight / numCols; float placementX = (backgroundWidth / 2f - goalSpacing - brickWidth / 2f) * dir; // place brick set for (int i = 0; i < numRows; ++i) { float placementY = backgroundHeight / 2f - wallSpacing - brickHeight / 2f; for (int j = 0; j < numCols; ++j) { GameObject instance = Instantiate(brickPrefab); SetObjectSize2D(instance, brickWidth, brickHeight); // TODO: BoxCollider2D Vector3 position = instance.transform.position; position.x = placementX; position.y = placementY; instance.transform.position = position; placementY -= brickHeight + colSpacing; } placementX -= (brickWidth + rowSpacing) * dir; } // place paddle -> create + resize + move GameObject paddle = Instantiate(paddlePrefab); PaddleController pCtrl = paddle.GetComponent <PaddleController>(); SetObjectSize2D(paddle, pCtrl.width, pCtrl.height); Vector3 paddlePos = paddle.transform.position; float spacing = (paddleSpacing + pCtrl.width / 2f) * -1; paddlePos.x = placementX + (spacing + rowSpacing) * dir; paddle.transform.position = paddlePos; return(paddle); }