Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. bool getRoomPlacement()
  2. {
  3. // Cast a ray from screen mouse position to 3d world space
  4. Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  5. RaycastHit cameraRayHit;
  6.  
  7. // Check if the casted ray collides only floor, then highlight the rectangular area
  8. if(Physics.Raycast(cameraRay, out cameraRayHit, 1000, 1 << 8))
  9. {
  10. // Move the hover tile to collison point
  11. Vector3 hitPoint = cameraRayHit.point;
  12. Vector2 mouseOverCell = getTilePoints(hitPoint);
  13.  
  14. // Calculate x and y distances, cell corner position
  15. int xDist;
  16. int yDist;
  17. xDist = (int)(mouseOverCell.x-selectedCell.x);
  18. yDist = (int)(mouseOverCell.y-selectedCell.y);
  19. RectanglePosition selectedCellPos = getRectPosition(xDist, yDist);
  20.  
  21. // Position the hover tile
  22. if(selectedCellPos == RectanglePosition.LeftBottom)
  23. {
  24. hoverTile.transform.position = new Vector3 (selectedCell.x, 0F, selectedCell.y);
  25. }
  26. else if(selectedCellPos == RectanglePosition.RightBottom)
  27. {
  28. hoverTile.transform.position = new Vector3 (selectedCell.x+(float)xDist, 0F, selectedCell.y);
  29. }
  30. else if(selectedCellPos == RectanglePosition.LeftTop)
  31. {
  32. hoverTile.transform.position = new Vector3 (selectedCell.x, 0F, selectedCell.y+(float)yDist);
  33. }
  34. else
  35. {
  36. hoverTile.transform.position = new Vector3 (selectedCell.x+(float)xDist, 0F, selectedCell.y+(float)yDist);
  37. }
  38.  
  39. // Scale the hover tile
  40. float xD = Mathf.Abs((float)xDist)+1F;
  41. float yD = Mathf.Abs((float)yDist)+1F;
  42. hoverTile.transform.localScale = new Vector3(xD,1F,yD);
  43.  
  44. // Convert left bottom in vector two
  45. Vector2 leftBottomPosition = getTilePoints(hoverTile.transform.position);
  46.  
  47. // Check if room purchase is valid
  48. if(isBlockSumZero(leftBottomPosition, xD, yD))
  49. {
  50. // Change color of hover tile to green
  51. hoverTileRenderer.material.SetColor("_Color", new Color(0F, 1.0F, 0F, 0.25F));
  52.  
  53. // If mouse click, build the room
  54. if(Input.GetMouseButtonUp(0))
  55. {
  56. // If room size is minimum 2x2
  57. if(xD >= 2f && yD>=2f)
  58. {
  59. Vector2 lt = new Vector2(leftBottomPosition.x,leftBottomPosition.y+yD);
  60. Vector2 rb = new Vector2(leftBottomPosition.x+xD,leftBottomPosition.y);
  61. Vector2 rt = new Vector2(leftBottomPosition.x+xD,leftBottomPosition.y+yD);
  62. Room room = new Room(selectedRoomType,leftBottomPosition,lt,rb,rt,horizontalWall,verticalWall,transform);
  63. rooms.Add(room);
  64.  
  65. // Update the floor map and room map
  66. setBlockValueFloorMap(leftBottomPosition, xD, yD, 2);
  67. setBlockValueRoomMap(leftBottomPosition, xD, yD, room.roomId);
  68.  
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. // Change color of hover tile to red
  80. hoverTileRenderer.material.SetColor("_Color", new Color(1.0F, 0F, 0F, 0.25F));
  81. return false;
  82. }
  83. }
  84. return false;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement