Exemplo n.º 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");
    }