Files
voxel/World/Blocks.cs

121 lines
3.2 KiB
C#

using System.Runtime.CompilerServices;
namespace Voxel.Core
{
public enum Blocks : byte
{
Air,
Stone,
Dirt,
OakPlanks,
Grass,
Bedrock,
Sand,
TNT
}
public enum Orientation : byte
{
West = 0, // + X
East = 1, // - X
Top = 2, // + Y
Bottom = 3,// - Y
North = 4, // + Z
South = 5, // - Z
}
public class BlockDefinition
{
public Blocks BlockType;
public Textures[] FaceTextures;
public BlockDefinition(Blocks type, Textures singleTexture)
{
BlockType = type;
FaceTextures = new Textures[6];
for (int i = 0; i < 6; i++) FaceTextures[i] = singleTexture;
}
public BlockDefinition(
Blocks type,
Textures west,
Textures east,
Textures top,
Textures bottom,
Textures north,
Textures south
)
{
BlockType = type;
FaceTextures = new Textures[]
{
west, east, top, bottom, north, south
};
}
}
public static class BlockDefinitions
{
private static readonly BlockDefinition[] _definitions = new BlockDefinition[256];
public static readonly Blocks[] CreativeInventory = new[]
{
Blocks.Stone,
Blocks.Dirt,
Blocks.OakPlanks,
Blocks.Bedrock,
Blocks.Sand,
Blocks.TNT
};
static BlockDefinitions()
{
// simple blocks
Define(Blocks.Stone, Textures.Stone);
Define(Blocks.Dirt, Textures.Dirt);
Define(Blocks.OakPlanks, Textures.OakPlanks);
Define(Blocks.Bedrock, Textures.Bedrock);
Define(Blocks.Sand, Textures.Sand);
// multi-texture blocks
// west (+X)
// east (-X)
// top
// bottom
// north (+Z)
// south (-Z)
Define(Blocks.Grass,
Textures.GrassSide,
Textures.GrassSide,
Textures.GrassTop,
Textures.Dirt,
Textures.GrassSide,
Textures.GrassSide
);
Define(Blocks.TNT,
Textures.TntSide,
Textures.TntSide,
Textures.TntTop,
Textures.TntBottom,
Textures.TntSide,
Textures.TntSide
);
}
/// highly optimized getter for the hot-path (Meshing/Raycasting)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BlockDefinition Get(Blocks type)
{
return _definitions[(byte)type];
}
// internal helper to map the array
private static void Define(Blocks type, Textures tex)
=> _definitions[(byte)type] = new BlockDefinition(type, tex);
private static void Define(Blocks type, Textures w, Textures e, Textures t, Textures b, Textures n, Textures s)
=> _definitions[(byte)type] = new BlockDefinition(type, w, e, t, b, n, s);
}
}