示例#1
0
    public void UpdateChunksFromPlayerPosition()
    {
        if (m_CurrentChunk == null)
        {
            return;
        }

        Vector3 playerPos       = m_Player.transform.position;
        Bounds  currChunkBounds = m_CurrentChunk.GetComponent <BoxCollider2D>().bounds;

        //Nothing to do if we're still in the current chunk
        if (currChunkBounds.Contains(new Vector3(playerPos.x,
                                                 playerPos.y,
                                                 m_CurrentChunk.transform.position.z)))
        {
            return;
        }

        Bounds     prevChunkBounds = m_CurrentChunk.m_Prev.GetComponent <BoxCollider2D>().bounds;
        WorldChunk newNextChunk, newCurrChunk, newPrevChunk = null;

        if (prevChunkBounds.Contains(new Vector3(playerPos.x,
                                                 playerPos.y,
                                                 m_CurrentChunk.transform.position.z)))
        {
            WorldChunk oldNext = m_CurrentChunk.m_Next;
            Destroy(oldNext.gameObject);

            newNextChunk      = m_CurrentChunk;
            newNextChunk.name = "next_chunk";

            newCurrChunk      = m_CurrentChunk.m_Prev;
            newCurrChunk.name = "current_chunk";

            newPrevChunk = CreateChunk(newCurrChunk.transform.position + new Vector3(0f, 10f, 0f), "prev_chunk");

            m_CurrentChunk        = newCurrChunk;
            m_CurrentChunk.m_Prev = newPrevChunk;
            m_CurrentChunk.m_Next = newNextChunk;

            return;
        }

        WorldChunk oldPrev = m_CurrentChunk.m_Prev;

        Destroy(oldPrev.gameObject);

        newPrevChunk      = m_CurrentChunk;
        newPrevChunk.name = "prev_chunk";

        newCurrChunk      = m_CurrentChunk.m_Next;
        newCurrChunk.name = "current_chunk";

        newNextChunk = CreateChunk(newCurrChunk.transform.position + new Vector3(0f, -10f, 0f), "next_chunk");

        m_CurrentChunk        = newCurrChunk;
        m_CurrentChunk.m_Prev = newPrevChunk;
        m_CurrentChunk.m_Next = newNextChunk;
    }
示例#2
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");
    }