Example #1
0
    private void SpawnChunk()
    {
        if (Chunk6Load.working)
        {
            return;
        }

        float      lastDis = 99999999;
        Chunk6Load target  = null;

        for (int i = 0; i < _chunks.Count; i++)
        {
            Chunk6Load chunk = _chunks[i];
            float      dis   = Vector3.Distance(chunk.transform.position, player.position);
            if (dis < lastDis)
            {
                if (chunk.ready == false)
                {
                    lastDis = dis;
                    target  = chunk;
                }
            }
        }

        if (target != null)
        {
            target.CreateMap();
        }
    }
Example #2
0
    private void Update()
    {
        if (_preload)
        {
            for (float x = player.position.x - _preloadRange; x < player.position.x + _preloadRange; x += Chunk.length)
            {
                int chunkX = Mathf.FloorToInt(x / Chunk.length);
                for (float z = player.position.z - _preloadRange; z < player.position.z + _preloadRange; z += Chunk.width)
                {
                    int chunkZ = Mathf.FloorToInt(z / Chunk.width);
                    for (float y = player.position.y - _preloadRange; y < player.position.y + _preloadRange; y += Chunk.height)
                    {
                        int chunkY = Mathf.FloorToInt(y / Chunk.height);
                        if (GetChunkByChunkPos(chunkX, chunkY, chunkZ) == null)
                        {
                            GameObject go = Instantiate(_chunkPrefab, new Vector3(
                                                            chunkX * Chunk.length, chunkY * Chunk.width, chunkZ * Chunk.height), Quaternion.identity);
                            Chunk6Load chunk = go.GetComponent <Chunk6Load>();
                            chunk.Init(chunkX, chunkY, chunkZ);
                            _chunks.Add(chunk);
                        }
                    }
                }
            }

            SpawnChunk();
        }

        DestoryChunk();
        BlockContrller();
    }
Example #3
0
    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))
            {
                Chunk6Load chunk = GetChunkByWorldPos(DataUtil.CeilToInt(pos));
                chunk.SetBlock(pos, null);
            }
            else if (Input.GetKeyDown(KeyCode.Q))
            {
                pos = hitInfo.point + hitInfo.normal / 2;
                Chunk6Load chunk = GetChunkByWorldPos(DataUtil.CeilToInt(pos));
                chunk.SetBlock(pos, BlockMap.GetBlock("TNT"));
            }
        }
        else
        {
            _hightBlock.transform.position = new Vector3(10000, 10000, 10000);
        }
    }
Example #4
0
 private void DestoryChunk()
 {
     for (int i = _chunks.Count - 1; i >= 0; i--)
     {
         Chunk6Load chunk = _chunks[i];
         float      dis   = Vector3.Distance(chunk.transform.position, player.position);
         if (dis > (_preloadRange * 2 + Chunk.width))
         {
             _chunks.Remove(chunk);
             Destroy(chunk.gameObject);
         }
     }
 }
Example #5
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 = ChunkMgr6Load.GetChunkByChunkPos(blockX + 1, blockY, blockZ);
            }
            StartCoroutine(_rightChunk.RebuildMesh());
            //Debug.Log("rihgt : " + _rightChunk.name);
        }
        // 左边
        if (blockX == 0)
        {
            if (_leftChunk == null)
            {
                _leftChunk = ChunkMgr6Load.GetChunkByChunkPos(chunkX - 1, chunkY, chunkZ);
            }
            StartCoroutine(_leftChunk.RebuildMesh());
            //Debug.Log("left : " + _leftChunk.name);
        }
        // 前面
        if (blockZ == 0)
        {
            if (_frontChunk == null)
            {
                _frontChunk = ChunkMgr6Load.GetChunkByChunkPos(blockX, blockY, blockZ - 1);
            }
            StartCoroutine(_frontChunk.RebuildMesh());
            //Debug.Log("front : " + _frontChunk.name);
        }
        // 后面
        if (blockZ == width - 1)
        {
            if (_backChunk == null)
            {
                _backChunk = ChunkMgr6Load.GetChunkByChunkPos(blockX, blockY, blockZ + 1);
            }
            StartCoroutine(_backChunk.RebuildMesh());
            //Debug.Log("back : " + _backChunk.name);
        }
        // 上面
        if (blockY == height - 1)
        {
            if (_topChunk == null)
            {
                _topChunk = ChunkMgr6Load.GetChunkByChunkPos(blockX, blockY + 1, blockZ);
            }
            StartCoroutine(_topChunk.RebuildMesh());
            //Debug.Log("top : " + _topChunk.name);
        }
        // 下面
        if (blockY == 0)
        {
            if (_bottomChunk == null)
            {
                _bottomChunk = ChunkMgr6Load.GetChunkByWorldPos(blockX, blockY - 1, blockZ);
            }
            StartCoroutine(_bottomChunk.RebuildMesh());
            //Debug.Log("bottom : " + _bottomChunk.name);
        }
        #endregion
    }
Example #6
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 = ChunkMgr6Load.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 = ChunkMgr6Load.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 = ChunkMgr6Load.GetChunkByWorldPos(worldPos);
            }
            if (_frontChunk != null && _frontChunk != this && _frontChunk.ready)
            {
                return(_frontChunk.GetBlock(worldPos) == null);
            }

            return(true);
        }

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

            return(true);
        }

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

            return(true);
        }

        // 下面
        if (y < 0)
        {
            if (_bottomChunk == null)
            {
                _bottomChunk = ChunkMgr6Load.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);
    }