public BaseItem[] GetItemArrayByType(SpawnSettingType type) { switch (type) { case SpawnSettingType.Tree: return(trees.ToArray()); case SpawnSettingType.Stone: return(rocks.ToArray()); case SpawnSettingType.Plant: return(plants.ToArray()); case SpawnSettingType.Grass: return(grasses.ToArray()); case SpawnSettingType.Life: return(animals.ToArray()); case SpawnSettingType.Food: return(foods.ToArray()); case SpawnSettingType.Resources: return(resources.ToArray()); default: Debug.LogError("Database does not yet contain the selected spawntype"); return(null); } }
private IEnumerator AddFoliage(SpawnSettingType spawnType) { BiomeSetting biomeSettings = WorldSettings.GetSettingsByType(parent._biomeType); SpawnSetting spawnSettings = biomeSettings.GetSpawnSettingsByType(spawnType); float[] noiseSamples = parent.GetNoiseSamples(parent._worldPosition, spawnType); for (int x = 0; x < spawnSettings.resolution; x++) { for (int y = 0; y < spawnSettings.resolution; y++) { float sample = noiseSamples[y * spawnSettings.resolution + x]; if (sample > 0.3f) { int randomNumber = Random.Range(0, spawnSettings.spawnDensity + 1); if (randomNumber == 1) { parent.SpawnObject(spawnType, new Vector2(x, y), spawnSettings.resolution); if ((y * spawnSettings.resolution + x) % 20 == 0) { yield return(null); } } } } } }
// --------------- CHUNK CREATION --------------- // --------------- CHUNK CREATION --------------- // --------------- NOISE DEBUGGING --------------- // --------------- NOISE DEBUGGING --------------- #region NOISE DEBUGGING private Texture2D MakeNoiseTexture(Vector2 worldPos, SpawnSettingType spawnType) { BiomeSetting biomeSettings = WorldSettings.GetSettingsByType(_biomeType); SpawnSetting noiseSettings = biomeSettings.GetSpawnSettingsByType(spawnType); Texture2D noiseTex = new Texture2D(noiseSettings.resolution, noiseSettings.resolution, TextureFormat.RGB24, true); noiseTex.wrapMode = TextureWrapMode.Clamp; noiseTex.filterMode = FilterMode.Trilinear; noiseTex.anisoLevel = 9; Color[] pix = new Color[noiseTex.width * noiseTex.height]; float[] noiseSamples = GetNoiseSamples(worldPos, spawnType); for (int x = 0; x < noiseSettings.resolution; x++) { for (int y = 0; y < noiseSettings.resolution; y++) { float sample = noiseSamples[y * noiseSettings.resolution + x]; pix[y * noiseSettings.resolution + x] = new Color(sample, sample, sample); } } noiseTex.SetPixels(pix); noiseTex.Apply(); return(FlipTexture(noiseTex)); }
public float[] GetNoiseSamples(Vector2 worldPosition, SpawnSettingType spawnType) { BiomeSetting biomeSettings = WorldSettings.GetSettingsByType(_biomeType); SpawnSetting spawnSettings = biomeSettings.GetSpawnSettingsByType(spawnType); float[] noiseSamples = new float[spawnSettings.resolution * spawnSettings.resolution]; float frequency = spawnSettings.frequency; float resolution = spawnSettings.resolution; float x = 0.0F; while (x < spawnSettings.resolution) { float y = 0.0F; while (y < spawnSettings.resolution) { float xCoord = (worldPosition.x) * frequency + x / resolution * frequency; float yCoord = (worldPosition.y) * frequency + y / resolution * frequency; noiseSamples[(int)(y * spawnSettings.resolution + x)] = Mathf.PerlinNoise(xCoord, yCoord); // TODO: Significantly faster -> Look at simplex for more performance and Voronoi for other effects y++; } x++; } return(noiseSamples); }
public void SpawnObject(SpawnSettingType spawnType, Vector2 spawnPosition, int resolution) { float xStart = (_worldPosition.x * parent._chunkSize.x) - (parent._chunkSize.x / 2); float zStart = (_worldPosition.y * parent._chunkSize.y) - (parent._chunkSize.y / 2); Vector3 newSpawnPos = new Vector3(xStart + spawnPosition.x * (parent._chunkSize.x / resolution) + Random.Range(-.5f, .5f), 0, zStart + spawnPosition.y * (parent._chunkSize.y / resolution) + Random.Range(-.5f, .5f)); if (newSpawnPos.x < xStart + noSpawnZone[0] || newSpawnPos.x > xStart + parent._chunkSize.x - noSpawnZone[3] || newSpawnPos.z <zStart + noSpawnZone[1] || newSpawnPos.z> zStart + parent._chunkSize.y - noSpawnZone[2]) { return; } BaseItem[] possibleItems = myBiomeItems.GetItemArrayByType(spawnType); if (possibleItems == null || possibleItems.Length <= 0) { return; } BaseItem item = possibleItems[Random.Range(0, possibleItems.Length)]; if (item == null || item.gameObject == null) { return; } Ray ray = new Ray(new Vector3(newSpawnPos.x, 10, newSpawnPos.z), -Vector3.up); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, 10, (1 << 8))) { newSpawnPos = hitInfo.point; float radius = item.transform.localScale.x + 0.2f; if (Physics.CheckSphere(newSpawnPos, radius, ~(1 << 8))) { return; } } else { return; } GameObject tmpObj = (GameObject)GameObject.Instantiate(item.gameObject, newSpawnPos, Quaternion.identity); tmpObj.GetComponent <BaseItem>().OnCreate(newSpawnPos, Quaternion.identity, ground.transform); BasePickableItem pickable = tmpObj.GetComponent <BasePickableItem>(); if (pickable) { changeableEntities.Add(tmpObj); } else { nonChangeableObject.Add(tmpObj); } }
public SpawnSetting GetSpawnSettingsByType(SpawnSettingType spawnType) { if (spawnSettings == null || spawnSettings.Length <= 0) { Debug.LogWarning("The selected settings don't exist, using defaults"); return(new SpawnSetting()); } foreach (SpawnSetting n in spawnSettings) { if (n.noisePreset == spawnType) { return(n); } } return(new SpawnSetting()); }