示例#1
0
        private void ShowMineSplitObj(WorldPos pos, int picIndex)
        {
            if (BlockMaskController.Instance.IsBlockMasking())
            {
                BlockMaskController.Instance.StopDo();
            }
            mineSplitObj.transform.position = new Vector3(pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f);
            if (!mineSplitObj.activeSelf)
            {
                mineSplitObj.SetActive(true);
            }
            int   sunLight       = World.world.GetSunLight(pos.x, pos.y, pos.z);
            int   blockLight     = World.world.GetBlockLight(pos.x, pos.y, pos.z);
            float lightIntensity = MTBSkyBox.Instance.GetLightIntensity(sunLight, blockLight);

            mineSplitRender.material.SetFloat("_lightIntensity", lightIntensity);
            mineSplitRender.material.SetFloat("_TexIndex", (float)picIndex);
        }
        public void UpdateRegionFileLinkByChunkPos(WorldPos chunkPos)
        {
            WorldPos        curRegionPos = GetRegionPos(chunkPos);
            List <WorldPos> removeList   = new List <WorldPos>();

            foreach (var regionPos in _map.Keys)
            {
                if (Math.Abs(regionPos.x - curRegionPos.x) > 1 || Math.Abs(regionPos.z - curRegionPos.z) > 1)
                {
                    removeList.Add(regionPos);
                }
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                _map[removeList[i]].Close();
                _map.Remove(removeList[i]);
            }
        }
