void GenerateMap(int width, int height, int numContinents, float continentSize) { //Debug.Log(string.Format("Width {0}, height {1}, numContinents {2}, continentSize {3}", width, height, numContinents, continentSize)); groundGrid = new char[width, height]; walkLevelGrid = new char[width, height]; dontCheckGrid = new char[width, height]; instantiatedTiles = new List <GameObject> [width, height]; mapHeight = height; mapWidth = width; // A map must always have a starting zone, but the other continents are mostly random. // ContinentType[] cTypes = new ContinentType[numContinents]; cTypes[0] = ContinentType.GRASSLAND; // Make sure the continents are varied; cTypes[1] = ContinentType.DESERT; cTypes[2] = ContinentType.GLACIER; cTypes[3] = ContinentType.MOUNTAIN; cTypes[4] = ContinentType.VOLCANO; MapCoor mapCenter = new MapCoor(mapWidth / 2, mapHeight / 2); float fromMapCenterRadius = mapWidth / 3; float defaultRadius = fromMapCenterRadius; float theta = 0; float ratio = (float)mapWidth / mapHeight; for (int continent = 0; continent < numContinents - 0; continent++) { MapCoor continentStartPos = new MapCoor((int)(mapCenter.x + fromMapCenterRadius * Mathf.Cos(theta)), (int)(mapCenter.y + fromMapCenterRadius * Mathf.Sin(theta))); //Debug.Log(string.Format("Type: {3}, Radius = {0}, theta = {1}, Center = {2}", fromMapCenterRadius, theta, continentStartPos, cTypes[continent])); // Continents are created in a circle around the center of the map /**EXAMPLE: * WIDTH numContinents = 5 * _____________________ * H | | * E | C3 C2 | * I | | * G | C1 | * H | C4 | * T | C5 | * |_____________________| * * */ int circleRadius = (int)(ratio * continentSize * 2.5f); // A continent is formed by several circles of varying size built off of each other. CreateContinent(continentStartPos.ToVector(), circleRadius, 12, cTypes[continent]); // Calculations for next circle theta += 6.28f / (float)(numContinents); fromMapCenterRadius = defaultRadius - Mathf.Abs(Mathf.Sin(theta) * defaultRadius * (1 - 1 / ratio)); } BuildCoast(); PlaceFeatures(); StartCoroutine(InstantiateTiles()); }
void GenerateMap(int width, int height, int numContinents, float continentSize) { groundGrid = new char[width, height]; walkLevelGrid = new char[width, height]; dontCheckGrid = new char[width, height]; mapHeight = height; mapWidth = width; // A map must always have a starting zone, but the other continents are mostly random. // ContinentType[] cTypes = new ContinentType[numContinents]; int rand = UnityEngine.Random.Range(0, numContinents); cTypes[rand] = ContinentType.STARTING_AREA; // Make sure the continents are varied; // if there is already a mountain, for example, a different type will be picked unless all types have been picked bool isMountain = false; for (int i = 1; i < numContinents; i++) { int randContinent = UnityEngine.Random.Range(0, numContinents); while (cTypes[randContinent] != ContinentType.None) { randContinent = UnityEngine.Random.Range(0, numContinents); } if (!isMountain) { cTypes[randContinent] = ContinentType.MOUNTAINOUS; isMountain = true; } else { cTypes[randContinent] = (ContinentType)Random.Range(1, 3); } } MapCoor mapCenter = new MapCoor(mapWidth / 2, mapHeight / 2); float fromMapCenterRadius = mapWidth / 3; float defaultRadius = fromMapCenterRadius; float theta = 0; float ratio = (float)mapWidth / mapHeight; for (int continent = 0; continent < numContinents - 0; continent++) { MapCoor continentStartPos = new MapCoor((int)(mapCenter.x + fromMapCenterRadius * Mathf.Cos(theta)), (int)(mapCenter.y + fromMapCenterRadius * Mathf.Sin(theta))); //Debug.Log(string.Format("Type: {3}, Radius = {0}, theta = {1}, Center = {2}", fromMapCenterRadius, theta, continentStartPos, cTypes[continent])); // Continents are created in a circle around the center of the map /**EXAMPLE: * WIDTH numContinents = 5 * _____________________ * H | | * E | C3 C2 | * I | | * G | C1 | * H | C4 | * T | C5 | * |_____________________| * * */ int circleRadius = (int)(ratio * continentSize * 2.5f); // A continent is formed by several circles of varying size built off of each other. CreateContinent(continentStartPos.ToVector(), circleRadius, 12, cTypes[continent]); // Calculations for next circle theta += 6.28f / (float)(numContinents); fromMapCenterRadius = defaultRadius - Mathf.Abs(Mathf.Sin(theta) * defaultRadius * (1 - 1 / ratio)); } BuildCoast(); PlaceFeatures(); StartCoroutine(InstantiateTiles()); }