Beispiel #1
0
    void Start()
    {
        TerrainManager manager = FindObjectOfType <TerrainManager>();

        if (manager == null || !manager.enabled)
        {
            Debug.LogError("Not found TerrainManager or is enabled on scene!");
            enabled = false;
            return;
        }

        Vector3 chunkSize = manager.chunkSize;

        Chunk.Coords startCoords = manager.ToChunkCoords(transform.position);

        Vector3 newPos = transform.position;

        if (startOnCenter)
        {
            newPos.x = startCoords.x * chunkSize.x + chunkSize.x / 2.0f;
            newPos.z = startCoords.y * chunkSize.z + chunkSize.z / 2.0f;
        }
        if (startOnSurface)
        {
            Chunk chunk = manager.GetChunk(startCoords);
            newPos.y = chunk.terrain.SampleHeight(newPos);
        }

        transform.position = newPos;
    }
Beispiel #2
0
    private bool IsOutOfRange(Chunk.Coords currentCoords, Chunk.Coords otherCoords)
    {
        float distanceX = Mathf.Abs(currentCoords.x - otherCoords.x);
        float distanceY = Mathf.Abs(currentCoords.y - otherCoords.y);

        return(distanceX > destoryRange || distanceY > destoryRange);
    }
Beispiel #3
0
 public bool ShowChunk(Chunk.Coords coords)
 {
     if (!chunkMap.ContainsKey(coords))
     {
         CreateChunk(coords);
         return(true);
     }
     return(false);
 }
Beispiel #4
0
 void Update()
 {
     if (ShouldCreateChunks())
     {
         Chunk.Coords coords = GetCurrentCoords();
         OnChangeChunk(coords);
         lastCreatedPosition = focus.position;
     }
 }
Beispiel #5
0
    public Chunk?TryGetChunk(Chunk.Coords coords)
    {
        Chunk chunk;

        if (chunkMap.TryGetValue(coords, out chunk))
        {
            return(chunk);
        }
        return(null);
    }
Beispiel #6
0
    private void OnChangeChunk(Chunk.Coords coords)
    {
        if (showChunksCoroutine != null)
        {
            StopCoroutine(showChunksCoroutine);
        }
        showChunksCoroutine = StartCoroutine(StartShowChunks(coords));

        RemoveChunksIfOutOfRange(coords);
    }
Beispiel #7
0
    private Terrain CreateTerrainForChunk(Chunk.Coords coords)
    {
        TerrainData terrainData = CreateAndSetupTerrainData();

        GameObject terrainObject = Terrain.CreateTerrainGameObject(terrainData);

        terrainObject.transform.position = new Vector3(chunkSize.x * coords.x, 0.0f, chunkSize.z * coords.y);
        terrainObject.transform.parent   = gameObject.transform;
        terrainObject.transform.name     = string.Format("Chunk ({0}, {1})", coords.x, coords.y);

        return(GetAndSetupTerrain(terrainObject));
    }
Beispiel #8
0
    private void RemoveChunksIfOutOfRange(Chunk.Coords currentCoords)
    {
        var chunkCoords = manager.LoadedChunksCoords;

        foreach (var coords in chunkCoords)
        {
            if (IsOutOfRange(currentCoords, coords))
            {
                manager.RemoveChunk(coords);
            }
        }
    }
Beispiel #9
0
    public Chunk GetChunk(Chunk.Coords coords)
    {
        Chunk?chunkOrNull = TryGetChunk(coords);

        if (chunkOrNull != null)
        {
            return((Chunk)chunkOrNull);
        }

        ShowChunk(coords);
        return((Chunk)TryGetChunk(coords));
    }
Beispiel #10
0
    public void RemoveChunk(Chunk.Coords coords)
    {
        Chunk?chunkOrNull = TryGetChunk(coords);

        if (chunkOrNull != null)
        {
            Chunk chunk = (Chunk)chunkOrNull;
            Destroy(chunk.terrain.gameObject);
            Destroy(chunk.terrain.terrainData);
            Destroy(chunk.terrain);
            chunkMap.Remove(coords);
        }
    }
Beispiel #11
0
    private IEnumerator StartShowChunks(Chunk.Coords cuurentCoords)
    {
        chunksToLoad      = GetChunkCoordsToLoad(cuurentCoords);
        chunksLoadedCount = 0;

        foreach (var coords in chunksToLoad)
        {
            manager.ShowChunk(coords);
            chunksLoadedCount++;
            yield return(null);
        }
        chunksToLoad      = null;
        chunksLoadedCount = 0;
    }
Beispiel #12
0
    private void SetupNeighbors(int x, int y)
    {
        Chunk.Coords coords = new Chunk.Coords(x, y);
        Chunk?       chunk  = TryGetChunk(x, y);

        if (chunk == null)
        {
            return;
        }

        Terrain left   = GetTerrainOrNull(coords.x - 1, coords.y);
        Terrain top    = GetTerrainOrNull(coords.x, coords.y + 1);
        Terrain right  = GetTerrainOrNull(coords.x + 1, coords.y);
        Terrain bottom = GetTerrainOrNull(coords.x, coords.y - 1);

        chunk.Value.terrain.SetNeighbors(left, top, right, bottom);
    }
Beispiel #13
0
    protected override void OnGenerateTerrain()
    {
        Chunk.Coords c = chunk.coords;
        heightmap = heightmapNoise.GetTiledMap(c.x, c.y, width, height);
        alphamap  = data.GetAlphamaps(0, 0, width, height);

        SetupSplats();
        SetupTrees();

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                float h = heightmap[y, x];
                PaintTexture(x, y, h);
                PlaceTree(x, y, h);
            }
        }
    }
Beispiel #14
0
    private void CreateChunk(Chunk.Coords coords)
    {
        Terrain terrain = CreateTerrainForChunk(coords);
        Chunk   chunk   = new Chunk();

        chunk.coords  = coords;
        chunk.terrain = terrain;
        generator.GenerateChunk(chunk);
        chunk.terrain.Flush();

        chunkMap.Add(chunk.coords, chunk);

        if (setupsNeighbors)
        {
            SetupNeighbors(chunk.coords.x, chunk.coords.y);
            SetupNeighbors(chunk.coords.x + 1, chunk.coords.y);
            SetupNeighbors(chunk.coords.x, chunk.coords.y + 1);
            SetupNeighbors(chunk.coords.x - 1, chunk.coords.y);
            SetupNeighbors(chunk.coords.x, chunk.coords.y - 1);
        }
    }
Beispiel #15
0
    private List <Chunk.Coords> GetChunkCoordsToLoad(Chunk.Coords coords)
    {
        List <Chunk.Coords> loadedChunk = manager.LoadedChunksCoords;
        List <Chunk.Coords> list        = new List <Chunk.Coords>();
        int r2 = createRange * createRange;

        for (int x = -createRange; x <= createRange; x++)
        {
            for (int y = -createRange; y <= createRange; y++)
            {
                Chunk.Coords c = new Chunk.Coords(coords.x + x, coords.y + y);

                bool inRange  = x * x + y * y < r2;
                bool isLoaded = loadedChunk.Contains(c);
                if (inRange && !isLoaded)
                {
                    list.Add(c);
                }
            }
        }

        return(list);
    }
Beispiel #16
0
 void Start()
 {
     Chunk.Coords coords = GetCurrentCoords();
     OnChangeChunk(coords);
     lastCreatedPosition = focus.position;
 }
Beispiel #17
0
 protected override void GenerateHeightmap()
 {
     Chunk.Coords c = chunk.coords;
     heightmap = noise.GetTiledMap(c.x, c.y, width, height);
 }