From 9dc12cde78570ddb38550a5ec100c4b890d3448d Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 25 Apr 2016 09:16:03 +0300 Subject: [PATCH] dminer example: implement grass - #247 --- examples/dminer/src/dminer/core/blocks.d | 84 ++++++++++++++++++------ examples/dminer/src/dminer/core/world.d | 7 +- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/examples/dminer/src/dminer/core/blocks.d b/examples/dminer/src/dminer/core/blocks.d index 3ce10bf4..0676383e 100644 --- a/examples/dminer/src/dminer/core/blocks.d +++ b/examples/dminer/src/dminer/core/blocks.d @@ -59,25 +59,22 @@ public: return false; } - /// create cube face - void createFace(World world, ref Position camPosition, Vector3d pos, Dir face, Mesh mesh) { - // default implementation + /// add cube face + protected void addFace(Vector3d pos, Dir face, Mesh mesh, int textureIndex) { ushort startVertexIndex = cast(ushort)mesh.vertexCount; float[VERTEX_COMPONENTS * 4] vptr; ushort[6] iptr; - createFaceMesh(vptr.ptr, face, pos.x, pos.y, pos.z, txIndex); + createFaceMesh(vptr.ptr, face, pos.x, pos.y, pos.z, textureIndex); for (int i = 0; i < 6; i++) iptr[i] = cast(ushort)(startVertexIndex + face_indexes[i]); - //if (HIGHLIGHT_GRID && ((pos.x & 7) == 0 || (pos.z & 7) == 0)) { - // for (int i = 0; i < 4; i++) { - // vptr[11 * i + 6 + 0] = 1.4f; - // vptr[11 * i + 6 + 1] = 1.4f; - // vptr[11 * i + 6 + 2] = 1.4f; - // } - //} mesh.addVertexes(vptr); mesh.addPart(PrimitiveType.triangles, iptr); } + + /// create cube face + void createFace(World world, ref Position camPosition, Vector3d pos, Dir face, Mesh mesh) { + addFace(pos, face, mesh, txIndex); + } /// create faces void createFaces(World world, ref Position camPosition, Vector3d pos, int visibleFaces, Mesh mesh) { for (int i = 0; i < 6; i++) @@ -250,6 +247,20 @@ enum BlockImage : int { red_sand, } +enum BlockId : cell_t { + air, // 0 + gray_brick, + brick, + bedrock, + clay, + cobblestone, + gravel, + red_sand, + sand, + dirt, + grass +} + /// init block types array __gshared static this() { import std.string; @@ -264,18 +275,20 @@ __gshared static this() { BLOCK_TYPE_VISIBLE[BOUND_BOTTOM] = true; // empty cell - registerBlockType(new BlockDef(0, "empty", BlockVisibility.INVISIBLE, 0)); + registerBlockType(new BlockDef(BlockId.air, "air", BlockVisibility.INVISIBLE, 0)); // standard block types - registerBlockType(new BlockDef(1, "gray_brick", BlockVisibility.OPAQUE, BlockImage.stonebrick)); - registerBlockType(new BlockDef(2, "brick", BlockVisibility.OPAQUE, BlockImage.brick)); - registerBlockType(new BlockDef(3, "bedrock", BlockVisibility.OPAQUE, BlockImage.bedrock)); - registerBlockType(new BlockDef(4, "clay", BlockVisibility.OPAQUE, BlockImage.clay)); - registerBlockType(new BlockDef(5, "cobblestone", BlockVisibility.OPAQUE, BlockImage.cobblestone)); - registerBlockType(new BlockDef(6, "gravel", BlockVisibility.OPAQUE, BlockImage.gravel)); - registerBlockType(new BlockDef(7, "red_sand", BlockVisibility.OPAQUE, BlockImage.red_sand)); - registerBlockType(new BlockDef(8, "sand", BlockVisibility.OPAQUE, BlockImage.sand)); + registerBlockType(new BlockDef(BlockId.gray_brick, "gray_brick", BlockVisibility.OPAQUE, BlockImage.stonebrick)); + registerBlockType(new BlockDef(BlockId.brick, "brick", BlockVisibility.OPAQUE, BlockImage.brick)); + registerBlockType(new BlockDef(BlockId.bedrock, "bedrock", BlockVisibility.OPAQUE, BlockImage.bedrock)); + registerBlockType(new BlockDef(BlockId.clay, "clay", BlockVisibility.OPAQUE, BlockImage.clay)); + registerBlockType(new BlockDef(BlockId.cobblestone, "cobblestone", BlockVisibility.OPAQUE, BlockImage.cobblestone)); + registerBlockType(new BlockDef(BlockId.gravel, "gravel", BlockVisibility.OPAQUE, BlockImage.gravel)); + registerBlockType(new BlockDef(BlockId.red_sand, "red_sand", BlockVisibility.OPAQUE, BlockImage.red_sand)); + registerBlockType(new BlockDef(BlockId.sand, "sand", BlockVisibility.OPAQUE, BlockImage.sand)); + registerBlockType(new BlockDef(BlockId.dirt, "dirt", BlockVisibility.OPAQUE, BlockImage.dirt)); + registerBlockType(new CustomTopBlock(BlockId.grass, "grass", BlockVisibility.OPAQUE, BlockImage.dirt, BlockImage.grass_top, BlockImage.grass_side)); - registerBlockType(new BlockDef(50, "box", BlockVisibility.HALF_OPAQUE, 50)); + //registerBlockType(new BlockDef(50, "box", BlockVisibility.HALF_OPAQUE, 50)); //registerBlockType(new TerrainBlock(100, "terrain_bedrock", 2)); //registerBlockType(new TerrainBlock(101, "terrain_clay", 3)); @@ -285,3 +298,32 @@ __gshared static this() { //registerBlockType(new TerrainBlock(105, "terrain_sand", 7)); } + +class CustomTopBlock : BlockDef { +public: + int topTxIndex; + int sideTxIndex; + this(cell_t blockId, string blockName, BlockVisibility v, int tx, int topTx, int sideTx) { + super(blockId, blockName, BlockVisibility.OPAQUE, tx); + topTxIndex = topTx; + sideTxIndex = sideTx; + } + ~this() { + } + + /// create cube face + override void createFace(World world, ref Position camPosition, Vector3d pos, Dir face, Mesh mesh) { + // checking cell above + cell_t blockAbove = world.getCell(pos.x, pos.y + 1, pos.z); + int tx = txIndex; + if (BLOCK_TYPE_CAN_PASS[blockAbove]) { + if (face == Dir.UP) { + tx = topTxIndex; + } else if (face != Dir.DOWN) { + tx = sideTxIndex; + } + } + addFace(pos, face, mesh, tx); + } +} + diff --git a/examples/dminer/src/dminer/core/world.d b/examples/dminer/src/dminer/core/world.d index 1bd4312e..996108f8 100644 --- a/examples/dminer/src/dminer/core/world.d +++ b/examples/dminer/src/dminer/core/world.d @@ -479,8 +479,11 @@ void initWorldTerrain(World world, int terrSizeBits = 10, int x0 = 0, int z0 = 0 terr.filter(1); for (int x = 0; x < terrSize; x++) { for (int z = 0; z < terrSize; z++) { + int cellx = x0 + x - terrSize / 2; + int cellz = z0 + z - terrSize / 2; int h = terr.get(x, z); - cell_t cell = 1; + //cell_t cell = BlockId.bedrock; + cell_t cell = BlockId.grass; //if (h < CHUNK_DY / 10) // cell = 100; //else if (h < CHUNK_DY / 5) @@ -494,7 +497,7 @@ void initWorldTerrain(World world, int terrSizeBits = 10, int x0 = 0, int z0 = 0 //else // cell = 105; for (int y = 0; y < h; y++) { - world.setCell(x0 + x - terrSize / 2, y, z0 + z - terrSize / 2, cell); + world.setCell(cellx, y, cellz, cell); } } }