示例#3
0
        private bool CreateTree(int x, int y, int z, Chunk chunk, int treeTrunkWidth, int treeTrunkHeight, int treeLeaveWidth, int treeLeaveHeight, int treeLeaveOffset, IMTBRandom random)
        {
            int halfTreeLeaveWidth = treeLeaveWidth / 2;
            int treeLeaveTopY      = treeLeaveHeight + treeLeaveOffset + treeTrunkHeight - 1 + y;
            int treeLeaveStartY    = treeLeaveTopY - halfTreeLeaveWidth;

            _spreadQueue.Clear();
            Block curBlock = chunk.GetBlock(x, treeLeaveStartY, z);

            if (curBlock.BlockType == BlockType.Air)
            {
                setBlock(chunk, x, treeLeaveStartY, z, new Block(BlockType.Block_41, 2));
            }
            WorldPos startPos = new WorldPos(x, treeLeaveStartY, z);

            _spreadQueue.Enqueue(startPos);
            while (_spreadQueue.Count > 0)
            {
                WorldPos pos = _spreadQueue.Dequeue();
                TreeLeaveSpread(chunk, pos.x + 1, pos.y, pos.z, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
                TreeLeaveSpread(chunk, pos.x - 1, pos.y, pos.z, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
                TreeLeaveSpread(chunk, pos.x, pos.y + 1, pos.z, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
                TreeLeaveSpread(chunk, pos.x, pos.y - 1, pos.z, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
                TreeLeaveSpread(chunk, pos.x, pos.y, pos.z + 1, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
                TreeLeaveSpread(chunk, pos.x, pos.y, pos.z - 1, _spreadQueue, halfTreeLeaveWidth, treeLeaveTopY, treeLeaveHeight, startPos);
            }


            bool hasFaceBlock = random.Range(0, 100) < 50;
            int  faceBlock    = random.Range(0, treeTrunkHeight);

            for (int i = 0; i < treeTrunkHeight; i++)
            {
                if (hasFaceBlock && faceBlock == i)
                {
                    setBlock(chunk, x, y + i, z, new Block(BlockType.Block_75, (byte)random.Range(0, 4)), true);
                }
                else
                {
                    setBlock(chunk, x, y + i, z, new Block(BlockType.Block_19, 2), true);
                }
            }
            return(true);
        }
示例#4
0
        protected override void Process()
        {
            try{
                PrevRenderChunk prevChunk = DequeuePrevRender();
                if (prevChunk != null)
                {
                    WorldPos pos = prevChunk.chunk.worldPos;
                    if (World.world.GetChunk(pos.x, pos.y, pos.z) == null)
                    {
                        return;
                    }
                    List <Chunk> renderChunks = _prevRenderHandler.HandlePrevRenderChunk(prevChunk);
                    for (int i = 0; i < renderChunks.Count; i++)
                    {
                        EnqueueRender(renderChunks[i], true);
                    }
                }

                Chunk chunk = DequeueHighRender();
                if (chunk == null)
                {
                    chunk = DequeueRender();
                }
                if (chunk != null)
                {
                    WorldPos pos = chunk.worldPos;
                    if (World.world.GetChunk(pos.x, pos.y, pos.z) == null)
                    {
                        return;
                    }
                    MeshData meshData = _manager.GetUnusedMeshData();
                    for (int i = 0; i < chunk.Sections.Length; i++)
                    {
                        Section s = chunk.Sections[i];
                        _meshCalculator.CalculateSectionMesh(s, meshData);
                    }
                    _manager.EnqueueWaitRender(chunk, meshData);
                }
            }catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }
        }
        public void Do(float screenX, float screenY, Vector3 position, float distance)
        {
            RaycastHit hit;
            int        mask = LayerMask.GetMask(new string[] { "TerrainMesh", "SupportColliderMesh" });

            if (Terrain.RayToWorld(screenX, screenY, position, distance, out hit, mask))
            {
                WorldPos pos = Terrain.GetWorldPos(hit, false);
                if (hit.collider.GetComponentInParent <ChunkObj>() == null)
                {
                    return;
                }
                ShowMaskObj(pos);
            }
            else
            {
                HideMaskObj();
            }
        }
        public void CheckEntityRefresh(Chunk chunk)
        {
            WorldPos pos = chunk.worldPos;

            if (IsInPlayerView(pos))
            {
                if (!chunk.hasRefreshEntities && !entityRefreshQueue.Contains(pos))
                {
                    entityRefreshQueue.Enqueue(pos);
                }
            }
            else
            {
                if (chunk.hasRefreshEntities && !entityRemoveQueue.Contains(pos))
                {
                    entityRemoveQueue.Enqueue(pos);
                }
            }
        }
 private void AddEntity(ClientEntity entity)
 {
     lock (_entityMap)
     {
         _entityMap.Add(entity.aoId, entity);
     }
     lock (_entityChunkMap)
     {
         WorldPos            chunkPos = entity.inChunkPos;
         List <ClientEntity> listMonster;
         _entityChunkMap.TryGetValue(chunkPos, out listMonster);
         if (listMonster == null)
         {
             listMonster = new List <ClientEntity>();
             _entityChunkMap.Add(chunkPos, listMonster);
         }
         listMonster.Add(entity);
     }
 }
        //单机的持续化工作
        private void SinglePersistanceWorker()
        {
            //单机
            //从本地数据加载
            PriorityChunk loadPriorityChunk = DeLoadQueue();

            if (loadPriorityChunk != null)
            {
                WorldPos pos = loadPriorityChunk.chunk.worldPos;
                if (World.world.GetChunk(pos.x, pos.y, pos.z) == null)
                {
                    return;
                }
                loadPriorityChunk.chunk.InitData();
                if (WorldConfig.Instance.loadInMap)
                {
                    WorldPersistanceManager.Instance.LoadChunk(loadPriorityChunk.chunk);
                }
                //这里进入产生线程
                _manager.generatorWorker.EnGeneratorQueue(loadPriorityChunk);
            }
            //保存到本地数据
            Chunk saveChunk = DeSaveQueue();

            if (saveChunk != null)
            {
                if (saveChunk.isUpdate)
                {
                    if (WorldConfig.Instance.saveBlock)
                    {
                        WorldPersistanceManager.Instance.SaveChunk(saveChunk);
                        saveChunk.isUpdate = false;
                    }
                }
                _manager.EnqueueWaitRemove(saveChunk);
            }
            RefreshChunkArea refreshChunkArea = DeRefreshQueue();

            if (refreshChunkArea != null)
            {
                _manager.generatorWorker.EnRefreshQueue(refreshChunkArea);
            }
        }
示例#9
0
        private void CreateExplodeParticle(WorldPos pos, Block block, Vector3 normal)
        {
            ParticleSystem explode = particleQueue.Dequeue();

            Renderer  render = explode.GetComponent <Renderer>();
            Direction dir    = Direction.left;
            //更新光照
            int      direction = CheckNormal(normal);
            WorldPos lightPos  = pos;

            if (direction != 0)
            {
                dir      = (Direction)direction;
                lightPos = new WorldPos(pos.x + Mathf.RoundToInt(normal.x), pos.y + Mathf.RoundToInt(normal.y),
                                        pos.z + Mathf.RoundToInt(normal.z));
            }

            int   sunLightLevel   = World.world.GetSunLight(lightPos.x, lightPos.y, lightPos.z);
            int   blockLightLevel = World.world.GetBlockLight(lightPos.x, lightPos.y, lightPos.z);
            float lightIntensity  = MTBSkyBox.Instance.GetLightIntensity(sunLightLevel, blockLightLevel);

            render.material.SetFloat("_lightIntensity", lightIntensity);
            //更新贴图
            ResetExplode(explode);
            BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(block.BlockType);
            byte             resExtendId        = calculator.GetResourceExtendId(block.ExtendId);
            WorldTextureType type = WorldTextureProvider.Instance.GetTextureType(block.BlockType, resExtendId);

            render.material.mainTexture = WorldTextureProvider.Instance.GetAtlasTexture(type);
            Rect rect = WorldTextureProvider.Instance.GetBlockTextureRect(block.BlockType, resExtendId, dir);

            if (rect != null)
            {
                render.material.SetFloat("_OffsetX", rect.x);
                render.material.SetFloat("_OffsetY", rect.y);
                render.material.SetFloat("_Width", rect.width);
                render.material.SetFloat("_Random", UnityEngine.Random.Range(0, 1f));
                explode.transform.position = new Vector3(pos.x + 0.5f, pos.y + 0.1f, pos.z + 0.5f);
            }
            explode.Play();
            particleQueue.Enqueue(explode);
        }
示例#10
0
        public NetWorldLoader(World world)
        {
            this.world         = world;
            _stop              = true;
            loadQueue          = new HeapPriorityQueue <PriorityWorldPos>(1000);
            entityRefreshQueue = new Queue <WorldPos>(200);
            _removedChunks     = new Dictionary <WorldPos, Chunk>(new WorldPosComparer());
            _savedChunks       = new Dictionary <WorldPos, Chunk>(new WorldPosComparer());
            _curChunkPos       = new WorldPos(int.MaxValue, 0, 0);
            EventManager.RegisterEvent(NetEventMacro.ON_NET_CHUNK_GENERATOR_RETURN, OnNetChunkGeneratorReturn);
            EventManager.RegisterEvent(NetEventMacro.ON_NET_CHUNK_GENERATOR, OnNetChunkGenerator);
            EventManager.RegisterEvent(NetEventMacro.ON_NET_SAVE_CHUNK, OnNetSaveChunk);
            EventManager.RegisterEvent(NetEventMacro.ON_NET_POPULATION_GENERATE, OnNetPopulationGenerate);
            EventManager.RegisterEvent(NetEventMacro.ON_NET_AREA_UPDATE, OnNetAreaUpdate);
            EventManager.RegisterEvent(NetEventMacro.ON_RESPONSE_REMOVE_CHUNK_EXT_DATA, OnResponseRemoveChunkExtData);

            EventManager.RegisterEvent(EventMacro.CHUNK_GENERATE_FINISH, OnChunkGenerateFinish);
            world.StartCoroutine(ResponseNetChunk());
            world.StartCoroutine(RefreshEntity());
        }
        public void LoadFirst(Vector3 pos, int size)
        {
            EventManager.SendEvent(EventMacro.START_GENERATE_FIRST_WORLD, null);
            EventManager.RegisterEvent(EventMacro.CHUNK_GENERATE_FINISH, OnFirstWorldChunkGenerate);
            WorldPos worldPos    = Terrain.GetWorldPos(pos);
            WorldPos curChunkPos = Terrain.GetChunkPos(worldPos.x, 0, worldPos.z);

            for (int x = -size; x <= size; x++)
            {
                for (int z = -size; z <= size; z++)
                {
                    WorldPos chunkPos = new WorldPos(curChunkPos.x + x * Chunk.chunkWidth, curChunkPos.y, curChunkPos.z + z * Chunk.chunkDepth);
                    if ((x >= -size + 2 && x <= size - 2) && (z >= -size + 2 && z <= size - 2))
                    {
                        firstChunks.Add(chunkPos);
                    }
                    world.WorldGenerator.GenerateChunk(chunkPos.x, chunkPos.y, chunkPos.z, curChunkPos);
                }
            }
        }
 private IEnumerator RefreshEntity()
 {
     while (true)
     {
         if (world.FirstGeneted)
         {
             if (entityRefreshQueue.Count > 0)
             {
                 WorldPos refreshPos = entityRefreshQueue.Dequeue();
                 //真正要刷的时候再次判断是否在玩家视野
                 if (IsInPlayerView(refreshPos))
                 {
                     Chunk chunk = world.GetChunk(refreshPos.x, refreshPos.y, refreshPos.z);
                     if (chunk != null && chunk.isGenerated)
                     {
                         if (chunk.RefreshEntity() > 0)
                         {
                             yield return(new WaitForSeconds(0.1f));
                         }
                     }
                 }
             }
             if (entityRemoveQueue.Count > 0)
             {
                 WorldPos removePos = entityRemoveQueue.Dequeue();
                 if (!IsInPlayerView(removePos))
                 {
                     Chunk chunk = world.GetChunk(removePos.x, removePos.y, removePos.z);
                     if (chunk != null)
                     {
                         if (chunk.RemoveEntity() > 0)
                         {
                             yield return(new WaitForSeconds(0.1f));
                         }
                     }
                 }
             }
         }
         yield return(null);
     }
 }
 public void RemoveEntity(ClientEntity entity)
 {
     lock (_entityMap)
     {
         _entityMap.Remove(entity.aoId);
     }
     lock (_entityChunkMap)
     {
         WorldPos            chunkPos = entity.inChunkPos;
         List <ClientEntity> listEntity;
         _entityChunkMap.TryGetValue(chunkPos, out listEntity);
         if (listEntity != null)
         {
             listEntity.Remove(entity);
             if (listEntity.Count <= 0)
             {
                 _entityChunkMap.Remove(chunkPos);
             }
         }
     }
 }
        //(联机使用)
        public void GenerateFromServer(int x, int y, int z)
        {
            WorldPos pos = new WorldPos(x, y, z);

            if (_world.chunks.ContainsKey(pos))
            {
                return;
            }
            Chunk newChunk = _world.CreateNewChunk(pos);

            if (newChunk == null)
            {
                return;
            }
            _world.chunks.Add(newChunk.worldPos, newChunk);
            RequestChunkPackage package = PackageFactory.GetPackage(PackageType.ResquestChunk) as RequestChunkPackage;

            package.roleId = NetManager.Instance.client.roleId;
            package.pos    = pos;
            NetManager.Instance.client.SendPackage(package);
        }
 public void RemovePlayer(ClientPlayer player)
 {
     lock (_playerMap)
     {
         _playerMap.Remove(player.id);
     }
     lock (_playerChunkMap)
     {
         WorldPos            chunkPos = player.inChunkPos;
         List <ClientPlayer> listPlayer;
         _playerChunkMap.TryGetValue(chunkPos, out listPlayer);
         if (listPlayer != null)
         {
             listPlayer.Remove(player);
             if (listPlayer.Count <= 0)
             {
                 _playerChunkMap.Remove(chunkPos);
             }
         }
     }
 }
 public void Update()
 {
     if (player == null)
     {
         player = HasActionObjectManager.Instance.playerManager.getMyPlayer();
     }
     else
     {
         WorldPos playerPos   = Terrain.GetWorldPos(player.transform.position);
         WorldPos curChunkPos = Terrain.GetChunkPos(playerPos.x, 0, playerPos.z);
         if (!curChunkPos.EqualOther(_curChunkPos))
         {
             _curChunkPos = curChunkPos;
             world.WorldGenerator.DataProcessorManager.UpdateChunkPriority(_curChunkPos);
             ChangeAllEntityVisible();
             LoadChunks();
             RemoveChunks();
             UpdatePersistanceOperate();
         }
     }
 }
示例#17
0
 public void BroadcastPlayerHasChunkPackage(ClientPlayer sourcePlayer, WorldPos chunkPos, NetPackage package, bool includeSelf)
 {
     lock (_chunkMap)
     {
         ClientChangedChunk changedChunk;
         _chunkMap.TryGetValue(chunkPos, out changedChunk);
         if (changedChunk != null)
         {
             List <int> playerIds = changedChunk.players;
             for (int i = 0; i < playerIds.Count; i++)
             {
                 if (!includeSelf && playerIds[i] == sourcePlayer.id)
                 {
                     continue;
                 }
                 ClientPlayer player = NetManager.Instance.server.playerManager.GetPlayer(playerIds[i]);
                 player.worker.SendPackage(package);
             }
         }
     }
 }
示例#18
0
 void Update()
 {
     if (!_isFly)
     {
         _curTime += Time.deltaTime;
         if (_curTime > disappearTime)
         {
             On_SelfDisappear(this);
         }
         WorldPos pos   = Terrain.GetWorldPos(this.transform.position);
         Block    block = World.world.GetBlock(pos.x, pos.y, pos.z);
         BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(block.BlockType);
         int   sunLight       = World.world.GetSunLight(pos.x, pos.y, pos.z);
         int   blockLight     = World.world.GetBlockLight(pos.x, pos.y, pos.z);
         float lightIntensity = MTBSkyBox.Instance.GetLightIntensity(sunLight, blockLight);
         render.material.SetFloat("_lightIntensity", lightIntensity);
         if (calculator.GetMeshColliderType(block.ExtendId) == MeshColliderType.terrainCollider)
         {
             On_SelfDisappear(this);
         }
     }
     else
     {
         if (_player != null)
         {
             Vector3 dis = _player.transform.position + new Vector3(0, 1.5f, 0) - this.transform.position;
             if (dis.magnitude < 0.3f)
             {
                 On_SelfDisappear(this);
                 return;
             }
             Vector3 v = dis.normalized * flySpeed;
             this.transform.position += v * Time.deltaTime;
         }
         else
         {
             On_SelfDisappear(this);
         }
     }
 }
        public List <int> GetAroundEntity(WorldPos curChunkPos, int width)
        {
            List <int> list = new List <int>();
            WorldPos   pos  = curChunkPos;

            for (int x = -width; x <= width; x++)
            {
                for (int z = -width; z <= width; z++)
                {
                    WorldPos            chunkPos    = new WorldPos(pos.x + x * Chunk.chunkWidth, pos.y, pos.z + z * Chunk.chunkDepth);
                    List <ClientEntity> monsterList = GetEntitiesInChunk(chunkPos);
                    if (monsterList != null)
                    {
                        for (int i = 0; i < monsterList.Count; i++)
                        {
                            list.Add(monsterList[i].aoId);
                        }
                    }
                }
            }
            return(list);
        }
示例#20
0
        public override void SetBlock(World world, int x, int y, int z, Block block, bool notify = true)
        {
            if (!IsHeightLimit(y))
            {
                return;
            }
            BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(block.BlockType);
            Direction direction = calculator.GetFaceDirection(block.ExtendId);
            WorldPos  offset    = new WorldPos();

            if (direction == Direction.front)
            {
                offset = new WorldPos(x - 1, y, z);
            }
            if (direction == Direction.back)
            {
                offset = new WorldPos(x + 1, y, z);
            }
            if (direction == Direction.left)
            {
                offset = new WorldPos(x, y, z - 1);
            }
            if (direction == Direction.right)
            {
                offset = new WorldPos(x, y, z + 1);
            }
            byte extendId = (byte)(block.ExtendId & 7);

            if (world.GetBlock(offset.x, offset.y, offset.z).BlockType != BlockType.Air)
            {
                return;
            }
            Block tempBlock = new Block(block.BlockType, extendId);

            SetBlockAndNotify(world, offset.x, offset.y, offset.z, tempBlock);
            SetBlockAndNotify(world, x, y, z, block);
            world.CheckAndRecalculateMesh(offset.x, offset.y, offset.z, tempBlock);
            world.CheckAndRecalculateMesh(x, y, z, block);
        }
示例#21
0
        public void UpdateState()
        {
            WorldPos pos = Terrain.GetWorldPos(_controller.transform.position);

            if (!_curPos.EqualOther(pos))
            {
                _curPos = pos;
                CheckAndResetPos();
                WorldPos chunkPos = Terrain.GetChunkPos(_curPos.x, 0, _curPos.z);
                if (!chunkPos.EqualOther(_attachChunk.worldPos))
                {
                    Chunk chunk = World.world.GetChunk(chunkPos.x, chunkPos.y, chunkPos.z);
                    AttachChunk(chunk);
                }

                _inBlock         = _attachChunk.GetBlock(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true);
                _standBlock      = _attachChunk.GetBlock(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y - 1, _curPos.z - chunkPos.z, true);
                _blockLightLevel = _attachChunk.GetBlockLight(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true);
                _sunLightLevel   = _attachChunk.GetSunLight(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true);
            }
            UpdateRenderLight(_sunLightLevel, _blockLightLevel);
        }
示例#22
0
        public virtual void removeObj(GameObject obj)
        {
            _listObj.Remove(obj);
            GameObjectController controller = obj.GetComponent <GameObjectController>();

            _mapObj.Remove(controller.baseAttribute.aoId);
            controller.On_ChangeChunk -= HandleOn_ChangeChunk;
            List <GameObject> list;
            WorldPos          pos = controller.gameObjectState.attachChunk.worldPos;

            _mapChunkPosObj.TryGetValue(pos, out list);
            if (list != null)
            {
                list.Remove(obj);
                if (list.Count <= 0)
                {
                    _mapChunkPosObj.Remove(pos);
                }
            }
            obj.SetActive(false);
            SelfDestroy.Destroy(obj);
        }
        public List <int> GetAroundPlayer(WorldPos curChunkPos, int width)
        {
            List <int> list = new List <int>();
            WorldPos   pos  = curChunkPos;

            for (int x = -width; x <= width; x++)
            {
                for (int z = -width; z <= width; z++)
                {
                    WorldPos            chunkPos   = new WorldPos(pos.x + x * Chunk.chunkWidth, pos.y, pos.z + z * Chunk.chunkDepth);
                    List <ClientPlayer> playerList = GetPlayersInChunk(chunkPos);
                    if (playerList != null)
                    {
                        for (int i = 0; i < playerList.Count; i++)
                        {
                            list.Add(playerList[i].id);
                        }
                    }
                }
            }
            return(list);
        }
        public Chunk CreateNewChunk(WorldPos pos, bool needNew = false)
        {
            Chunk chunk;

            if (_cacheChunks.Count > 0)
            {
                chunk = _cacheChunks.Dequeue();
            }
            else
            {
                if (needNew)
                {
                    chunk = new Chunk();
                }
                else
                {
                    return(null);
                }
            }
            chunk.worldPos = pos;
            chunk.world    = this;
            return(chunk);
        }
 public void AddPlayer(ClientPlayer player, bool isMainPlayer = false)
 {
     if (isMainPlayer)
     {
         mainPlayer = player;
     }
     lock (_playerMap)
     {
         _playerMap.Add(player.id, player);
     }
     lock (_playerChunkMap)
     {
         WorldPos            chunkPos = player.inChunkPos;
         List <ClientPlayer> listPlayer;
         _playerChunkMap.TryGetValue(chunkPos, out listPlayer);
         if (listPlayer == null)
         {
             listPlayer = new List <ClientPlayer>();
             _playerChunkMap.Add(chunkPos, listPlayer);
         }
         listPlayer.Add(player);
     }
 }
        private RegionFile GetNetRegionFile(WorldPos worldPos)
        {
            RegionFile regionFile = null;

            _map.TryGetValue(worldPos, out regionFile);
            if (regionFile != null)
            {
                return(regionFile);
            }
            else
            {
                _netMap.TryGetValue(worldPos, out regionFile);
            }
            if (regionFile != null)
            {
                return(regionFile);
            }
            string     regionFileName = GetRegionFileName(worldPos);
            RegionFile file           = new RegionFile(regionFileName, MTBCompressType.ZLib);

            file.Init();
            _netMap.Add(worldPos, file);
            return(file);
        }
 public void UpdateEntityChunkPos(WorldPos oldPos, WorldPos newPos, ClientEntity entity)
 {
     lock (_entityChunkMap)
     {
         List <ClientEntity> listEntity;
         _entityChunkMap.TryGetValue(oldPos, out listEntity);
         if (listEntity != null)
         {
             listEntity.Remove(entity);
             if (listEntity.Count <= 0)
             {
                 _entityChunkMap.Remove(oldPos);
             }
         }
         List <ClientEntity> newPosListEntity;
         _entityChunkMap.TryGetValue(newPos, out newPosListEntity);
         if (newPosListEntity == null)
         {
             newPosListEntity = new List <ClientEntity>();
             _entityChunkMap.Add(newPos, newPosListEntity);
         }
         newPosListEntity.Add(entity);
     }
 }
        public bool Decorade(Chunk chunk, int x, int y, int z, IMTBRandom random)
        {
            int width  = random.Range(1, 3);
            int height = 6;

            for (int cy = y; cy < y + height; cy++)
            {
                for (int cx = -width; cx <= width; cx++)
                {
                    for (int cz = -width; cz <= width; cz++)
                    {
                        if (chunk.GetBlock(cx, cy, cz).BlockType != BlockType.Air)
                        {
                            return(false);
                        }
                    }
                }
            }
            _spreadQueue.Clear();
            setBlock(chunk, x, y, z, new Block(BlockType.Block_11));
            _spreadQueue.Enqueue(new WorldPos(x, y, z));
            int spreadRate       = 10;
            int heightSpreadRate = 40;
            int nextX;
            int nextY;
            int nextZ;

            while (_spreadQueue.Count > 0)
            {
                WorldPos pos = _spreadQueue.Dequeue();
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextX -= 1;
                if (nextX >= x - width && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < spreadRate)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextX += 1;
                if (nextX <= x + width && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < spreadRate)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextY -= 1;
                if (nextY >= y && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < spreadRate)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextY += 1;
                if (nextY < y + height && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < heightSpreadRate)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextZ -= 1;
                if (nextZ >= z - width && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < spreadRate)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
                nextX  = pos.x;
                nextY  = pos.y;
                nextZ  = pos.z;
                nextZ += 1;
                if (nextZ <= z + width && chunk.GetBlock(nextX, nextY, nextZ).BlockType == BlockType.Air)
                {
                    if (random.Range(0, 100) < 60)
                    {
                        setBlock(chunk, nextX, nextY, nextZ, new Block(BlockType.Block_11));
                        _spreadQueue.Enqueue(new WorldPos(nextX, nextY, nextZ));
                    }
                }
            }
            return(true);
        }
        public void UpdatePersistanceOperate()
        {
            WorldPos playerPos = Terrain.GetWorldPos(player.transform.position);

            WorldPersistanceManager.Instance.UpdateRegionFileLinkByChunkPos(_curChunkPos);
        }
        private bool IsInPlayerView(WorldPos pos)
        {
            int width = Mathf.Max(Mathf.Abs(_curChunkPos.x - pos.x) / Chunk.chunkWidth, Mathf.Abs(_curChunkPos.z - pos.z) / Chunk.chunkDepth);

            return(width <= WorldConfig.Instance.playerViewWidth);
        }