Advertisement
Guest User

Room.cs

a guest
Oct 5th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Room
  4. {
  5.     public int xPos;                      // The x coordinate of the lower left tile of the room.
  6.     public int yPos;                      // The y coordinate of the lower left tile of the room.
  7.     public int roomWidth;                     // How many tiles wide the room is.
  8.     public int roomHeight;                    // How many tiles high the room is.
  9.     public Direction enteringCorridor;    // The direction of the corridor that is entering this room.
  10.  
  11.  
  12.     // This is used for the first room.  It does not have a Corridor parameter since there are no corridors yet.
  13.     public void SetupRoom (IntRange widthRange, IntRange heightRange, int columns, int rows)
  14.     {
  15.         // Set a random width and height.
  16.         roomWidth = widthRange.Random;
  17.         roomHeight = heightRange.Random;
  18.  
  19.         // Set the x and y coordinates so the room is roughly in the middle of the board.
  20.         xPos = Mathf.RoundToInt(columns / 2f - roomWidth / 2f);
  21.         yPos = Mathf.RoundToInt(rows / 2f - roomHeight / 2f);
  22.     }
  23.  
  24.  
  25.     // This is an overload of the SetupRoom function and has a corridor parameter that represents the corridor entering the room.
  26.     public void SetupRoom (IntRange widthRange, IntRange heightRange, int columns, int rows, Corridor corridor)
  27.     {
  28.         // Set the entering corridor direction.
  29.         enteringCorridor = corridor.direction;
  30.  
  31.         // Set random values for width and height.
  32.         roomWidth = widthRange.Random;
  33.         roomHeight = heightRange.Random;
  34.  
  35.         switch (corridor.direction)
  36.         {
  37.             // If the corridor entering this room is going north...
  38.             case Direction.North:
  39.                 // ... the height of the room mustn't go beyond the board so it must be clamped based
  40.                 // on the height of the board (rows) and the end of corridor that leads to the room.
  41.                 roomHeight = Mathf.Clamp(roomHeight, 1, rows - corridor.EndPositionY);
  42.  
  43.                 // The y coordinate of the room must be at the end of the corridor (since the corridor leads to the bottom of the room).
  44.                 yPos = corridor.EndPositionY;
  45.            
  46.                 // The x coordinate can be random but the left-most possibility is no further than the width
  47.                 // and the right-most possibility is that the end of the corridor is at the position of the room.
  48.                 xPos = Random.Range (corridor.EndPositionX - roomWidth + 1, corridor.EndPositionX);
  49.  
  50.                 // This must be clamped to ensure that the room doesn't go off the board.
  51.                 xPos = Mathf.Clamp (xPos, 0, columns - roomWidth);
  52.                 break;
  53.             case Direction.East:
  54.                 roomWidth = Mathf.Clamp(roomWidth, 1, columns - corridor.EndPositionX);
  55.                 xPos = corridor.EndPositionX;
  56.  
  57.                 yPos = Random.Range (corridor.EndPositionY - roomHeight + 1, corridor.EndPositionY);
  58.                 yPos = Mathf.Clamp (yPos, 0, rows - roomHeight);
  59.                 break;
  60.             case Direction.South:
  61.                 roomHeight = Mathf.Clamp (roomHeight, 1, corridor.EndPositionY);
  62.                 yPos = corridor.EndPositionY - roomHeight + 1;
  63.  
  64.                 xPos = Random.Range (corridor.EndPositionX - roomWidth + 1, corridor.EndPositionX);
  65.                 xPos = Mathf.Clamp (xPos, 0, columns - roomWidth);
  66.                 break;
  67.             case Direction.West:
  68.                 roomWidth = Mathf.Clamp (roomWidth, 1, corridor.EndPositionX);
  69.                 xPos = corridor.EndPositionX - roomWidth + 1;
  70.  
  71.                 yPos = Random.Range (corridor.EndPositionY - roomHeight + 1, corridor.EndPositionY);
  72.                 yPos = Mathf.Clamp (yPos, 0, rows - roomHeight);
  73.                 break;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement