private void LoadEntities(BiomePreset biome, Vector2 tilePosition, Vector2 chunkCoord, System.Random RNG) { if (!chunkValues.ContainsKey(chunkCoord)) { if (RNG.NextDouble() > 1 - (biome.enemySpawnPercentage / 100)) { Instantiate(biome.PickRandomPrefab(RNG, biome.enemyPrefabs), tilePosition, Quaternion.identity, mapObjectsParent); } if (RNG.NextDouble() > 1 - (biome.foliageSpawnPercentage / 100)) { Instantiate(biome.PickRandomPrefab(RNG, biome.foliagePrefabs), tilePosition, Quaternion.identity, mapObjectsParent); } } else { foreach (GameObject entitie in chunkValues[chunkCoord].entities) { if (entitie != null) { entitie.SetActive(true); } } } }
private void SpawnFoliage(BiomePreset biome, Vector3 position) { if (random.NextDouble() > 1 - (biome.foliageSpawnPercentage / 100)) { Instantiate(biome.PickRandomFoliage(), position, Quaternion.identity); } }
void LoadChunk(Vector2 chunkCoord) { System.Random foliageRNG = new System.Random(mapValues.seed + (int)chunkCoord.x); heightMap = Noise.GenerateTileMapNoise(chunkSize, chunkSize, mapValues.seed, chunkCoord.x * chunkSize, chunkCoord.y * chunkSize, mapValues.scale, mapValues.octaves, mapValues.persistance, mapValues.lacunarity); List <Vector3Int> tilePositions = new List <Vector3Int>(); List <TileBase> tiles = new List <TileBase>(); for (int x = 0; x < chunkSize; x++) { for (int y = 0; y < chunkSize; y++) { int xPos = x + Mathf.RoundToInt(chunkCoord.x * chunkSize); int yPos = y + Mathf.RoundToInt(chunkCoord.y * chunkSize); BiomePreset biome = GetBiome(Mathf.Clamp01(heightMap[x, y])); Vector3Int newTilePosition = new Vector3Int(xPos, yPos, 0); tilePositions.Add(newTilePosition); tiles.Add(GetBiome(heightMap[x, y]).PickRandomTile()); SpawnFoliage(biome, (Vector3)newTilePosition + new Vector3(0.5f, 0.5f), foliageRNG); } } tilemap.SetTiles(tilePositions.ToArray(), tiles.ToArray()); }
private void SpawnFoliage(BiomePreset biome, Vector3 position, System.Random foliageRNG) { if (foliageRNG.NextDouble() > 1 - (biome.foliageSpawnPercentage / 100)) { Instantiate(biome.PickRandomFoliage(), position, Quaternion.identity, foliageParent.transform); } }
void GenerateMap() { heightMap = NoiseGenerator.Generate(width, height, scale, offset, heightWaves); moistureMap = NoiseGenerator.Generate(width, height, scale, offset, moistureWaves); heatMap = NoiseGenerator.Generate(width, height, scale, offset, heatWaves); Color[] pixels = new Color[width * height]; int i = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { switch (displayType) { case MapDisplay.Height: pixels[i] = heightDebugColors.Evaluate(heightMap[x, y]); break; case MapDisplay.Moisture: pixels[i] = moistureDebugColors.Evaluate(moistureMap[x, y]); break; case MapDisplay.Heat: pixels[i] = heatDebugColors.Evaluate(heatMap[x, y]); break; case MapDisplay.Biome: { BiomePreset biome = GetBiome(heightMap[x, y], moistureMap[x, y], heatMap[x, y]); pixels[i] = biome.debugColor; if (Application.isPlaying) { GameObject tile = Instantiate(tilePrefab, new Vector3(x, y, 0), Quaternion.identity); tile.GetComponent <SpriteRenderer>().sprite = biome.GetTileSprite(); } break; } } i++; } } Texture2D tex = new Texture2D(width, height); tex.SetPixels(pixels); tex.filterMode = FilterMode.Point; tex.Apply(); debugImage.texture = tex; }
private BiomePreset GetBiome(float height) { BiomePreset ChosenBiome = biomes[0]; float minHeight = 0f; foreach (BiomePreset biome in biomes) { if (height >= biome.minHeight && biome.minHeight >= minHeight) { minHeight = biome.minHeight; ChosenBiome = biome; } } return(ChosenBiome); }
// will get the biome that is closest to what the noise maps gives BiomePreset GetBiome(float height, float moisture, float heat) { BiomePreset biomeToReturn = null; List <BiomePreset> tempBiomes = new List <BiomePreset>(); // will loop through each of the biomes if it matches the requirements it will be added to tempbiomes foreach (BiomePreset biome in biomes) { if (biome.MatchCondition(height, moisture, heat)) { tempBiomes.Add(biome); } } float curVal = 0.0f; // loop through each of the biomes that meet the requirements // find the one closes to the original height, moisture and heat values foreach (BiomePreset biome in tempBiomes) { float diffVal = (height - biome.minHeight) + (moisture - biome.minMoisture) + (heat - biome.minHeat); if (biomeToReturn == null) { biomeToReturn = biome; curVal = diffVal; } else if (diffVal < curVal) { biomeToReturn = biome; curVal = diffVal; } } // if no biome is found - return the first one in the biomes array if (biomeToReturn == null) { return(biomes[0]); } return(biomeToReturn); }
private void GenerateMap() { heightMap = Noise.GenerateNoise(mapValues.width, mapValues.height, mapValues.seed, heightMapValues.offsetX, heightMapValues.offsetY, mapValues.scale, heightMapValues.octaves, heightMapValues.persistance, heightMapValues.lacunarity); Vector2 center = new Vector2(mapValues.width * 0.5f, mapValues.height * 0.5f); float[,] gradientMap = RadialGradient.GenerateGradient(mapValues.width, mapValues.height, gradientMapValues.gradientThreshold, center, gradientMapValues.gradientIntensityPoint, gradientMapValues.gradientIntensity); for (int x = 0; x < mapValues.width; ++x) { for (int y = 0; y < mapValues.height; ++y) { BiomePreset biome = GetBiome(Mathf.Clamp01(heightMap[x, y] + gradientMap[x, y])); tilemap.SetTile(new Vector3Int(x, y, 0), biome.PickRandomTile()); //tilemap.SetTile(new Vector3Int(x, y, 0), SpawnFoliage(biome)); } } }
void LoadChunk(Vector2 chunkCoord) { System.Random RNG = new System.Random(mapValues.seed + (int)chunkCoord.x + (int)chunkCoord.y); heightMap = Noise.GenerateTileMapNoise(chunkSize, chunkSize, mapValues.seed, chunkCoord.x * chunkSize, chunkCoord.y * chunkSize, mapValues.scale, mapValues.octaves, mapValues.persistance, mapValues.lacunarity); List <Vector3Int> landTilePositions = new List <Vector3Int>(); List <Vector3Int> waterTilePositions = new List <Vector3Int>(); List <TileBase> landTiles = new List <TileBase>(); List <TileBase> waterTiles = new List <TileBase>(); for (int x = 0; x < chunkSize; x++) { for (int y = 0; y < chunkSize; y++) { int xPos = x + Mathf.RoundToInt(chunkCoord.x * chunkSize); int yPos = y + Mathf.RoundToInt(chunkCoord.y * chunkSize); BiomePreset biome = GetBiome(Mathf.Clamp01(heightMap[x, y])); Vector3Int newTilePosition = new Vector3Int(xPos, yPos, 0); if (GetBiome(heightMap[x, y]).name != "Ocean") { landTilePositions.Add(newTilePosition); landTiles.Add(GetBiome(heightMap[x, y]).PickRandomTile(RNG)); } else { landTilePositions.Add(newTilePosition); landTiles.Add(GetBiome(0.3f).PickRandomTile(RNG)); waterTilePositions.Add(newTilePosition); waterTiles.Add(GetBiome(heightMap[x, y]).PickRandomTile(RNG)); } LoadEntities(biome, (Vector3)newTilePosition + new Vector3(0.5f, 0.5f), chunkCoord, RNG); } } landTilemap.SetTiles(landTilePositions.ToArray(), landTiles.ToArray()); waterTilemap.SetTiles(waterTilePositions.ToArray(), waterTiles.ToArray()); }
private BiomePreset GetBiome(float height, float moisture, float heat) { List <BiomeTempData> biomeTemp = new List <BiomeTempData>(); foreach (BiomePreset biome in _biomes) { if (biome.MatchCondition(height, moisture, heat)) { biomeTemp.Add(new BiomeTempData(biome)); } } float curVal = 0.0f; BiomePreset biomeToReturn = null; foreach (BiomeTempData biome in biomeTemp) { if (biomeToReturn == null) { biomeToReturn = biome.biome; curVal = biome.GetDiffValue(height, moisture, heat); } else { if (biome.GetDiffValue(height, moisture, heat) < curVal) { biomeToReturn = biome.biome; curVal = biome.GetDiffValue(height, moisture, heat); } } } if (biomeToReturn == null) { biomeToReturn = _biomes[0]; } return(biomeToReturn); }
BiomePreset GetBiome(float height, float moisture, float heat) { BiomePreset biomeToReturn = null; List <BiomePreset> tempBiomes = new List <BiomePreset>(); foreach (BiomePreset biome in biomes) { if (biome.MatchCondition(height, moisture, heat)) { tempBiomes.Add(biome); } } float curValue = 0.0f; foreach (BiomePreset biome in tempBiomes) { float diffValue = (height - biome.minHeight) + (moisture - biome.minMoisture) + (heat - biome.minHeat); if (biomeToReturn == null) { biomeToReturn = biome; curValue = diffValue; } else if (diffValue < curValue) { biomeToReturn = biome; curValue = diffValue; } } if (biomeToReturn == null) { return(biomes[0]); } return(biomeToReturn); }
public BiomeTempData(BiomePreset preset) { biome = preset; }
void CreateMap() { // Create the height map heightMap = NoiseGenerator.Generate(width, height, scale, offset, heightWaves); // Create the moisture map moistureMap = NoiseGenerator.Generate(width, height, scale, offset, moistureWaves); // Create the heat map heatMap = NoiseGenerator.Generate(width, height, scale, offset, heatWaves); //creates an array for each of the pixels in the texture Color[] pixels = new Color[width * height]; int i = 0; // loop through each pixel for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // how do we want to display the debug map? switch (displayType) { case MapDisplay.Height: pixels[i] = heightDebugColors.Evaluate(heightMap[x, y]); break; case MapDisplay.Moisture: pixels[i] = moistureDebugColors.Evaluate(moistureMap[x, y]); break; case MapDisplay.Heat: pixels[i] = heatDebugColors.Evaluate(heatMap[x, y]); break; case MapDisplay.Biome: { BiomePreset biome = GetBiome(heightMap[x, y], moistureMap[x, y], heatMap[x, y]); pixels[i] = biome.debugColor; if (Application.isPlaying) { GameObject tile = Instantiate(tilePrefab, new Vector3(x, y, 0), Quaternion.identity); tile.GetComponent <SpriteRenderer>().sprite = biome.GetTileSprite(); tile.GetComponent <BoxCollider2D>(); } break; } } i++; } } // create the texture Texture2D tex = new Texture2D(width, height); tex.SetPixels(pixels); tex.filterMode = FilterMode.Point; tex.Apply(); // apply the texture to the raw image debugImage.texture = tex; SpawnEnemies(); }