Exemplo n.º 1
0
    /// <summary>
    /// 生产图块 创建地图
    /// </summary>
    private void SpawnChunk()
    {
        if (Chunk7Thread.working)
        {
            return;
        }

        float        lastDis = 99999999;
        Chunk7Thread target  = null;

        foreach (Chunk7Thread chunk in _chunks)
        {
            float dis = Vector3.Distance(chunk.transform.position, player.position);
            if (dis < lastDis)
            {
                if (chunk.ready == false)
                {
                    lastDis = dis;
                    target  = chunk;
                }
            }
        }

        if (target != null)
        {
            StartCoroutine(target.CreateMap());
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// 图块控制
    /// </summary>
    private void BlockContrller()
    {
        RaycastHit hitInfo;

        if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hitInfo, 10f))
        {
            Vector3 pos = hitInfo.point - hitInfo.normal / 2;
            //Vector3 pos = new Vector3(hitX, hitY, hitZ);
            _hightBlock.transform.position = DataUtil.CeilToInt(pos);

            if (Input.GetMouseButton(0))
            {
                Chunk7Thread chunk = GetChunkByWorldPos(DataUtil.CeilToInt(pos));
                chunk.SetBlock(pos, null);
            }
            else if (Input.GetKeyDown(KeyCode.Q))
            {
                pos = hitInfo.point + hitInfo.normal / 2;
                Chunk7Thread chunk = GetChunkByWorldPos(DataUtil.CeilToInt(pos));
                chunk.SetBlock(pos, BlockMap.GetBlock("TNT"));
            }
        }
        else
        {
            _hightBlock.transform.position = new Vector3(10000, 10000, 10000);
        }
    }
Exemplo n.º 3
0
    public void AddChunk(int chunkX, int chunkY, int chunkZ)
    {
        GameObject go = Instantiate(_chunkPrefab, new Vector3(
                                        chunkX * Chunk.length, chunkY * Chunk.width, chunkZ * Chunk.height), Quaternion.identity);
        Chunk7Thread chunk = go.GetComponent <Chunk7Thread>();

        chunk.Init(chunkX, chunkY, chunkZ);
        _chunks.Add(chunk);
    }
Exemplo n.º 4
0
 /// <summary>
 /// 销毁图块
 /// </summary>
 private void DestoryChunk()
 {
     for (int i = _chunks.Count - 1; i >= 0; i--)
     {
         Chunk7Thread chunk = _chunks[i];
         float        dis   = Vector3.Distance(chunk.transform.position, player.position);
         if (dis > (_preloadRange + Chunk.width * 2))
         {
             _chunks.Remove(chunk);
             Destroy(chunk.gameObject);
         }
     }
 }
Exemplo n.º 5
0
    /// <summary>
    /// 加载图块周围的图块
    /// </summary>
    private void LoadChunkAround()
    {
        if (!_preload)
        {
            return;
        }

        for (int i = 0; i < _chunks.Count; i++)
        {
            Chunk7Thread chunk = _chunks[i];
            chunk.LoadAround();
        }
    }
Exemplo n.º 6
0
    public void SetBlock(Vector3 pos, Block block)
    {
        Vector3 localPos = pos - transform.position;
        int     blockX   = Mathf.CeilToInt(localPos.x);
        int     blockY   = Mathf.CeilToInt(localPos.y);
        int     blockZ   = Mathf.CeilToInt(localPos.z);

        //print("pos: " + pos.x + ", " + pos.y + ", " + pos.z);
        //print("local pos: " + blockX + ", " + blockY + ", " + blockZ);
        _map[blockX, blockY, blockZ] = block;

        StartCoroutine(RebuildMesh());
        if (block != null)
        {
            return;
        }

        #region 重构相邻的图块,补充未显示的面
        // 右边
        if (blockX == length - 1)
        {
            if (_rightChunk == null)
            {
                _rightChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX + 1, blockY, blockZ);
            }
            StartCoroutine(_rightChunk.RebuildMesh());
            //Debug.Log("rihgt : " + _rightChunk.name);
        }
        // 左边
        if (blockX == 0)
        {
            if (_leftChunk == null)
            {
                _leftChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX - 1, chunkY, chunkZ);
            }
            StartCoroutine(_leftChunk.RebuildMesh());
            //Debug.Log("left : " + _leftChunk.name);
        }
        // 前面
        if (blockZ == 0)
        {
            if (_frontChunk == null)
            {
                _frontChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY, blockZ - 1);
            }
            StartCoroutine(_frontChunk.RebuildMesh());
            //Debug.Log("front : " + _frontChunk.name);
        }
        // 后面
        if (blockZ == width - 1)
        {
            if (_backChunk == null)
            {
                _backChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY, blockZ + 1);
            }
            StartCoroutine(_backChunk.RebuildMesh());
            //Debug.Log("back : " + _backChunk.name);
        }
        // 上面
        if (blockY == height - 1)
        {
            if (_topChunk == null)
            {
                _topChunk = ChunkMgr7Thread.GetChunkByChunkPos(blockX, blockY + 1, blockZ);
            }
            StartCoroutine(_topChunk.RebuildMesh());
            //Debug.Log("top : " + _topChunk.name);
        }
        // 下面
        if (blockY == 0)
        {
            if (_bottomChunk == null)
            {
                _bottomChunk = ChunkMgr7Thread.GetChunkByWorldPos(blockX, blockY - 1, blockZ);
            }
            StartCoroutine(_bottomChunk.RebuildMesh());
            //Debug.Log("bottom : " + _bottomChunk.name);
        }
        #endregion
    }
