Exemple #1
0
        public float GetHeightAt(Vector2 worldCoords)
        {
            TerrainChunk chunk = terrainChunks[mapManager.mapGenerator.GetRelativeChunksCoordsAt(worldCoords)];

            /*Vector2 absoluteChunkCoords = chunk.transform.position.ToVector2WithoutY();
             * float chunkAbsoluteSize = mapGenerator.mapConfiguration.chunkWorldSize;
             * Vector2 distanceFromChunkCenter = new Vector2(worldCoords.x - absoluteChunkCoords.x, worldCoords.y - absoluteChunkCoords.y);
             * float chunkAbsoluteWidth = mapGenerator.mapConfiguration.chunkWorldSize;
             * int heightMapWidth = chunk.heightMapAbsolute.values.GetLength(0);
             * int heightMapCoordsX = Mathf.RoundToInt(chunkAbsoluteWidth / heightMapWidth * distanceFromChunkCenter.x + heightMapWidth / 2f);//(int)(coords.x * chunkAbsoluteSize / 2f / absoluteChunkCoords.x);
             * int heightMapCoordsY = Mathf.RoundToInt(heightMapWidth - (chunkAbsoluteWidth / heightMapWidth * distanceFromChunkCenter.y + heightMapWidth / 2f));//(int)(coords.y * chunkAbsoluteSize / 2f / absoluteChunkCoords.y);
             * //Debug.Log($"CHECKING HEIGHT AT {heightMapCoordsX}, {heightMapCoordsY}. coords = {coords}, chunkAbsoluteSize = {chunkAbsoluteSize}, absoluteChunkCoords = {absoluteChunkCoords}");
             * return chunk.heightMapAbsolute.values[heightMapCoordsX,heightMapCoordsY];*/

            return(TerrainMeshGenerator.GetHeight(chunk.heightMapAbsolute.values, mapManager.mapConfiguration, worldCoords - chunk.transform.position.ToVector2WithoutY()));
        }
Exemple #2
0
        /// <summary>
        /// Updates the TerrainChunks by creating it (if missing) or updating its visuals.
        /// </summary>
        /// <param name="clearPreviousTerrain">If existent, should the previously created terrain be deleted?</param>
        private void UpdateChunks(bool clearPreviousTerrain)
        {
            //Debug.Log($"Creating and/or updating TerrainChunks {(clearPreviousTerrain? "previously deleting" : "without destroying")} the existing ones.");

            if (clearPreviousTerrain)
            {
                Delete();
            }
            int chunksAtSideOfCentralRow = totalChunksInMapRow / 2;

            loadingChunks = totalChunksInMapRow * totalChunksInMapRow;

            terrainFullyLoadCallback += OnTerrainFullyLoad;
            int currentViewerChunkCordX = Mathf.RoundToInt(viewerPosition.x / mapManager.mapConfiguration.chunkWorldSize);
            int currentViewerChunkCordY = Mathf.RoundToInt(viewerPosition.y / mapManager.mapConfiguration.chunkWorldSize);

            for (int yOffset = -chunksAtSideOfCentralRow; yOffset <= chunksAtSideOfCentralRow; yOffset++)
            {
                for (int xOffset = -chunksAtSideOfCentralRow; xOffset <= chunksAtSideOfCentralRow; xOffset++)
                {
                    //Debug.Log("XXX");
                    Vector2Int chunkIndex = new Vector2Int(currentViewerChunkCordX + xOffset, currentViewerChunkCordY + yOffset);

                    //if (!alreadyUpdatedChunkCoords.Contains(viewedChunkCoord)) {
                    if (terrainChunks.ContainsKey(chunkIndex))
                    {
                        terrainChunks[chunkIndex].UpdateChunk();
                    }
                    else
                    {
                        GameObject   chunkGameObject = Instantiate(chunkPrefab);
                        TerrainChunk terrainChunk    = chunkGameObject.GetComponentRequired <TerrainChunk>();
                        terrainChunk.Setup(chunkIndex, detailLevels, this.transform, mapManager);
                        terrainChunks.Add(chunkIndex, terrainChunk);
                        //newChunk.onVisibilityChanged += OnTerrainChunkVisibilityChanged;
                        terrainChunk.Load(CompletionRegisterer);
                        chunkGameObject.name = $"Chunk {chunkIndex} ({chunkGameObject.transform.position})";
                    }
                    //}
                }
            }
        }