private void SpawnEnemies() { var numEnemies = GetNumberOfEnemiesForFloor(); enemies = Level.GetRequiredEnemies(); var spawnLocations = Level.GetPossibleSpawnLocations(); foreach (var enemy in enemies) { var enemyLocation = BattleManager.ConvertVector(enemy.transform.position); spawnLocations.Remove(enemyLocation); //If this is the boss if (enemy.AI is BossBrain) { PlaceBoss(enemy); } else { map[enemyLocation.x, enemyLocation.y].tileEntityType = Roguelike.Tile.TileEntityType.enemy; PlaceObjectOn(enemyLocation.x, enemyLocation.y, enemy); } } while (enemies.Count < numEnemies && spawnLocations.Count > 0) { var location = SeededRandom.PickRandom(spawnLocations); spawnLocations.Remove(location); map[location.x, location.y].tileEntityType = Roguelike.Tile.TileEntityType.enemy; SpawnEnemyAt(location.x, location.y); } }
/// <summary> /// Paints the tileMap to match the roguelike map /// </summary> /// <param name="tileMap"></param> /// <param name="map"></param> public void Paint(Tilemap tileMap, Roguelike.Tile[,] map, int sizeX, int sizeZ) { for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeZ; j++) { Tile tile = null; switch (map[i, j].tileEntityType) { //Enemies and the player can only spawn on floors case Roguelike.Tile.TileEntityType.enemy: case Roguelike.Tile.TileEntityType.player: case Roguelike.Tile.TileEntityType.empty: if (map[i, j].tileTerrainType == Roguelike.Tile.TileTerrainType.stairsDown) { decorations.PaintStairs(new Vector3Int(i, j, 0)); } tile = SeededRandom.PickRandom(FloorTiles); break; case Roguelike.Tile.TileEntityType.wall: tile = GetWallTile(i, j, map); break; } if (tile != null) { tileMap.SetTile(new Vector3Int(i, j, 0), tile); var getTile = tileMap.GetTile(new Vector3Int(i, j, 0)); Debug.Log(getTile.name); } } } }
private void PlaceTraps() { traps = Level.GetRequiredTraps(); var spawnLocations = Level.GetPossibleSpawnLocations(); foreach (var trap in traps) { var trapLocation = BattleManager.ConvertVector(trap.transform.position); spawnLocations.Remove(trapLocation); PlaceTerrainOn(trapLocation.x, trapLocation.y, trap); } var numTraps = GetNumberOfTrapsForFloor(); while (traps.Count < numTraps && spawnLocations.Count > 0) { var spawnLoc = SeededRandom.PickRandom(spawnLocations); spawnLocations.Remove(spawnLoc); var trap = EnemySpawner.SpawnSpikes(spawnLoc).GetComponent <Spikes>(); traps.Add(trap); PlaceTerrainOn(spawnLoc.x, spawnLoc.y, trap); if (SeededRandom.RandBool(.5f)) { trap.MakeInvisible(); } } }
public void PaintTopWall(int minX, int maxX, int yValue) { for (int i = minX; i < maxX; i++) { var tile = SeededRandom.PickRandom(Painter.TopWallTiles); Level.Terrain.SetTile(new Vector3Int(i, yValue, 0), tile); } }
public void PaintRightWall(int minY, int maxY, int xValue) { for (int i = minY; i < maxY; i++) { var tile = SeededRandom.PickRandom(Painter.RightWallTiles); Level.Terrain.SetTile(new Vector3Int(xValue, i, 0), tile); } }
public void PaintFloorArea(int minX, int maxX, int minY, int maxY) { for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { var tile = SeededRandom.PickRandom(Painter.FloorTiles); Level.Terrain.SetTile(new Vector3Int(i, j, 0), tile); } } }
private Tile GetWallTile(int x, int y) { //This is terrible but no super easy way of doing it right now int maxX = Room.Bounds.Right; int maxY = Room.Bounds.Top; if (IsFloor(x, y - 1)) { return(SeededRandom.PickRandom(Painter.TopWallTiles)); } if (IsFloor(x + 1, y) && IsFloor(x, y + 1)) { return(SeededRandom.PickRandom(Painter.TurnRightTiles)); } if (IsFloor(x - 1, y) && IsFloor(x, y + 1)) { return(SeededRandom.PickRandom(Painter.TurnLeftTiles)); } if (IsFloor(x - 1, y)) { return(SeededRandom.PickRandom(Painter.RightWallTiles)); } if (IsFloor(x + 1, y)) { return(SeededRandom.PickRandom(Painter.LeftWallTiles)); } if (IsFloor(x, y + 1)) { return(SeededRandom.PickRandom(Painter.BottomWallTiles)); } if (IsFloor(x + 1, y - 1)) { Room.Corners.Add(new Vector2Int(x, y)); return(SeededRandom.PickRandom(Painter.LeftWallTiles)); } if (IsFloor(x - 1, y - 1)) { Room.Corners.Add(new Vector2Int(x, y)); return(SeededRandom.PickRandom(Painter.RightWallTiles)); } if (IsFloor(x + 1, y + 1)) { Room.Corners.Add(new Vector2Int(x, y)); return(Painter.BottomLeftCorner); } if (IsFloor(x - 1, y + 1)) { Room.Corners.Add(new Vector2Int(x, y)); return(Painter.BottomRightCorner); } return(null); }
public override List <EnemyBody> GetAnyRequiredEnemies(Level level) { var possibleSpawnLocations = GetPossibleSpawnLocations(level); var spawnLoc = SeededRandom.PickRandom(possibleSpawnLocations); if (spawnLoc != null) { var brute = EnemySpawner.SpawnBrute(spawnLoc); return(new List <EnemyBody> { brute.GetComponent <EnemyBody>() }); } return(new List <EnemyBody>()); }
private Tile GetWallTile(int x, int y, Roguelike.Tile[,] map) { //This is terrible but no super easy way of doing it right now int maxX = map.GetLength(0) - 1; int maxY = map.GetLength(1) - 1; if (y != 0 && IsEmpty(map[x, y - 1])) { return(SeededRandom.PickRandom(TopWallTiles)); } if (x != maxX && y != maxY && IsEmpty(map[x + 1, y]) && IsEmpty(map[x, y + 1])) { return(SeededRandom.PickRandom(TurnRightTiles)); } if (x != 0 && y != maxY && IsEmpty(map[x - 1, y]) && IsEmpty(map[x, y + 1])) { return(SeededRandom.PickRandom(TurnLeftTiles)); } if (x != 0 && IsEmpty(map[x - 1, y])) { return(SeededRandom.PickRandom(RightWallTiles)); } if (x != maxX && IsEmpty(map[x + 1, y])) { return(SeededRandom.PickRandom(LeftWallTiles)); } if (y != maxY && IsEmpty(map[x, y + 1])) { return(SeededRandom.PickRandom(BottomWallTiles)); } if (x != maxX && y != 0 && IsEmpty(map[x + 1, y - 1])) { return(SeededRandom.PickRandom(LeftWallTiles)); } if (x != 0 && y != 0 && IsEmpty(map[x - 1, y - 1])) { return(SeededRandom.PickRandom(RightWallTiles)); } if (x != maxX && y != maxY && IsEmpty(map[x + 1, y + 1])) { return(BottomLeftCorner); } if (x != 0 && y != maxY && IsEmpty(map[x - 1, y + 1])) { return(BottomRightCorner); } return(EmptySpace); }
private Vector2Int?FindStairsLocation() { var spawnArea = new RogueRect() { Left = Bounds.Left + 2, Right = Bounds.Right - 3, Top = Bounds.Top - 3, Bottom = Bounds.Bottom + 2 }; var possiblePoints = spawnArea.GetPoints(); if (possiblePoints.Count > 0) { return(SeededRandom.PickRandom(possiblePoints)); } return(null); }
private void PlaceTreasure() { var treasures = Level.GetRequiredTreasure(); var spawnLocations = Level.GetPossibleSpawnLocations(); foreach (var treasure in treasures) { var treLocation = BattleManager.ConvertVector(treasure.transform.position); spawnLocations.Remove(treLocation); treasure.xPos = treLocation.x; treasure.zPos = treLocation.y; map[treasure.xPos, treasure.zPos].SetItemOnTile(treasure); } var numTreasure = GetNumberOfTreasureForFloor(); while (treasures.Count < numTreasure && spawnLocations.Count != 0) { var spawnLoc = SeededRandom.PickRandom(spawnLocations); spawnLocations.Remove(spawnLoc); treasures.Add(SpawnChestAt(spawnLoc.x, spawnLoc.y, TreasureChest.TreasureChestTypeEnum.small)); } }
private SerializableVector2Int?GetConnectionPoint(Room room) { var doorPoints = new List <SerializableVector2Int>(); var intersection = Bounds.Intersect(room.Bounds); if (intersection == null) { return(null); } foreach (var point in intersection.GetPoints()) { if (CanConnect(point) && room.CanConnect(point)) { doorPoints.Add(point); } } if (doorPoints.Count > 0) { return(SeededRandom.PickRandom(doorPoints)); } return(null); }
protected void CreateBranches(List <Room> roomsToBranch) { Debug.Log($"Number of rooms to branch: {roomsToBranch.Count}"); var currentBranch = new List <Room>(); var i = 0; while (i < roomsToBranch.Count) { currentBranch.Clear(); var room = roomsToBranch[i]; var curr = SeededRandom.PickRandom(branchable); var numConnections = SeededRandom.PickRandom(branchConnectionLength); currentBranch = TryCreateBranch(room, curr, numConnections); if (currentBranch.Count != numConnections + 1) { continue; } foreach (var branched in currentBranch) { if (branched.Info.MaxConnections > 1 && SeededRandom.RandBool(.3f)) { if (branched is StandardRoom) { for (int j = 0; j < SeededRandom.Range(1, 4); j++) { branchable.Add(branched); } } else { branchable.Add(branched); } } } i++; } }
public virtual void PaintDoors(Level level) { var painter = new RoomPainter(level, this); foreach (var door in ConnectionPoints.Values) { if (door.x == Bounds.Right) { painter.PaintFloor(door.x - 1, door.y); } else if (door.y == Bounds.Top) { painter.PaintFloor(door.x, door.y - 1); } else { painter.PaintFloor(door.x, door.y); } if (door.x == Bounds.Right || door.x == Bounds.Left) { var xValue = door.x == Bounds.Right ? door.x - 1 : door.x; if (door.y <= Bounds.Top - 2) { painter.PaintTopWall(xValue, xValue + 1, door.y + 1); } if (door.y > Bounds.Bottom + 1) { if (door.x == Bounds.Right) { painter.PaintTerrainTile(xValue, door.y - 1, SeededRandom.PickRandom(Painter.TurnLeftTiles)); } else { painter.PaintTerrainTile(xValue, door.y - 1, SeededRandom.PickRandom(Painter.TurnRightTiles)); } } else if (door.y == Bounds.Bottom + 1) { painter.PaintBottomWall(xValue, xValue + 1, door.y - 1); } } else if (door.y == Bounds.Bottom) { if (door.x < Bounds.Right - 2) { painter.PaintTerrainTile(door.x + 1, door.y, SeededRandom.PickRandom(Painter.TurnLeftTiles)); } else if (door.x == Bounds.Right - 2) { painter.PaintRightWall(door.y, door.y + 1, door.x + 1); } if (door.x > Bounds.Left + 1) { painter.PaintTerrainTile(door.x - 1, door.y, SeededRandom.PickRandom(Painter.TurnRightTiles)); } else if (door.x == Bounds.Left + 1) { painter.PaintLeftWall(door.y, door.y + 1, door.x - 1); } } } }
public override bool Build(List <Room> rooms) { this.Rooms = rooms; InitBuilder(); if (entrance == null || exit == null) { return(false); } float direction = SeededRandom.RandomDirection(); entrance.SetSize(); entrance.Bounds.SetPosition(0, 0); PlacedRooms.Add(entrance); branchable.Add(entrance); int roomsOnPath = (int)(multiConnections.Count * pathLengthPercentage) + SeededRandom.PickRandom(pathLengthJitters); roomsOnPath = Math.Min(roomsOnPath, multiConnections.Count); float pathVariance = 15; var curr = entrance; float res; int rejected = 0; for (int i = 0; i < roomsOnPath; i++) { for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++) { var connectionRoom = new ConnectionRoom(); res = AttemptToPlaceRoom(curr, connectionRoom, direction + SeededRandom.Range(-pathVariance, pathVariance)); if (res != -1) { PlacedRooms.Add(connectionRoom); branchable.Add(connectionRoom); curr = connectionRoom; } else { rejected++; } } var next = multiConnections[i]; res = AttemptToPlaceRoom(curr, next, direction + SeededRandom.Range(-pathVariance, pathVariance)); if (res != -1) { PlacedRooms.Add(next); branchable.Add(next); curr = next; } else { rejected++; } } for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++) { var connectionRoom = new ConnectionRoom(); res = AttemptToPlaceRoom(curr, connectionRoom, direction + SeededRandom.Range(-pathVariance, pathVariance)); if (res != -1) { PlacedRooms.Add(connectionRoom); branchable.Add(connectionRoom); curr = connectionRoom; } else { rejected++; } } if (!PlaceExit(curr, direction, pathVariance)) { return(false); } var roomsToBranch = GetRoomsToBranch(roomsOnPath); CreateBranches(roomsToBranch); FindNeighbours(); return(true); }
public void PaintFloor(int x, int y) { var tile = SeededRandom.PickRandom(Painter.FloorTiles); Level.Terrain.SetTile(new Vector3Int(x, y, 0), tile); }
public override bool Build(List <Room> rooms) { this.Rooms = rooms; InitBuilder(); if (entrance == null || exit == null) { return(false); } float startAngle = SeededRandom.RandomDirection(); entrance.SetSize(); entrance.Bounds.SetPosition(0, 0); PlacedRooms.Add(entrance); branchable.Add(entrance); var loop = new List <Room>(); int roomsOnPath = (int)(multiConnections.Count * pathLengthPercentage) + SeededRandom.PickRandom(pathLengthJitters); roomsOnPath = Math.Min(roomsOnPath, multiConnections.Count); for (int i = 0; i <= roomsOnPath; i++) { if (i == 0) { loop.Add(entrance); } else { loop.Add(multiConnections[0]); multiConnections.RemoveAt(0); } for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++) { var newRoom = new ConnectionRoom(); loop.Add(newRoom); } } loop.Insert((loop.Count + 1) / 2, exit); var prev = entrance; float targetAngle; for (int i = 1; i < loop.Count; i++) { var curr = loop[i]; targetAngle = startAngle + TargetAngle(i / (float)loop.Count); var res = AttemptToPlaceRoom(prev, curr, targetAngle); if (res != -1) { PlacedRooms.Add(curr); prev = curr; branchable.Add(curr); } else if (!(curr is ConnectionRoom)) { //Little chance dependant, could be better return(false); } } //While not connected with the entrance, place connection rooms to connect //Also chance dependent. Should find better way to handle while (!prev.Connect(entrance)) { var c = new ConnectionRoom(); var angleToEntrance = GetAngleBetweenRooms(prev, entrance); if (AttemptToPlaceRoom(prev, c, angleToEntrance) == -1) { return(false); } loop.Add(c); PlacedRooms.Add(c); branchable.Add(c); prev = c; } UnityEngine.Debug.Log($"There are {loop.Count} rooms in the loop"); //Pass in 0 because we remove the rooms from multiconnection var roomsToBranch = GetRoomsToBranch(0); CreateBranches(roomsToBranch); FindNeighbours(); return(true); }