예제 #1
0
    public void GenerateNewChunk()
    {
        Stopwatch time = new Stopwatch();

        time.Start();

        WorldChunk newChunk = Instantiate(_worldChunk);

        // First chunk
        if (_chunks.Count == 0)
        {
            newChunk.Initialize(this, null, EDirection.None);
            var animator = newChunk.GetComponent <Animator>();

            if (animator != null)
            {
                Destroy(animator);
            }
        }
        else
        {
            // Get the world chunks that don't have all their neighbor yet
            List <WorldChunk> expandableChunks = new List <WorldChunk>();

            foreach (var chunk in _chunks)
            {
                List <EDirection> currentChunkEmptyNeighbors = chunk.GetEmptyNeighbors();

                if (currentChunkEmptyNeighbors.Count > 0)
                {
                    expandableChunks.Add(chunk);
                }
            }

            WorldChunk        randomExpandableChunk = expandableChunks[Random.Range(0, expandableChunks.Count)];
            List <EDirection> emptyNeighbors        = randomExpandableChunk.GetEmptyNeighbors();
            int        randomDirectionIndex         = Random.Range(0, emptyNeighbors.Count);
            EDirection randomDirection = emptyNeighbors[randomDirectionIndex];

            newChunk.Initialize(this, randomExpandableChunk, GetInverseDirection(randomDirection));
        }

        _chunks.Add(newChunk);

        time.Stop();
        Debug.Log($"Time to generate a new chunk: {time.ElapsedMilliseconds}ms");

        time.Restart();
        // Really time consuming => find a solution to build local chunk?
        GameManager.Instance.NavMeshSurface.BuildNavMesh();
        time.Stop();

        Debug.Log($"Time to build navmesh: {time.ElapsedMilliseconds}ms");
    }
예제 #2
0
    public WorldChunk InitializeChunk(Vector3Int pos)
    {
        GameObject go = new GameObject("Chunk [" + pos.x + "," + pos.y + "," + pos.z + "]");

        go.transform.parent = this.transform;
        WorldChunk chunk = go.AddComponent <WorldChunk>();

        // TODO: Have chunk load these from resources
        chunk.blockAtlas           = this.blockAtlas;
        chunk.chunkOpaqueMaterial  = this.chunkOpaqueMaterial;
        chunk.chunkWaterMaterial   = this.chunkWaterMaterial;
        chunk.chunkFoliageMaterial = this.chunkFoliageMaterial;

        //Debug.Log("Init Chunk [" + pos.x + "," + pos.y + "," + pos.z + "]");
        chunk.Initialize(pos, chunkSize, seed);
        chunk.chunkManager = this;

        return(chunk);
    }