Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool getRoomPlacement()
- {
- // Cast a ray from screen mouse position to 3d world space
- Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit cameraRayHit;
- // Check if the casted ray collides only floor, then highlight the rectangular area
- if(Physics.Raycast(cameraRay, out cameraRayHit, 1000, 1 << 8))
- {
- // Move the hover tile to collison point
- Vector3 hitPoint = cameraRayHit.point;
- Vector2 mouseOverCell = getTilePoints(hitPoint);
- // Calculate x and y distances, cell corner position
- int xDist;
- int yDist;
- xDist = (int)(mouseOverCell.x-selectedCell.x);
- yDist = (int)(mouseOverCell.y-selectedCell.y);
- RectanglePosition selectedCellPos = getRectPosition(xDist, yDist);
- // Position the hover tile
- if(selectedCellPos == RectanglePosition.LeftBottom)
- {
- hoverTile.transform.position = new Vector3 (selectedCell.x, 0F, selectedCell.y);
- }
- else if(selectedCellPos == RectanglePosition.RightBottom)
- {
- hoverTile.transform.position = new Vector3 (selectedCell.x+(float)xDist, 0F, selectedCell.y);
- }
- else if(selectedCellPos == RectanglePosition.LeftTop)
- {
- hoverTile.transform.position = new Vector3 (selectedCell.x, 0F, selectedCell.y+(float)yDist);
- }
- else
- {
- hoverTile.transform.position = new Vector3 (selectedCell.x+(float)xDist, 0F, selectedCell.y+(float)yDist);
- }
- // Scale the hover tile
- float xD = Mathf.Abs((float)xDist)+1F;
- float yD = Mathf.Abs((float)yDist)+1F;
- hoverTile.transform.localScale = new Vector3(xD,1F,yD);
- // Convert left bottom in vector two
- Vector2 leftBottomPosition = getTilePoints(hoverTile.transform.position);
- // Check if room purchase is valid
- if(isBlockSumZero(leftBottomPosition, xD, yD))
- {
- // Change color of hover tile to green
- hoverTileRenderer.material.SetColor("_Color", new Color(0F, 1.0F, 0F, 0.25F));
- // If mouse click, build the room
- if(Input.GetMouseButtonUp(0))
- {
- // If room size is minimum 2x2
- if(xD >= 2f && yD>=2f)
- {
- Vector2 lt = new Vector2(leftBottomPosition.x,leftBottomPosition.y+yD);
- Vector2 rb = new Vector2(leftBottomPosition.x+xD,leftBottomPosition.y);
- Vector2 rt = new Vector2(leftBottomPosition.x+xD,leftBottomPosition.y+yD);
- Room room = new Room(selectedRoomType,leftBottomPosition,lt,rb,rt,horizontalWall,verticalWall,transform);
- rooms.Add(room);
- // Update the floor map and room map
- setBlockValueFloorMap(leftBottomPosition, xD, yD, 2);
- setBlockValueRoomMap(leftBottomPosition, xD, yD, room.roomId);
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- // Change color of hover tile to red
- hoverTileRenderer.material.SetColor("_Color", new Color(1.0F, 0F, 0F, 0.25F));
- return false;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement