MapData GenerateMapData(Vector2 centre, int Size) { float[,] noiseMap = NoiseGeneration.GenerateNoiseMap(Size, Size, seed, noiseScale, sheets, persistance, lacunarity, centre + offset); float[,] islandMap = new float[Size, Size]; float[,] fallMap = FalloffGenerator.GenerateFalloff((halfIslandSize * 2) + 1, falloff_a, falloff_b); List <Vector2> islands = new List <Vector2>(); List <float> heights = new List <float>(); Color[] colorMap = new Color[Size * Size]; for (int y = 0; y < Size; y++) { for (int x = 0; x < Size; x++) { if (falloff) { noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - falloffMap[x, y]); } float currentHeight = noiseMap[x, y]; if (currentHeight >= falloffHeight && checkIslandOverlap(islands, x, y)) { islands.Add(new Vector2(x, y)); heights.Add(currentHeight); } for (int i = 0; i < regions.Length; i++) { if (currentHeight <= regions[i].height) { colorMap[y * Size + x] = regions[i].color; break; } } } } for (int i = 0; i < islands.Count; i++) { int x = (int)islands[i].x; int y = (int)islands[i].y; for (int o = -halfIslandSize; o < halfIslandSize; o++) { for (int p = -halfIslandSize; p < halfIslandSize; p++) { if (y + o > 0 && y + o < Size && x + p > 0 && x + p < Size) { noiseMap[x + p, y + o] = Mathf.Clamp01(noiseMap[x + p, y + o] - fallMap[p + halfIslandSize, o + halfIslandSize]); islandMap[x + p, y + o] = noiseMap[x + p, y + o]; } } } } return(new global::MapData(islandMap, colorMap)); }
void GenerateTile() { //assigning the value from the noismap script to the heighMap array, aswell as passing in the erquired data for the paramneters on the noiseGeneration script heightMap = noiseGeneration.GenerateNoiseMap(tileLength * chunkNumber, tileDepth * chunkNumber, mapScale); //this array stores the position of every single cube that is created, the second 3 dimnesional array holds all the cubes within it, so the array knows whihc cube belongs to which chunk in the world chunkArray = new GameObject[chunkNumber, chunkNumber][, , ]; //this loop will run for the legnth of chunkSize, so if two chunks ae wanted we will call the cubemake script two times for (int chunkX = 0; chunkX < chunkNumber; chunkX++) { for (int chunkY = 0; chunkY < chunkNumber; chunkY++) { //now we assign each spot of chunkArray at the position of the chunk we are looking at, say chunkX & chunkY are at 1&1 we will be working on the top right hand corner of the chunk, //essentially two chunks are split into almos four total sections, one chunk has two parts to it. also pass in paramteres for the CubeMake script chunkArray[chunkX, chunkY] = cubeSpawnScript.CubeMake(tileLength, tileDepth, mapHeight, heightMap, chunkX, chunkY); } } }
private GameObject currentTerrain; //holds a reference to the current terrainType, used to check if next tile is the same type /* * Method to generate my map from the noise, uses helper methods to generate the trees and grass */ public void GenerateMap() { //creating the individual maps which add together to make the overall map float[,] heightMap = NoiseGeneration.GenerateNoiseMap(mapWidth, mapHeight, seed, zoom, octaves, amplitudeModifier, frequencyModifier); float[,] moistureMap = NoiseGeneration.GenerateNoiseMap(mapWidth, mapHeight, moistureSeed, moistureZoom, moistureOctaves, moistureAmplitudeModifier, moistureFrequencyModifier); float[,] falloffMap = FalloffGeneration.GenerateFalloffMap(mapWidth, falloffPower, falloffOffset); currentTerrain = biomes[0].terrain; //first block will always be 'deepWater' so sets currentTerrain to 'deepWater' //loop to go through each point in my map, generate its terrain block, and have a chance to spawn trees and grass on it for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { float noiseHeight = Mathf.Clamp01(heightMap [x, y] - falloffMap[x, y]); float noiseMoisture = moistureMap [x, y]; //loop to categorise the points into their biomes (based on the noiseHeight and noiseMoisture variables) for (int i = 0; i < biomes.Length; i++) { if (noiseHeight <= biomes [i].height) { Director.GetComponent <Director> ().SetSpawnHeight(x, y, biomes [i].terrainHeight + 0.5f); if (biomes [i].terrainHeight == 1) { Director.GetComponent <Director> ().NotSpawnable(x, y); //sets water tiles (the only tiles with terrainHeight 1) to non spawnable } if (noiseMoisture <= biomes [i].moisture) { spawnTerrain(biomes[i], x, y); //spawning in terrain spawnGrass(biomes [i], x, y); //has a chance to spawn 'grass' on each block if (x % 2 != 0 && y % 2 != 0) //has a chance to place a tree every other block { spawnTrees(biomes [i], x, y); } break; } } } } } }