private Corridor GenerateCorridor(MapPoint startingPoint, CorridorDirection direction) { var result = new Corridor(startingPoint, direction, Random.Range(minCorridorLength, maxCorridorLength)); corridors.Add(result); return(result); }
// Rotate the cell public void PointFromTo(CorridorDirection from, CorridorDirection to) { Debug.Log("FROM:" + from + " TO:" + to); Debug.Log ("STUFF: " + CorridorDirection.North); Debug.Log ("walls[0]: " + walls [0]); Debug.Log ("walls[1]: " + walls [1]); Debug.Log ("walls[2]: " + walls [2]); Debug.Log ("walls[3]: " + walls [3]); if (from == CorridorDirection.North) { walls [1].SetActive (false); } else if (from == CorridorDirection.South) { walls [0].SetActive (false); } else if (from == CorridorDirection.East) { walls [3].SetActive (false); } else if (from == CorridorDirection.West) { walls [2].SetActive (false); } if (to == CorridorDirection.North) { walls [0].SetActive (false); } else if (to == CorridorDirection.South) { walls [1].SetActive (false); } else if (to == CorridorDirection.East) { walls [2].SetActive (false); } else if (to == CorridorDirection.West) { walls [3].SetActive (false); } }
// Method which sets up a corridor public void SetupCorridor(Dungeon_Room room, int minCorridorLength, int maxCorridorLength, int roomWidth, int roomHeight, int columns, int rows, bool isFirstCorridor) { // Set a direction for the corridor, a random int cast to a CorridorDirection direction = (CorridorDirection)Random.Range(0, 4); // This calculates the opposite direction from the corridor that enters the room. CorridorDirection oppositeDirection = (CorridorDirection)(((int)room.enteringCorridor + 2) % 4); // Only the direction of the first corridor has to be checked if (isFirstCorridor) { CheckFirstCorridorDirection(direction); } if (!isFirstCorridor && direction == oppositeDirection) { // It can't be the first corridor since it doesn't have an opposite direction // To make it so all the rooms are not connected by corridors all going the same way // This rotates the corridor 90 degrees instead of 180 degrees. int intDirection = (int)direction; intDirection = (intDirection + 1) % 4; direction = (CorridorDirection)intDirection; } // Get a random length for the corridor corridorLength = Random.Range(minCorridorLength, maxCorridorLength + 1); // Make a variable which caps the maximum length of the corridor. This is necessary for making sure the corridor won't be able to exit the maximum size of the dungeon int maxLength = maxCorridorLength; if (direction == CorridorDirection.Up) { startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth - 1); // Get a random position along the X-axis startZPos = room.zPos + room.roomHeight; // The Z position must be at the top of the room maxLength = rows - startZPos - room.roomHeight; // This makes sure the corridor does not exit the dungeon on the top side } else if (direction == CorridorDirection.Right) { startXPos = room.xPos + room.roomWidth; startZPos = Random.Range(room.zPos, room.zPos + room.roomHeight - 1); maxLength = columns - startXPos - room.roomWidth; } else if (direction == CorridorDirection.Down) { startXPos = Random.Range(room.xPos, room.xPos + room.roomWidth); startZPos = room.zPos; maxLength = startZPos - room.roomHeight; } else if (direction == CorridorDirection.Left) { startXPos = room.xPos; startZPos = Random.Range(room.zPos, room.zPos + room.roomHeight); maxLength = startXPos - room.roomWidth; } // Finally, clamp the corridorlength using the maxLength calculated just before this corridorLength = Mathf.Clamp(corridorLength, 1, maxLength); }
} // getcell // Generate the corridor public void Generate() { cells = new CorridorCell[size.x, size.z]; bool turned = false, turnedRecently = false; // Current coordinates, initialised with -1, -1 IntVector2 currentCoordinates; // Coordinates to generate the next cell on IntVector2 nextCoordinates = RandomCoordinates; // The direction to generate the next cell to CorridorDirection nextDirection = CorridorDirections.RandomValue; // Direction of the current cell CorridorDirection currentDirection = nextDirection; CreateFirstCell(nextCoordinates, nextDirection); currentCoordinates = nextCoordinates; currentDirection = nextDirection; // Generate the cells while (true) { turned = false; // Check if change direction or not if (Random.Range(0, 100) < cornerProbability && !turnedRecently) { // Try random direction. If wrong, try until right nextDirection = CorridorDirections.RandomValue; while (nextDirection == CorridorDirections.OppositeOf(currentDirection)) { nextDirection = CorridorDirections.RandomValue; } // while turned = true; } // if else { nextDirection = currentDirection; } turnedRecently = turned; nextCoordinates += currentDirection.ToIntVector2(); if (!(ContainsCoordinates(nextCoordinates) && GetCell(nextCoordinates) == null)) { break; } CreateCell(nextCoordinates, currentDirection, nextDirection); cellList.Add(GetCell(currentCoordinates)); currentDirection = nextDirection; currentCoordinates = nextCoordinates; } // while // Spawn the npcs if (GameManager.instance.enemies) { corridorSpawnNPCs(); } } // generate
// If the first corridor is going down or to the left, meaning directly into the wall, choose another direction void CheckFirstCorridorDirection(CorridorDirection corridorDirection) { if (corridorDirection == CorridorDirection.Left || corridorDirection == CorridorDirection.Down) { corridorDirection = (CorridorDirection)Random.Range(0, 4); direction = corridorDirection; // Do this again untill the direction is up or to the right CheckFirstCorridorDirection(corridorDirection); } }
} // PointTo // Rotate the cell public void PointFromTo(CorridorDirection from, CorridorDirection to) { if (from == CorridorDirection.North) { walls [1].SetActive(false); } else if (from == CorridorDirection.South) { walls [0].SetActive(false); } else if (from == CorridorDirection.East) { walls [3].SetActive(false); } else if (from == CorridorDirection.West) { walls [2].SetActive(false); } if (to == CorridorDirection.North) { walls [0].SetActive(false); } else if (to == CorridorDirection.South) { walls [1].SetActive(false); } else if (to == CorridorDirection.East) { walls [2].SetActive(false); } else if (to == CorridorDirection.West) { walls [3].SetActive(false); } // Generate the door foreach (GameObject wall in walls) { if (wall.activeSelf) { if (Random.Range(0, 100) < wallDoorProbability) { // Generate new door wall.GetComponent <WallControl>().door = true; doorInstance = Instantiate(doorPrefab) as GameObject; doorInstance.transform.SetParent(wall.transform); doorInstance.transform.localPosition = new Vector3(0f, 0f, -0.05f); doorInstance.transform.localRotation = Quaternion.Euler(new Vector3(0f, 180f, 0f)); doorInstance.transform.localScale = new Vector3(0.1f, 0.1f, 0.3f); //wall.GetComponent<MeshRenderer>().material.color = Color.blue; } } // if } // foreach } // point from to
// This is an overload of the SetupRoom function and has a corridor parameter that represents the corridor entering the room. public void SetupRoom(int level, DungeonSettings gameInit, int columns, int rows, Corridor corridor) { // Set the entering corridor direction. enteringCorridor = corridor.direction; // Set random values for width and height. dim.x = gameInit.roomWidth.Random; dim.y = gameInit.roomHeight.Random; switch (corridor.direction) { // If the corridor entering this room is going north... case CorridorDirection.North: // ... the height of the room mustn't go beyond the board so it must be clamped based // on the height of the board (rows) and the end of corridor that leads to the room. dim.y = Mathf.Clamp(dim.y, 1, rows - corridor.EndPositionY); // The y coordinate of the room must be at the end of the corridor (since the corridor leads to the bottom of the room). pos.y = corridor.EndPositionY; // The x coordinate can be random but the left-most possibility is no further than the width // and the right-most possibility is that the end of the corridor is at the position of the room. pos.x = Random.Range(corridor.EndPositionX - dim.x + 1, corridor.EndPositionX); // This must be clamped to ensure that the room doesn't go off the board. pos.x = Mathf.Clamp(pos.x, 0, columns - dim.x); break; case CorridorDirection.East: dim.x = Mathf.Clamp(dim.x, 1, columns - corridor.EndPositionX); pos.x = corridor.EndPositionX; pos.y = Random.Range(corridor.EndPositionY - dim.y + 1, corridor.EndPositionY); pos.y = Mathf.Clamp(pos.y, 0, rows - dim.y); break; case CorridorDirection.South: dim.y = Mathf.Clamp(dim.y, 1, corridor.EndPositionY); pos.y = corridor.EndPositionY - dim.y + 1; pos.x = Random.Range(corridor.EndPositionX - dim.x + 1, corridor.EndPositionX); pos.x = Mathf.Clamp(pos.x, 0, columns - dim.x); break; case CorridorDirection.West: dim.x = Mathf.Clamp(dim.x, 1, corridor.EndPositionX); pos.x = corridor.EndPositionX - dim.x + 1; pos.y = Random.Range(corridor.EndPositionY - dim.y + 1, corridor.EndPositionY); pos.y = Mathf.Clamp(pos.y, 0, rows - dim.y); break; } }
public void PointTo(CorridorDirection to) { if (to == CorridorDirection.North) { walls [0].SetActive (false); } else if (to == CorridorDirection.South) { walls [1].SetActive (false); } else if (to == CorridorDirection.East) { walls [2].SetActive (false); } else if (to == CorridorDirection.West) { walls [3].SetActive (false); } }
// Gets the inverse public static CorridorDirection OppositeOf(CorridorDirection direction) { switch (direction) { case CorridorDirection.North: return CorridorDirection.South; case CorridorDirection.South: return CorridorDirection.North; case CorridorDirection.East: return CorridorDirection.West; case CorridorDirection.West: return CorridorDirection.East; default: return CorridorDirection.North; }// switch }
} // generate // Create the first cell (speshal) private void CreateFirstCell(IntVector2 nextCoordinates, CorridorDirection nextDirection) { CorridorCell newCell; newCell = Instantiate(cell) as CorridorCell; newCell.PointTo(nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; newCell.material.color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); newCell.name = "First Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3(nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); cellList.Add(newCell); } // createFirstCell
public MapPoint GetPointTowardsDir(CorridorDirection direction, int distance) { switch (direction) { case CorridorDirection.North: return(new MapPoint(x, y + distance)); case CorridorDirection.South: return(new MapPoint(x, y - distance)); case CorridorDirection.West: return(new MapPoint(x + distance, y)); case CorridorDirection.East: return(new MapPoint(x - distance, y)); } return(null); }
} // generate // Create the first cell (speshal) private void CreateFirstCell(IntVector2 nextCoordinates, CorridorDirection nextDirection) { CorridorCell newCell; newCell = Instantiate(corridorCell) as CorridorCell; newCell.PointTo(nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; newCell.name = "First Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3(nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); newCell.setFull(); cellList.Add(newCell); // Set spawn point as this cell spawn = new IntVector2((int)newCell.transform.position.x, (int)newCell.transform.position.z); } // createFirstCell
public void PointTo(CorridorDirection to) { if (to == CorridorDirection.North) { walls [0].SetActive(false); } else if (to == CorridorDirection.South) { walls [1].SetActive(false); } else if (to == CorridorDirection.East) { walls [2].SetActive(false); } else if (to == CorridorDirection.West) { walls [3].SetActive(false); } } // PointTo
} // PointTo // Rotate the cell public void PointFromTo(CorridorDirection from, CorridorDirection to) { Debug.Log("FROM:" + from + " TO:" + to); Debug.Log("STUFF: " + CorridorDirection.North); Debug.Log("walls[0]: " + walls [0]); Debug.Log("walls[1]: " + walls [1]); Debug.Log("walls[2]: " + walls [2]); Debug.Log("walls[3]: " + walls [3]); if (from == CorridorDirection.North) { walls [1].SetActive(false); } else if (from == CorridorDirection.South) { walls [0].SetActive(false); } else if (from == CorridorDirection.East) { walls [3].SetActive(false); } else if (from == CorridorDirection.West) { walls [2].SetActive(false); } if (to == CorridorDirection.North) { walls [0].SetActive(false); } else if (to == CorridorDirection.South) { walls [1].SetActive(false); } else if (to == CorridorDirection.East) { walls [2].SetActive(false); } else if (to == CorridorDirection.West) { walls [3].SetActive(false); } } // point from to
// Gets the inverse public static CorridorDirection OppositeOf(CorridorDirection direction) { switch (direction) { case CorridorDirection.North: return(CorridorDirection.South); case CorridorDirection.South: return(CorridorDirection.North); case CorridorDirection.East: return(CorridorDirection.West); case CorridorDirection.West: return(CorridorDirection.East); default: return(CorridorDirection.North); } // switch } // inverse
// Method for setting up the other rooms public void SetupRoom(int width, int height, int columns, int rows, Dungeon_Corridor corridor) { // Set the direction of the entering corridor enteringCorridor = corridor.direction; // Setup the width and height roomWidth = width; roomHeight = height; // The corridor.direction is the direction of the entering corridor // These if-statements are used to make sure the rooms are always within the size of the dungeon if (corridor.direction == CorridorDirection.Up) { roomHeight = Mathf.Clamp(roomHeight, 1, rows - corridor.EndZPos); // Clamp the roomheight so the room won't go out of the dungeon zPos = corridor.EndZPos; // Set the Z position of the room xPos = Random.Range(corridor.EndXPos - roomWidth + 1, corridor.EndXPos + 1); // Set a random X position for the room xPos = Mathf.Clamp(xPos, 0, columns - roomWidth); // Clamp the X position of the room so it won't go out of the dungeon } else if (corridor.direction == CorridorDirection.Right) { roomWidth = Mathf.Clamp(roomWidth, 1, columns - corridor.EndXPos); xPos = corridor.EndXPos; zPos = Random.Range(corridor.EndZPos - roomHeight + 1, corridor.EndZPos + 1); zPos = Mathf.Clamp(zPos, 0, rows - roomHeight); } else if (corridor.direction == CorridorDirection.Down) { roomHeight = Mathf.Clamp(roomHeight, 1, corridor.EndZPos); zPos = corridor.EndZPos - roomHeight + 1; xPos = Random.Range(corridor.EndXPos - roomWidth + 1, corridor.EndXPos + 1); xPos = Mathf.Clamp(xPos, 0, columns - roomWidth); } else if (corridor.direction == CorridorDirection.Left) { roomWidth = Mathf.Clamp(roomWidth, 1, corridor.EndXPos); xPos = corridor.EndXPos - roomWidth + 1; zPos = Random.Range(corridor.EndZPos - roomHeight + 1, corridor.EndZPos + 1); zPos = Mathf.Clamp(zPos, 0, rows - roomHeight); } }
} // createFirstCell // Create a single corridor cell private void CreateCell(IntVector2 nextCoordinates, CorridorDirection currentDirection, CorridorDirection nextDirection) { // Cell's neighbors CorridorCell north = GetCell(new IntVector2(nextCoordinates.x, nextCoordinates.z + 1)); CorridorCell south = GetCell(new IntVector2(nextCoordinates.x, nextCoordinates.z - 1)); CorridorCell east = GetCell(new IntVector2(nextCoordinates.x + 1, nextCoordinates.z)); CorridorCell west = GetCell(new IntVector2(nextCoordinates.x - 1, nextCoordinates.z)); // The new cell CorridorCell newCell; newCell = Instantiate(corridorCell) as CorridorCell; newCell.PointFromTo(currentDirection, nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; //newCell.material.color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f)); newCell.name = "Corridor Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3(nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); cellList.Add(newCell); } // createCell
// Rotate the cell public void PointFromTo(CorridorDirection from, CorridorDirection to) { if (from == CorridorDirection.North) { walls [1].SetActive (false); } else if (from == CorridorDirection.South) { walls [0].SetActive (false); } else if (from == CorridorDirection.East) { walls [3].SetActive (false); } else if (from == CorridorDirection.West) { walls [2].SetActive (false); } if (to == CorridorDirection.North) { walls [0].SetActive (false); } else if (to == CorridorDirection.South) { walls [1].SetActive (false); } else if (to == CorridorDirection.East) { walls [2].SetActive (false); } else if (to == CorridorDirection.West) { walls [3].SetActive (false); } // Generate the door foreach (GameObject wall in walls) { if(wall.activeSelf){ if(Random.Range(0, 100) < wallDoorProbability) { // Generate new door wall.GetComponent<WallControl>().door = true; doorInstance = Instantiate(doorPrefab) as GameObject; doorInstance.transform.SetParent(wall.transform); doorInstance.transform.localPosition = new Vector3(0f, 0f, -0.05f); doorInstance.transform.localRotation = Quaternion.Euler(new Vector3(0f, 180f, 0f)); doorInstance.transform.localScale = new Vector3(0.1f, 0.1f, 0.3f); //wall.GetComponent<MeshRenderer>().material.color = Color.blue; } }// if }// foreach }
} // RandomValue // Converts into int vectors public static IntVector2 ToIntVector2(this CorridorDirection direction) { return(vectors [(int)direction]); }
// Create the first cell (speshal) private void CreateFirstCell(IntVector2 nextCoordinates, CorridorDirection nextDirection) { CorridorCell newCell; newCell = Instantiate (corridorCell) as CorridorCell; newCell.PointTo (nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; newCell.name = "First Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3 (nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); newCell.setFull (); cellList.Add(newCell); // Set spawn point as this cell spawn = new IntVector2 ((int)newCell.transform.position.x, (int)newCell.transform.position.z); }
// Create a single corridor cell private void CreateCell(IntVector2 nextCoordinates, CorridorDirection currentDirection, CorridorDirection nextDirection) { // Cell's neighbors CorridorCell north = GetCell (new IntVector2(nextCoordinates.x, nextCoordinates.z + 1)); CorridorCell south = GetCell (new IntVector2(nextCoordinates.x, nextCoordinates.z - 1)); CorridorCell east = GetCell (new IntVector2(nextCoordinates.x + 1, nextCoordinates.z)); CorridorCell west = GetCell (new IntVector2(nextCoordinates.x - 1, nextCoordinates.z)); // The new cell CorridorCell newCell; newCell = Instantiate (corridorCell) as CorridorCell; newCell.PointFromTo (currentDirection, nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; //newCell.material.color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f)); newCell.name = "Corridor Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3 (nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); cellList.Add(newCell); }
public void SetupCorridor(Room room, IntRange length, IntRange roomWidth, IntRange roomHeight, int columns, int rows, bool firstCorridor) { // Set a random direction (a random index from 0 to 3, cast to CorridorDirection). direction = (CorridorDirection)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. CorridorDirection oppositeDirection = (CorridorDirection)(((int)room.enteringCorridor + 2) % 4); // If this is noth the first corridor and the randomly selected direction is opposite to the previous corridor's direction... if (!firstCorridor && 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 = (CorridorDirection)directionInt; } // Set a random length. corridorLength = length.Random; // Create a cap for how long the length can be (this will be changed based on the direction and position). int maxLength = length.m_Max; switch (direction) { // If the choosen direction is North (up)... case CorridorDirection.North: // ... the starting position in the x axis can be random but within the width of the room. startXPos = Random.Range(room.pos.x, room.pos.x + room.dim.x - 1); // The starting position in the y axis must be the top of the room. startYPos = room.pos.y + room.dim.y; // The maximum length the corridor can be is the height of the board (rows) but from the top of the room (y pos + height). maxLength = rows - startYPos - roomHeight.m_Min; break; case CorridorDirection.East: startXPos = room.pos.x + room.dim.x; startYPos = Random.Range(room.pos.y, room.pos.y + room.dim.y - 1); maxLength = columns - startXPos - roomWidth.m_Min; break; case CorridorDirection.South: startXPos = Random.Range(room.pos.x, room.pos.x + room.dim.x); startYPos = room.pos.y; maxLength = startYPos - roomHeight.m_Min; break; case CorridorDirection.West: startXPos = room.pos.x; startYPos = Random.Range(room.pos.y, room.pos.y + room.dim.y); maxLength = startXPos - roomWidth.m_Min; break; } // We clamp the length of the corridor to make sure it doesn't go off the board. corridorLength = Mathf.Clamp(corridorLength, 1, maxLength); }
public MapPoint GetNextPointTowardsDir(CorridorDirection direction) => GetPointTowardsDir(direction, 1);
public Corridor(MapPoint startingPoint, CorridorDirection direction, int length) { this.startingPoint = startingPoint; this.length = length; this.direction = direction; }
// Create the first cell (speshal) private void CreateFirstCell(IntVector2 nextCoordinates, CorridorDirection nextDirection) { CorridorCell newCell; newCell = Instantiate (cell) as CorridorCell; newCell.PointTo (nextDirection); cells [nextCoordinates.x, nextCoordinates.z] = newCell; newCell.material.color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f)); newCell.name = "First Cell " + nextCoordinates.x + ", " + nextCoordinates.z; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3 (nextCoordinates.x - size.x * 0.5f + 0.5f, 0f, nextCoordinates.z - size.z * 0.5f + 0.5f); cellList.Add(newCell); }