当前位置: 首页>>代码示例>>C#>>正文


C# IDefinitionManager.GetBlockDefinitionByIndex方法代码示例

本文整理汇总了C#中IDefinitionManager.GetBlockDefinitionByIndex方法的典型用法代码示例。如果您正苦于以下问题:C# IDefinitionManager.GetBlockDefinitionByIndex方法的具体用法?C# IDefinitionManager.GetBlockDefinitionByIndex怎么用?C# IDefinitionManager.GetBlockDefinitionByIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDefinitionManager的用法示例。


在下文中一共展示了IDefinitionManager.GetBlockDefinitionByIndex方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Serialize

        /// <summary>
        /// Serialisiert die Chunksäule in den angegebenen Stream.
        /// </summary>
        /// <param name="stream">Zielstream</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        public void Serialize(Stream stream, IDefinitionManager definitionManager)
        {
            using (BinaryWriter bw = new BinaryWriter(stream))
            {
                // Definitionen sammeln
                List<IBlockDefinition> definitions = new List<IBlockDefinition>();
                for (int c = 0; c < Chunks.Length; c++)
                {
                    IChunk chunk = Chunks[c];
                    for (int i = 0; i < chunk.Blocks.Length; i++)
                    {
                        if (chunk.Blocks[i] != 0)
                        {
                            IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]);
                            if (!definitions.Contains(definition))
                                definitions.Add(definition);
                        }
                    }
                }

                bool longIndex = definitions.Count > 254;
                bw.Write((byte)((longIndex) ? 1 : 0));

                // Schreibe Phase 1 (Column Meta: Heightmap, populated, chunkcount)
                bw.Write((byte)Chunks.Length); // Chunk Count
                bw.Write(Populated); // Populated
                for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
                    for (int x = 0; x < Chunk.CHUNKSIZE_X; x++)
                        bw.Write((ushort)Heights[x, y]);
                for (int i = 0; i < Chunks.Length; i++) // Change Counter
                    bw.Write(Chunks[i].ChangeCounter);

                // Schreibe Phase 2 (Block Definitionen)
                if (longIndex)
                    bw.Write((ushort)definitions.Count);
                else
                    bw.Write((byte)definitions.Count);

                foreach (var definition in definitions)
                    bw.Write(definition.GetType().FullName);

                // Schreibe Phase 3 (Chunk Infos)
                for (int c = 0; c < Chunks.Length; c++)
                {
                    IChunk chunk = Chunks[c];
                    for (int i = 0; i < chunk.Blocks.Length; i++)
                    {
                        if (chunk.Blocks[i] == 0)
                        {
                            // Definition Index (Air)
                            if (longIndex)
                                bw.Write((ushort)0);
                            else
                                bw.Write((byte)0);
                        }
                        else
                        {
                            // Definition Index
                            IBlockDefinition definition = definitionManager.GetBlockDefinitionByIndex(chunk.Blocks[i]);

                            if (longIndex)
                                bw.Write((ushort)(definitions.IndexOf(definition) + 1));
                            else
                                bw.Write((byte)(definitions.IndexOf(definition) + 1));

                            // Meta Data
                            if (definition.HasMetaData)
                                bw.Write(chunk.MetaData[i]);
                        }
                    }
                }
            }
        }
开发者ID:reicheltp,项目名称:octoawesome,代码行数:78,代码来源:ChunkColumn.cs

示例2: Deserialize

        /// <summary>
        /// Deserialisiert die Chunksäule aus dem angegebenen Stream.
        /// </summary>
        /// <param name="stream">Quellstream</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        /// <param name="columnIndex">Die Position der Säule</param>
        /// <param name="planetId">Der Index des Planeten</param>
        public void Deserialize(Stream stream, IDefinitionManager definitionManager, int planetId, Index2 columnIndex)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                bool longIndex = br.ReadByte() > 0;

                // Phase 1 (Column Meta: Heightmap, populated, chunkcount)
                Chunks = new Chunk[br.ReadByte()]; // Chunk Count
                Planet = planetId;
                Index = columnIndex;

                Populated = br.ReadBoolean(); // Populated
                for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
                    for (int x = 0; x < Chunk.CHUNKSIZE_X; x++)
                        Heights[x, y] = br.ReadUInt16();

                int[] counter = new int[Chunks.Length];
                for (int i = 0; i < Chunks.Length; i++) // ChangeCounter
                    counter[i] = br.ReadInt32();

                // Phase 2 (Block Definitionen)
                List<IBlockDefinition> types = new List<IBlockDefinition>();
                Dictionary<ushort, ushort> map = new Dictionary<ushort, ushort>();

                int typecount = longIndex ? br.ReadUInt16() : br.ReadByte();
                for (int i = 0; i < typecount; i++)
                {
                    string typeName = br.ReadString();
                    IBlockDefinition[] definitions = definitionManager.GetBlockDefinitions().ToArray();
                    var blockDefinition = definitions.FirstOrDefault(d => d.GetType().FullName == typeName);
                    types.Add(blockDefinition);

                    map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1));
                }

                // Phase 3 (Chunk Infos)
                for (int c = 0; c < Chunks.Length; c++)
                {
                    IChunk chunk = Chunks[c] = new Chunk(new Index3(columnIndex, c), planetId);
                    for (int i = 0; i < chunk.Blocks.Length; i++)
                    {
                        ushort typeIndex = longIndex ? br.ReadUInt16() : br.ReadByte();
                        chunk.MetaData[i] = 0;
                        if (typeIndex > 0)
                        {
                            chunk.Blocks[i] = map[typeIndex];

                            var definition = definitionManager.GetBlockDefinitionByIndex(map[typeIndex]);
                            if (definition.HasMetaData)
                                chunk.MetaData[i] = br.ReadInt32();
                        }
                    }
                    chunk.ChangeCounter = counter[c];
                }
            }
        }
开发者ID:reicheltp,项目名称:octoawesome,代码行数:63,代码来源:ChunkColumn.cs


注:本文中的IDefinitionManager.GetBlockDefinitionByIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。