Exemplo n.º 7
0
    /// <summary>
    /// 控制重建网格后,显示的面
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="z"></param>
    /// <returns>是否显示</returns>
    private bool IsRebuildBlockTransparent(int x, int y, int z)
    {
        #region 检查周围是否有图块,没有则显示该面
        Vector3 worldPos = DataUtil.FloorToInt(new Vector3(x, y, z) + transform.position);
        // 右边
        if (x >= length)
        {
            if (_rightChunk == null)
            {
                _rightChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX + 1, chunkY, chunkZ);
            }
            if (_rightChunk != null && _rightChunk != this && _rightChunk.ready)
            {
                return(_rightChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

        // 左边
        if (x < 0)
        {
            if (_leftChunk == null)
            {
                _leftChunk = ChunkMgr7Thread.GetChunkByChunkPos(chunkX - 1, chunkY, chunkZ);
            }
            if (_leftChunk != null && _leftChunk != this && _leftChunk.ready)
            {
                return(_leftChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

        // 前面
        if (z < 0)
        {
            if (_frontChunk == null)
            {
                _frontChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos);
            }
            if (_frontChunk != null && _frontChunk != this && _frontChunk.ready)
            {
                return(_frontChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

        // 后面
        if (z >= width)
        {
            if (_backChunk == null)
            {
                _backChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos);
            }
            if (_backChunk != null && _backChunk != this && _backChunk.ready)
            {
                return(_backChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

        // 上面
        if (y >= height)
        {
            if (_topChunk == null)
            {
                _topChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos);
            }
            if (_topChunk != null && _topChunk != this && _topChunk.ready)
            {
                return(_topChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

        // 下面
        if (y < 0)
        {
            if (_bottomChunk == null)
            {
                _bottomChunk = ChunkMgr7Thread.GetChunkByWorldPos(worldPos);
            }
            if (_bottomChunk != null && _bottomChunk != this && _bottomChunk.ready)
            {
                return(_bottomChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }
        #endregion

        if (_map[x, y, z] == null)
        {
            return(true);
        }

        return(false);
    }
Exemplo n.º 8
0
    /// <summary>
    /// 加载周围的图块
    /// </summary>
    public void LoadAround()
    {
        // 左边
        if (_leftChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3((chunkX - 1) * Chunk.length, chunkY * Chunk.height, chunkZ * Chunk.width)))
            {
                return;
            }

            _leftChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX - 1, chunkY, chunkZ));
            if (_leftChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX - 1, chunkY, chunkZ);
            }
        }
        // 右边
        if (_rightChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3((chunkX + 1) * Chunk.length, chunkY * Chunk.height, chunkZ * Chunk.width)))
            {
                return;
            }

            _rightChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX + 1, chunkY, chunkZ));
            if (_rightChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX + 1, chunkY, chunkZ);
            }
        }
        // 前面
        if (_frontChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3(chunkX * Chunk.length, chunkY * Chunk.height, (chunkZ - 1) * Chunk.width)))
            {
                return;
            }

            _frontChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX, chunkY, chunkZ - 1));
            if (_frontChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX, chunkY, chunkZ - 1);
            }
        }
        // 后面
        if (_backChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3(chunkX * Chunk.length, chunkY * Chunk.height, (chunkZ + 1) * Chunk.width)))
            {
                return;
            }

            _backChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX, chunkY, chunkZ + 1));
            if (_backChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX + 1, chunkY, chunkZ + 1);
            }
        }
        // 上面
        if (_topChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3(chunkX * Chunk.length, (chunkY + 1) * Chunk.height, chunkZ * Chunk.width)))
            {
                return;
            }

            _topChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX, chunkY + 1, chunkZ));
            if (_topChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX, chunkY + 1, chunkZ);
            }
        }
        // 下面
        if (_bottomChunk == null)
        {
            if (!ChunkMgr7Thread.Instance().IsInPreLoadRange(
                    new Vector3(chunkX * Chunk.length, (chunkY - 1) * Chunk.height, chunkZ * Chunk.width)))
            {
                return;
            }

            _bottomChunk = ChunkMgr7Thread.GetChunkByChunkPos(new Vector3(chunkX, chunkY - 1, chunkZ));
            if (_bottomChunk == null)
            {
                ChunkMgr7Thread.Instance().AddChunk(chunkX, chunkY - 1, chunkZ);
            }
        }
    }