Skip to content

Instantly share code, notes, and snippets.

@talecrafter
Created April 19, 2017 16:27
Show Gist options
  • Save talecrafter/9391167720e56d3567048f5750062064 to your computer and use it in GitHub Desktop.
Save talecrafter/9391167720e56d3567048f5750062064 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Random = CraftingLegends.Core.Random;
namespace CraftingLegends.Framework
{
public class RandomDungeonConnectCenters : MonoBehaviour
{
[System.Serializable]
public class RandomDungeonConnectCentersSettings
{
public int width = 30;
public int height = 30;
public int minRoomNumber = 5;
public int maxRoomNumber = 12;
public int minRoomSize = 3;
public int maxRoomSize = 6;
}
// ================================================================================
// public methods
// --------------------------------------------------------------------------------
public DungeonGrid CreateDungeon(int width, int height)
{
RandomDungeonConnectCentersSettings settings = GetStandardSettings(width, height);
return CreateDungeon(settings);
}
public static RandomDungeonConnectCentersSettings GetStandardSettings(int width, int height)
{
RandomDungeonConnectCentersSettings settings = new RandomDungeonConnectCentersSettings();
settings.width = width;
settings.height = height;
int fieldCount = width * height;
int minRoomNumber = fieldCount / 180;
minRoomNumber = Mathf.Max(minRoomNumber, 2);
int maxRoomNumber = fieldCount / 75;
maxRoomNumber = Mathf.Max(maxRoomNumber, minRoomNumber + 2);
settings.minRoomNumber = minRoomNumber;
settings.maxRoomNumber = maxRoomNumber;
settings.minRoomSize = 3;
settings.maxRoomSize = 6;
return settings;
}
public static Grid<T> GetDungeon<T>(RandomDungeonConnectCentersSettings settings, Grid<DungeonFieldType>.ConvertValue<T> convertFunc)
{
var oldGrid = CreateDungeon(settings);
return oldGrid.ConvertToOtherGrid(convertFunc);
}
public static DungeonGrid CreateDungeon(RandomDungeonConnectCentersSettings settings)
{
DungeonGrid dungeon = new DungeonGrid(settings.width, settings.height);
CreateRooms(dungeon, settings);
dungeon.MoveRoomsToCenter();
// mark rooms on the grid
for (int i = 0; i < dungeon.rooms.Count; i++)
{
dungeon.Fill(dungeon.rooms[i], DungeonFieldType.Ground);
}
PlaceCorridors(dungeon);
dungeon.CalculateWalls();
dungeon.PlaceLandmarks();
return dungeon;
}
// ================================================================================
// private methods
// --------------------------------------------------------------------------------
private static void CreateRooms(DungeonGrid grid, RandomDungeonConnectCentersSettings settings)
{
int numberOfRooms = Random.Range(settings.minRoomNumber, settings.maxRoomNumber + 1);
int roomsPlaced = 0;
// number of times a room could not be placed
int faults = 0;
while (roomsPlaced < numberOfRooms && faults < 50)
{
GridRoom room = CreateRandomRoom(grid, settings);
if (room.HasInvalidSize)
{
faults++;
continue;
}
if (grid.rooms.Intersects(room))
{
faults++;
continue;
}
grid.rooms.Add(room);
roomsPlaced++;
faults = 0;
}
}
private static void PlaceCorridors(DungeonGrid grid)
{
for (int i = 0; i < grid.rooms.Count - 1; i++)
{
GridRoom fromRoom = grid.rooms[i];
GridRoom toRoom = grid.rooms[i + 1];
grid.ConnectTwoRoomsWithOffsetPosAndStraightPaths(fromRoom, toRoom, DungeonFieldType.Ground);
}
}
private static GridRoom CreateRandomRoom(DungeonGrid grid, RandomDungeonConnectCentersSettings settings)
{
var maxWidth = Mathf.Min(grid.width - 2, settings.maxRoomSize);
var maxHeight = Mathf.Min(grid.height - 2, settings.maxRoomSize);
var minWidth = Mathf.Min(settings.minRoomSize, maxWidth);
var minHeight = Mathf.Min(settings.minRoomSize, maxHeight);
int width = Random.Range(minWidth, maxWidth + 1);
int height = Random.Range(minHeight, maxHeight + 1);
int x = Random.Range(1, grid.width - width - 1);
int y = Random.Range(1, grid.height - height - 1);
return new GridRoom(x, y, width, height);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment