Exemple #1
0
        public static ObjectPoint[] CreateObjectPoints(int uniformSize, MeshSettings meshSettings, ResourceMapSettings resourceMapSettings, BiomeMap biomeMap, HeightMap heightMap, TerrainChunk terrainChunk)
        {
            string fileName = $"chunkInfo{terrainChunk.Coord.x}{terrainChunk.Coord.y}.dat";

            // Try to load a save file
            if (File.Exists(SaveDataManager.WorldDataPath + fileName))
            {
                return(ObjectMapLoader.LoadObjectMap(terrainChunk, SaveDataManager.WorldDataPath).ObjectPoints);
            }
            else
            {
                //ResourceMap resourceMap = ResourceMapGenerator.GenerateResourceMap(uniformSize, resourceMapSettings, terrainChunk.SampleCenter);

                ResourceMap resourceMap = ResourceMapGenerator.GenerateResourceMap(uniformSize, resourceMapSettings, terrainChunk.SampleCenter, biomeMap, heightMap);

                List <ObjectPoint> tempObjectPoints = new List <ObjectPoint>();

                // Resource points to object points
                foreach (var resourcePoint in resourceMap.resourcePoints)
                {
                    int x = resourcePoint.x;
                    int z = resourcePoint.z;

                    HeightMapLayer layer = heightMap.GetLayer(x + 1, z + 1);

                    // Don't spawn objects on water.
                    if (layer.IsWater)
                    {
                        continue;
                    }

                    float   height   = heightMap.Values[x + 1, z + 1];
                    Vector3 position = new Vector3(terrainChunk.Bounds.center.x, 0f, terrainChunk.Bounds.center.y) + new Vector3((x - (uniformSize - 1) / 2f) * meshSettings.MeshScale, height, (z - (uniformSize - 1) / 2f) * -meshSettings.MeshScale);

                    // Create a seeded System.Random for the rotation based on the position
                    float         a    = position.x + position.y;
                    float         b    = position.z + position.y;
                    System.Random rand = new System.Random((int)(0.5 * (a + b) * (a + b + 1) + b));

                    Quaternion rotation = Quaternion.Euler(new Vector3(0f, rand.Next(0, 360), 0f));

                    int chunkPartSize = uniformSize / meshSettings.ChunkPartSizeRoot + 1;
                    int coordX        = Mathf.FloorToInt(x / chunkPartSize) - 1;
                    int coordZ        = Mathf.FloorToInt(z / chunkPartSize) - 1;

                    Vector2 chunkPartCoords = terrainChunk.Coord * meshSettings.ChunkPartSizeRoot + new Vector2(coordX, -coordZ);

                    tempObjectPoints.Add(new ObjectPoint(position, rotation, resourcePoint.biomeId, resourcePoint.worldResourcePrefabId, chunkPartCoords.x, chunkPartCoords.y));
                }

                return(tempObjectPoints.ToArray());
            }
        }
        /// <summary>
        /// Get terrain properties at the given index.
        /// </summary>
        /// <param name="x">The X index.</param>
        /// <param name="z">The Z idnex.</param>
        /// <returns>Biome properties etc.</returns>
        public TerrainInfo GetTerrainInfoFromIndex(int x, int z)
        {
            if (!Loaded)
            {
                Debug.LogError("TerrainChunk hasn't loaded");
                return(new TerrainInfo());
            }

            Biome          biome  = TerrainChunk.DataMap.GetBiome(x, z);
            float          height = TerrainChunk.DataMap.GetHeight(x, z);
            HeightMapLayer layer  = TerrainChunk.DataMap.GetLayer(x, z);

            Vector3 pos = transform.position + new Vector3((x - (TerrainChunk.DataMap.UniformSize - 1) / 2f) * TerrainChunk.MeshSettings.MeshScale, height, (z - (TerrainChunk.DataMap.UniformSize - 1) / 2f) * -TerrainChunk.MeshSettings.MeshScale);

            return(new TerrainInfo(biome, layer, pos));
        }
        /// <summary>
        /// Get terrain properties at the given world location.
        /// </summary>
        /// <param name="worldX">The X position in world space that lies within the TerrainChunk bounds.</param>
        /// <param name="worldZ">The Z position in world space that lies within the TerrainChunk bounds.</param>
        /// <returns>Biome properties etc.</returns>
        public TerrainInfo GetTerrainInfoFromWorldPoint(float worldX, float worldZ)
        {
            if (!Loaded)
            {
                Debug.LogError("TerrainChunk hasn't loaded");
                return(new TerrainInfo());
            }

            Vector3 distance = new Vector3(worldX, 0f, worldZ) - new Vector3(transform.position.x, 0f, transform.position.z);

            float xPercentage = distance.x / TerrainChunk.Bounds.size.x * 100f + 50f; // +50 since distance from middle is on scale of 0 to 50 or 0 to -50
            float zPercentage = distance.z / TerrainChunk.Bounds.size.y * 100f + 50f; // +50 since distance from middle is on scale of 0 to 50 or 0 to -50

            int xIndex = Mathf.RoundToInt((TerrainChunk.DataMap.UniformSize - 1) / 100f * xPercentage);
            int zIndex = TerrainChunk.DataMap.UniformSize - 1 - Mathf.RoundToInt((TerrainChunk.DataMap.UniformSize - 1) / 100f * zPercentage); // Inversed

            // These offsets are needed for the HeightMap, since that [,] is 2 longer in width and height because those are needed for normal calculations.
            int indexOffsetX = 0;
            int indexOffsetZ = 0;

            if (xPercentage < 50)
            {
                indexOffsetX = 0;
                indexOffsetZ = zPercentage < 50f ? 1 : 0;
            }
            else
            {
                indexOffsetX = 1;
                indexOffsetZ = zPercentage < 50f ? 1 : 0;
            }

            Biome          biome  = TerrainChunk.DataMap.BiomeMap.GetBiome(xIndex, zIndex);
            HeightMapLayer layer  = TerrainChunk.DataMap.GetLayer(xIndex + indexOffsetX, zIndex + indexOffsetZ);
            float          height = TerrainChunk.DataMap.GetHeight(xIndex + indexOffsetX, zIndex + indexOffsetZ);

            Vector3 pos = transform.position + new Vector3((xIndex - TerrainChunk.DataMap.UniformSize / 2f) * TerrainChunk.MeshSettings.MeshScale, height, (zIndex - TerrainChunk.DataMap.UniformSize / 2f) * -TerrainChunk.MeshSettings.MeshScale);

            return(new TerrainInfo(biome, layer, pos));
        }
 public TerrainInfo(Biome biome, HeightMapLayer heightMapLayer, Vector3 point)
 {
     this.Biome         = biome;
     this.Layer         = heightMapLayer;
     this.WorldPosition = point;
 }