Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 85 additions & 6 deletions Assets/Scripts/ProcGen/BoardCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public enum TileType
Wall, Floor,
}


public int columns = 100; // The number of columns on the board (how wide it will be).
public int rows = 100; // The number of rows on the board (how tall it will be).
public int columns = 200; // The number of columns on the board (how wide it will be).
public int rows = 200; // The number of rows on the board (how tall it will be).
public IntRange numRooms = new IntRange(15, 20); // The range of the number of rooms there can be.
public IntRange roomWidth = new IntRange(3, 10); // The range of widths rooms can have.
public IntRange roomHeight = new IntRange(3, 10); // The range of heights rooms can have.
Expand All @@ -27,7 +26,9 @@ public enum TileType
private TileType[][] tiles; // A jagged array of tile types representing the board, like a grid.
private Room[] rooms; // All the rooms that are created for this board.
private Corridor[] corridors; // All the corridors that connect the rooms.
private Corridor[] aCorridors; // All the appending corridors that connects to the main corridor.
private GameObject boardHolder; // GameObject that acts as a container for all other tiles.
private int numAppend = 0;


private void Start()
Expand Down Expand Up @@ -68,7 +69,10 @@ void CreateRoomsAndCorridors()

// There should be one less corridor than there is rooms.
corridors = new Corridor[rooms.Length - 1];


// There will be a specified number of appending corridor
aCorridors = new Corridor[rooms.Length - 2];

// Create the first room and corridor.
rooms[0] = new Room();
corridors[0] = new Corridor();
Expand All @@ -91,12 +95,26 @@ void CreateRoomsAndCorridors()
// If room overlaps with any other rooms, create entirely new corridor leaving from the last created room
while (!goodRoomPlacement)
{
bool appendCorridor = false;

// Create test corridor and room
Corridor corridorToBePlaced = new Corridor();
Corridor corridorToAppend = new Corridor();

if (numAppend < rooms.Length - 1)
appendCorridor = true;

corridorToBePlaced.SetupCorridor (rooms [i-1], corridorLength, roomWidth, roomHeight, columns, rows, false);

Room roomToBePlaced = new Room ();
roomToBePlaced.SetupRoom (roomWidth, roomHeight, columns, rows, corridorToBePlaced);

if (appendCorridor)
{
corridorToAppend.appendCorridor (corridorToBePlaced, corridorLength, corridorToBePlaced.EndPositionX, corridorToBePlaced.EndPositionY, columns, rows);
roomToBePlaced.SetupRoom(roomWidth, roomHeight, columns, rows, corridorToAppend);
}
else
roomToBePlaced.SetupRoom (roomWidth, roomHeight, columns, rows, corridorToBePlaced);

// Loop over all other rooms created, except for one to be placed
for (int j = 0; j < i; j++)
Expand All @@ -121,6 +139,12 @@ void CreateRoomsAndCorridors()
if (goodRoomPlacement)
{
corridors [i - 1] = corridorToBePlaced;
if (appendCorridor)
{
aCorridors[numAppend] = corridorToAppend;
numAppend++;
}

rooms [i] = roomToBePlaced;

//Rolls the dice
Expand Down Expand Up @@ -190,6 +214,61 @@ void SetTilesValuesForCorridors()
{
Corridor currentCorridor = corridors[i];

// and go through it's length.
for (int j = 0; j < currentCorridor.corridorLength; j++)
{
// Start the coordinates at the start of the corridor.
int xCoord = currentCorridor.startXPos;
int yCoord = currentCorridor.startYPos;

// Depending on the direction, add or subtract from the appropriate
// coordinate based on how far through the length the loop is.
switch (currentCorridor.direction)
{
case Direction.North:
yCoord += j;
break;
case Direction.East:
xCoord += j;
break;
case Direction.South:
yCoord -= j;
break;
case Direction.West:
xCoord -= j;
break;
}

//Widens the corridor to set width
for ( int k = 0; k < currentCorridor.corridorWidth; k++) {
switch(currentCorridor.direction)
{
case Direction.North:
xCoord++;
break;
case Direction.East:
yCoord++;
break;
case Direction.South:
xCoord++;
break;
case Direction.West:
yCoord++;
break;

}
// Set the tile at these coordinates to Floor.
tiles[xCoord][yCoord] = TileType.Floor;
}

}
}

// Go through every corridor...
for (int i = 0; i < numAppend; i++)
{
Corridor currentCorridor = aCorridors[i];

// and go through it's length.
for (int j = 0; j < currentCorridor.corridorLength; j++)
{
Expand Down
76 changes: 76 additions & 0 deletions Assets/Scripts/ProcGen/Corridor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,80 @@ public void SetupCorridor(Room room, IntRange length, IntRange roomWidth, IntRan
// We clamp the length of the corridor to make sure it doesn't go off the board.
corridorLength = Mathf.Clamp(corridorLength, 1, maxLength);
}

public void appendCorridor(Corridor corridor, IntRange length, int xStart, int yStart, int columns, int rows)
{
// Set a random direction (a random index from 0 to 3, cast to Direction).
//First corridor cannot head West because it is in the West most room
direction = (Direction)Random.Range (0, 4);

// Find the direction opposite to the one entering the room this corridor is leaving from.
// Cast the previous corridor's direction to an int between 0 and 3 and add 2 (a number between 2 and 5).
// Find the remainder when dividing by 4 (if 2 then 2, if 3 then 3, if 4 then 0, if 5 then 1).
// Cast this number back to a direction.
// Overall effect is if the direction was South then that is 2, becomes 4, remainder is 0, which is north.
Direction oppositeDirection = (Direction)(((int)corridor.direction + 2) % 4);

// If this is north the first corridor and the randomly selected direction is opposite to the previous corridor's direction...
if (direction == oppositeDirection)
{
// Rotate the direction 90 degrees clockwise (North becomes East, East becomes South, etc).
// This is a more broken down version of the opposite direction operation above but instead of adding 2 we're adding 1.
// This means instead of rotating 180 (the opposite direction) we're rotating 90.
int directionInt = (int)direction;
directionInt++;
directionInt = directionInt % 4;
direction = (Direction)directionInt;
}

int maxLength = length.m_Max;

//int maxLength;

switch (direction)
{
case Direction.North:
startXPos = xStart - corridorWidth;
startYPos = yStart - corridorWidth + 1;
maxLength = rows - startYPos;
break;
case Direction.East:

if (oppositeDirection == Direction.North || oppositeDirection == Direction.South)
startXPos = xStart - corridorWidth + 1;
else
startXPos = xStart + 1;

startYPos = yStart - corridorWidth;
maxLength = columns - startXPos;
break;
case Direction.South:
startXPos = xStart - corridorWidth;
startYPos = yStart;
maxLength = startYPos;
break;
case Direction.West:
if (corridor.direction == Direction.North || corridor.direction == Direction.South)
startXPos = xStart;
else
startXPos = xStart + 1;

startYPos = yStart - corridorWidth;
maxLength = columns - startXPos;
break;
}

// Set a random length, ensure the length is greater than corridor width
// for proper corridor appending
do
{
corridorLength = length.Random;
}while(corridorLength <= corridorWidth + 1);

if(maxLength <= corridorLength + 1)
maxLength = corridorLength + 1;

// We clamp the length of the corridor to make sure it doesn't go off the board.
corridorLength = Mathf.Clamp(corridorLength, 1, maxLength);
}
}
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 5.3.2p3
m_EditorVersion: 5.3.2p1
m_StandardAssetsVersion: 0