// Connects a sector to one of its neighbors, chosen by indexing into the neighbor array public void connectSectortoNeighbor(DungeonSector a, int neighborIndex) { int sharedWallCoordinate; Vector2Int doorLocation; Door door; // Get both neighbor objects so that we can attach a reference to the door to each Neighbor n = a.neighbors[neighborIndex]; DungeonSector b = n.getSector(); Neighbor n2 = b.getNeighbor(a); // Decide the location of the door, making sure its on a shared wall between the two sectors DungeonSector.SIDE side = n.getSide(); if (side == DungeonSector.SIDE.BOTTOM || side == DungeonSector.SIDE.TOP) { sharedWallCoordinate = getRandomSharedWallCoordinate(a.position.x, a.size.x, b.position.x, b.size.x); doorLocation = new Vector2Int(sharedWallCoordinate, a.size.y); if (side == DungeonSector.SIDE.BOTTOM) { doorLocation.y = -1; } } else if (side == DungeonSector.SIDE.LEFT || side == DungeonSector.SIDE.RIGHT) { sharedWallCoordinate = getRandomSharedWallCoordinate(a.position.y, a.size.y, b.position.y, b.size.y); doorLocation = new Vector2Int(-1, sharedWallCoordinate); if (side == DungeonSector.SIDE.RIGHT) { doorLocation.x = a.size.x; } } else { return; } // Save the door info to the dungeon, as well as the neighbor objects of both sectors doorLocation = a.sectorToDungeonCoordinates(doorLocation); door = new Door(doorLocation, n, n2); if ((a.type == DungeonSector.TYPE.FILLED) && (b.type == DungeonSector.TYPE.FILLED)) { door.opened = true; } tiles[doorLocation.x][doorLocation.y] = new ShiblitzTile(ShiblitzTile.TYPE.DOOR); doors.Add(door); n.connect(door); n2.connect(door); }
protected override bool validLocation(Vector2Int location) { Dungeon dungeon = Game.getDungeon(); // return false if location is out of bounds if (location.x < 0 || location.x >= dungeon.dungeonSize.x) { return(false); } if (location.y < 0 || location.y >= dungeon.dungeonSize.y) { return(false); } // return false if location is on a wall if (dungeon.tiles[location.x][location.y].type == ShiblitzTile.TYPE.WALL) { return(false); } // If the caster is an enemy, dont let them cast on closed doors Door d = dungeon.getDoor(location); if (caster is Enemy) { if (Game.getDungeonBoard().occupiedSpaces.Contains(location)) { return(false); } if (d != null && !d.opened) { return(false); } } else { if (d != null && !d.neighbor1.getSector().revealed&& !d.neighbor2.getSector().revealed) { return(false); } } // If the location is in an unrevealed sector, return false DungeonSector s = dungeon.getSector(location); if (d == null && (s == null || !s.revealed)) { return(false); } return(true); }
// Returns true if the given location is occupiable public bool canMoveTo(Vector2Int location) { DungeonSector d = getSector(location); if (d != null) { return(tiles[location.x][location.y].type == ShiblitzTile.TYPE.FLOOR); } else { if (getDoor(location) != null) { return(true); } } return(false); }
// Recursively split the allowed space into sectors private List <DungeonSector> splitSectorsRecursively(Vector2Int position, Vector2Int size) { Vector2Int sector1Position; Vector2Int sector2Position; Vector2Int sector1Size; Vector2Int sector2Size; int minimumHorizontalSpace = minRoomSize * 2 + 1; // Need enough space for 2 rooms and a wall tile between them int minimumVerticalSpace = minRoomSize * 2 + 1; // Build and return the sector if it is too small to split. Also do this randomly sometimes as long as the room is not too big if (size.x <= maxRoomSize && size.y <= maxRoomSize && UnityEngine.Random.value > 0.75f || (size.x < minimumHorizontalSpace && size.y < minimumVerticalSpace)) { DungeonSector.TYPE type = DungeonSector.TYPE.REGULAR; if (!(position.x == 1 && position.y == 1) && UnityEngine.Random.value > (1 - corridorRate)) // Add a chance to become a corridor as long as we are not the starting room { type = DungeonSector.TYPE.FILLED; } DungeonSector sector = new DungeonSector(position, size, this, type); return(new List <DungeonSector> { sector }); } bool verticalSplitAllowed = size.x >= minimumHorizontalSpace; bool horizontalSplitAllowed = size.y >= minimumVerticalSpace; // Pick between veritcal and horizontal split if (horizontalSplitAllowed && verticalSplitAllowed) { if (UnityEngine.Random.value > 0.5f) { horizontalSplitAllowed = false; } else { verticalSplitAllowed = false; } } if (verticalSplitAllowed) // Do a vertical split { int allowedVariance = (size.x - minimumHorizontalSpace) / 2; int extra = UnityEngine.Random.Range(0, allowedVariance + 1); if (allowedVariance > 0 && extra == 0) { extra = allowedVariance; } if (allowedVariance == 0) { extra = 0; } if (UnityEngine.Random.value > 0.5f) { extra *= -1; } sector1Position = position; sector1Size = new Vector2Int((size.x / 2) + extra, size.y); sector2Size = new Vector2Int(size.x - sector1Size.x - 1, size.y); sector2Position = new Vector2Int(position.x + sector1Size.x + 1, position.y); } else if (horizontalSplitAllowed) // Do a horizontal split { int allowedVariance = (size.y - minimumVerticalSpace) / 2; int extra = UnityEngine.Random.Range(0, allowedVariance + 1); if (allowedVariance > 0 && extra == 0) { extra = allowedVariance; } if (UnityEngine.Random.value > 0.5f) { extra *= -1; } sector1Position = position; sector1Size = new Vector2Int(size.x, (size.y / 2) + extra); sector2Size = new Vector2Int(size.x, size.y - sector1Size.y - 1); sector2Position = new Vector2Int(position.x, position.y + sector1Size.y + 1); } else { Debug.Log("neither horizontal or vertical split was allowed, but sector was split anyways!"); return(null); } List <DungeonSector> firstSectorList = splitSectorsRecursively(sector1Position, sector1Size); List <DungeonSector> secondSectorList = splitSectorsRecursively(sector2Position, sector2Size); firstSectorList.AddRange(secondSectorList); return(firstSectorList); }
public void panAndZoomTo(DungeonSector d) { Camera.main.orthographicSize = Mathf.Max(d.size.x, d.size.y); transform.position = new Vector3((d.position.x + d.size.x) / 2, (d.position.y + d.size.y) / 2, -10); }
public void findNeighbors() { neighbors = new List <Neighbor>(); int i = 0; // Get neighbors below if (position.y > 1) { while (i < size.x) { Vector2Int dungeonCoordinates = sectorToDungeonCoordinates(new Vector2Int(i, -2)); DungeonSector neighbor = dungeon.getSector(dungeonCoordinates); if (neighbor != null) { neighbors.Add(new Neighbor(neighbor, SIDE.BOTTOM)); i = neighbor.position.x + neighbor.size.x + 1 - position.x; } else { i++; } } } i = 0; // Get neighbors above if (position.y + size.y < dungeon.dungeonSize.y - 1) { while (i < size.x) { Vector2Int dungeonCoordinates = sectorToDungeonCoordinates(new Vector2Int(i, size.y + 2)); DungeonSector neighbor = dungeon.getSector(dungeonCoordinates); if (neighbor != null) { neighbors.Add(new Neighbor(neighbor, SIDE.TOP)); i = neighbor.position.x + neighbor.size.x + 1 - position.x; } else { i++; } } } i = 0; // left if (position.x > 1) { while (i < size.y) { Vector2Int dungeonCoordinates = sectorToDungeonCoordinates(new Vector2Int(-2, i)); DungeonSector neighbor = dungeon.getSector(dungeonCoordinates); if (neighbor != null) { neighbors.Add(new Neighbor(neighbor, SIDE.LEFT)); i = neighbor.position.y + neighbor.size.y + 1 - position.y; } else { i++; } } } i = 0; // right if (position.x + size.x < dungeon.dungeonSize.x - 1) { while (i < size.y) { Vector2Int dungeonCoordinates = sectorToDungeonCoordinates(new Vector2Int(size.x + 2, i)); DungeonSector neighbor = dungeon.getSector(dungeonCoordinates); if (neighbor != null) { neighbors.Add(new Neighbor(neighbor, SIDE.RIGHT)); i = neighbor.position.y + neighbor.size.y + 1 - position.y; } else { i++; } } } }