本文整理汇总了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]);
}
}
}
}
}
示例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];
}
}
}