Exemple #1
0
    static void Single_OnChunksEnterLeaveViewReq(object obj, Action <object> callback)
    {
        CSChunksEnterLeaveViewReq req = obj as CSChunksEnterLeaveViewReq;
        CSChunksEnterLeaveViewRes res = new CSChunksEnterLeaveViewRes();

        res.RetCode = 0;

        foreach (CSVector2Int chunk in req.EnterViewChunks)
        {
            Vector2Int chunkPos = chunk.ToVector2Int();
            byte[]     blocks   = GetChunkData(chunkPos);
            if (!chunkGenerateFlagSet.Contains(chunkPos))
            {
                TerrainGenerator.GenerateChunkData(chunk, blocks);
                chunkGenerateFlagSet.Add(chunkPos);
            }
            CSChunk c = new CSChunk();
            c.Position      = chunk;
            c.BlocksInBytes = (byte[])blocks.Clone();
            res.EnterViewChunks.Add(c);
        }
        foreach (CSVector2Int chunk in req.LeaveViewChunks)
        {
            res.LeaveViewChunks.Add(chunk);
        }
        callback(res);
    }
Exemple #2
0
    public static void LoadChunk(CSChunk csChunk)
    {
        //Debug.Log("loadChunk,x=" + csChunk.Position.x + ",z=" + csChunk.Position.y);
        Chunk chunk = ChunkPool.GetChunk();

        chunk.SetData(csChunk.Position.x, csChunk.Position.y, csChunk.BlocksInBytes);
        AddToChunkDict(chunk);
        ChunkRefresher.Add(chunk);
    }
        static bool ProcessChunksEnterView(Player player, List <CSVector2Int> cschunks, out List <CSChunk> enterViewChunks)
        {
            bool retBool = true;

            enterViewChunks = new List <CSChunk>();

            //转换
            List <Vector2Int> chunks = Vector2Int.CSVector2IntList_To_Vector2IntList(cschunks);

            //校验
            foreach (Vector2Int chunk in chunks)
            {
                foreach (Player p in TerrainData.GetChunkViewPlayers(chunk))
                {
                    if (p.id == player.id)
                    {
                        retBool = false;
                        break;
                    }
                }
                foreach (Vector2Int c in player.chunks)
                {
                    if (c.x == chunk.x && c.y == chunk.y)
                    {
                        retBool = false;
                        break;
                    }
                }
            }

            //若校验成功则操作
            if (retBool)
            {
                foreach (Vector2Int chunk in chunks)
                {
                    player.chunks.Add(chunk);
                    List <Player> playersInChunk = TerrainData.GetChunkPlayers(chunk);
                    TerrainData.GetChunkViewPlayers(chunk).Add(player);

                    List <byte>    blocksInBytes = new List <byte>();
                    List <CSBlock> blocks        = TerrainData.GetChunkBlocks(chunk);
                    Dictionary <Vector3Int, CSBlockType> pos2type = new Dictionary <Vector3Int, CSBlockType>();
                    foreach (CSBlock block in blocks)
                    {
                        Vector3Int blockPos = Vector3Int.ParseFromCSVector3Int(block.position);
                        pos2type[blockPos] = block.type;
                    }
                    Vector3Int tempPos = new Vector3Int();
                    for (int k = 0; k < 256; k++)
                    {
                        for (int i = 0; i < 16; i++)
                        {
                            for (int j = 0; j < 16; j++)
                            {
                                tempPos.x = chunk.x * 16 + i;
                                tempPos.y = k;
                                tempPos.z = chunk.y * 16 + j;
                                CSBlockType type = pos2type.ContainsKey(tempPos) ? pos2type[tempPos] : CSBlockType.None;
                                blocksInBytes.Add((byte)type);
                            }
                        }
                    }
                    CSChunk c = new CSChunk();
                    c.Position      = chunk.ToCSVector2Int();
                    c.BlocksInBytes = blocksInBytes.ToArray();
                    //Ultilities.Print($"id={player.id},chunk=({c.Position.x},{c.Position.y}),length={playersInChunk.Count}");
                    foreach (Player p in playersInChunk)
                    {
                        //Ultilities.Print($"this id={p.id}");
                        if (p.id != player.id)
                        {
                            //Ultilities.Print($"add");
                            c.Players.Add(new CSPlayer
                            {
                                PlayerID = p.id,
                                Name     = p.name,
                                Position = new CSVector3 {
                                    x = p.position.x, y = p.position.y, z = p.position.z
                                },
                                Rotation = new CSVector3 {
                                    x = p.rotation.x, y = p.rotation.y, z = p.rotation.z
                                }
                            });
                        }
                    }
                    enterViewChunks.Add(c);
                }
            }

            return(retBool);
        }