/// <summary>
    /// 更新当前块列表
    /// </summary>
    /// <param name="actulChunkList">实际块列表</param>
    /// <param name="currentVector">当前中心块位置</param>
    private void UpdateCurrentChunkList(List <ChunkExample> actulChunkList, ChunkVector2 currentVector)
    {
        for (int i = 0; i < currentCacheChunkList.Count; i++)
        {
            ChunkExample chunk = currentCacheChunkList[i];
            if (!actulChunkList.Contains(chunk))   //实际列表里若不存在当前列表的指定元素 则卸载删除
            {
                chunk.DisplayUnload();             //卸载不存在于实际块列表的块

                currentCacheChunkList.RemoveAt(i); //移除当前块列表中不存在与实际列表的块

                i--;                               //在遍历列表时删除列表元素 记得索引-1 否则无法正确遍历
            }
            else
            {
                actulChunkList.Remove(chunk);//实际块列表移除和当前块列表中相同的元素 注:移除完毕后,实际块列表中的元素
                //先获取chunk的实际状态
                ChunkState actualState = GetChunkStateByRelativePosition(chunk, currentVector);
                chunk.Excute(actualState);
            }
        }

        for (int i = 0; i < actulChunkList.Count; i++)
        {
            ChunkExample chunk = actulChunkList[i];
            //先获取chunk的实际状态
            ChunkState actualState = GetChunkStateByRelativePosition(chunk, currentVector);
            chunk.Excute(actualState);
            currentCacheChunkList.Add(chunk);//这里添加完以后,当前块列表将与实际块列表保持一致
        }
    }
    /// <summary>
    /// 获取实际块列表
    /// </summary>
    /// <param name="currentVector">当前中心块位置</param>
    /// <returns></returns>
    List <ChunkExample> GetActualChunkList(ChunkVector2 currentVector)
    {
        int currentRow = currentVector.rowNum;
        int currentCol = currentVector.colNum;

        int index = 0;

        for (int i = -2; i <= 2; i++)
        {
            for (int j = -2; j <= 2; j++)
            {
                var temp = expectChunkVectorList[index];
                temp.rowNum = currentRow + i;
                temp.colNum = currentCol + j;
                expectChunkVectorList[index] = temp;
                index++;
            }
        }
        List <ChunkExample> actulDisplayChunkList = new List <ChunkExample>();

        for (int i = 0; i < expectChunkVectorList.Count; i++)
        {
            if (m_chunkMap.ContainsKey(expectChunkVectorList[i]))
            {
                var chunk = m_chunkMap[expectChunkVectorList[i]];
                actulDisplayChunkList.Add(chunk);
            }
        }
        return(actulDisplayChunkList);
    }
 bool IsChange(ChunkVector2 current)
 {
     if (m_currentVector != current)
     {
         m_currentVector = current;
         return(true);
     }
     return(false);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 玩家所在块是否发生改变
 /// </summary>
 /// <param name="realtimePos"></param>
 /// <returns></returns>
 bool IsChange(ChunkVector2 realtimePos)
 {
     if (m_currentPos != realtimePos)
     {
         m_currentPos = realtimePos;
         return(true);
     }
     return(false);
 }
Exemplo n.º 5
0
    protected virtual void InitMap()
    {
        //先确定玩家位置,得到玩家所在块的位置
        ChunkVector2 currentPos = GetCurrentChunkVector(m_player.position);

        m_currentPos = currentPos;
        //利用块的位置获取实际块列表
        List <ChunkVector2> actChunkList = GetActualChunkList(currentPos);

        //再加载实际列表中的所有块
        UpdateCurrentChunkList(actChunkList, currentPos);
    }
Exemplo n.º 6
0
    void Start()
    {
        for (int i = 0; i < m_row; i++)
        {
            for (int j = 0; j < m_col; j++)
            {
                ChunkVector2 vector = new ChunkVector2(i, j);
                m_chunkMap[vector] = new Chunk(vector);
            }
        }


        InitMap();
    }
    protected virtual void InitMap()
    {
        for (int i = 0; i < m_row; i++)
        {
            for (int j = 0; j < m_col; j++)
            {
                Vector3 vect = new Vector3();
                vect.x = j * chunkLength;
                vect.z = i * chunkLength;
                GameObject go = Instantiate(chunkPrefab, vect, Quaternion.identity) as GameObject;
                go.name = string.Format("chunk[{0}][{1}]", i, j);

                ChunkExample chunk    = go.GetComponent <ChunkExample>();
                ChunkVector2 position = new ChunkVector2(i, j);
                chunk.position       = position;
                m_chunkMap[position] = chunk;
            }
        }
    }
Exemplo n.º 8
0
    /// <summary>
    /// 获取指定块的相对状态
    /// </summary>
    /// <param name="specified">指定块</param>
    /// <param name="relativeVector">参照块坐标</param>
    /// <returns>相对块状态</returns>
    ChunkState GetChunkStateByRelativePosition(ChunkVector2 specified, ChunkVector2 relative)
    {
        int rowAmount = Mathf.Abs(specified.rowNum - relative.rowNum);
        int colAmount = Mathf.Abs(specified.colNum - relative.colNum);

        if (rowAmount > 2 || colAmount > 2)
        {
            return(ChunkState.UnLoad);
        }
        if (rowAmount == 2 || colAmount == 2)
        {
            return(ChunkState.Cache);
        }
        if (rowAmount <= 1 || colAmount <= 1)
        {
            return(ChunkState.Display);
        }

        return(ChunkState.UnLoad);
    }
Exemplo n.º 9
0
    /// <summary>
    /// 获取实际块列表
    /// </summary>
    /// <param name="currentVector">当前中心块位置</param>
    /// <returns></returns>
    List <ChunkVector2> GetActualChunkList(ChunkVector2 currentVector)
    {
        List <ChunkVector2> expectChunkPosList = new List <ChunkVector2>();
        int currentRow = currentVector.rowNum;
        int currentCol = currentVector.colNum;

        for (int i = -2; i <= 2; i++)
        {
            for (int j = -2; j <= 2; j++)
            {
                int expRow = currentRow + i;
                int expCol = currentCol + j;
                if (expRow < 0 || expCol < 0 || expRow > m_row - 1 || expCol > m_col - 1)
                {
                    continue;
                }
                expectChunkPosList.Add(new ChunkVector2(expRow, expCol));
            }
        }
        return(expectChunkPosList);
    }
Exemplo n.º 10
0
    /// <summary>
    /// 对比当前块列表与实际块列表,并更新当前块列表
    /// </summary>
    /// <param name="actulChunkList">实际块列表</param>
    /// <param name="currentPos">当前中心块位置</param>
    private void UpdateCurrentChunkList(List <ChunkVector2> actulChunkList, ChunkVector2 currentPos)
    {
        for (int i = 0; i < m_currentChunkList.Count; i++)
        {
            ChunkVector2 pos   = m_currentChunkList[i];
            Chunk        chunk = m_chunkMap[pos];
            if (!actulChunkList.Contains(pos))  //实际块列表里若不存在当前列表的指定元素 则卸载删除
            {
                chunk.Unload();                 //卸载不存在于实际块列表的块

                m_currentChunkList.RemoveAt(i); //移除当前块列表中不存在与实际块列表的块

                i--;                            //在遍历列表时删除列表元素 记得索引-1 否则无法正确遍历
            }
            else
            {
                actulChunkList.Remove(pos);//实际块列表移除和当前块列表中相同的元素 注:移除完毕后,实际块列表中的元素
                //先获取chunk的实际状态
                ChunkState actualState = GetChunkStateByRelativePosition(pos, currentPos);

                chunk.Update(actualState);
            }
        }

        for (int i = 0; i < actulChunkList.Count; i++)
        {
            ChunkVector2 pos   = actulChunkList[i];
            Chunk        chunk = m_chunkMap[pos];
            //先获取chunk的实际状态
            ChunkState actualState = GetChunkStateByRelativePosition(pos, currentPos);
            //使用实际状态去更新当前状态
            chunk.Update(actualState);

            m_currentChunkList.Add(pos);//这里添加完以后,当前块列表将与实际块列表保持一致
        }

        Resources.UnloadUnusedAssets();
    }
Exemplo n.º 11
0
    /// <summary>
    /// 获取指定块的相对状态
    /// </summary>
    /// <param name="chunk">指定块</param>
    /// <param name="relativeVector">参照块坐标</param>
    /// <returns>相对块状态</returns>
    ChunkState GetChunkStateByRelativePosition(ChunkExample chunk, ChunkVector2 relativePosition)
    {
        ChunkVector2 vector = chunk.position;


        int rowAmount = Mathf.Abs(vector.rowNum - relativePosition.rowNum);
        int colAmount = Mathf.Abs(vector.colNum - relativePosition.colNum);

        if (rowAmount > 2 || colAmount > 2)
        {
            return(ChunkState.UnLoad);
        }
        if (rowAmount == 2 || colAmount == 2)
        {
            return(ChunkState.Cache);
        }
        if (rowAmount <= 1 || colAmount <= 1)
        {
            return(ChunkState.Display);
        }

        return(ChunkState.UnLoad);
    }
Exemplo n.º 12
0
 public Chunk(ChunkVector2 position)
     : this(position.rowNum, position.colNum)
 {
 }
Exemplo n.º 13
0
 /// <summary>
 /// 创建一个块对象
 /// </summary>
 /// <param name="rowNum">在块列表中的第几行</param>
 /// <param name="colNum">在块列表中的第几列</param>
 public Chunk(int rowNum, int colNum)
 {
     m_position = new ChunkVector2(rowNum, colNum);
     m_resPath  = string.Format("TerrainPrefab/Terrain_Slice_{0}_{1}", (rowNum + 1), (colNum + 1));
 }