Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone!

I am using XNA to develope a game of mine. It's similar to Terraria and Minecraft so I'd just call it a 2D voxel engine. And basically, I just created a Chunk System that works fine. The only problem I have so far is drawing the view. When I draw the view, I draw block by block, now obviously that will give you a wierd grid effect. I got a picture below to give you an understanding of what I currently have and what I want to have.

[IMG]http://i47.tinypic.com/1zbug61.png[/IMG]

Basically, I am currently drawing the blocks in a view snapped to the blocks, which I do by using the middle chunk(4), I could also get a block by doing ThePlayer.GetBlock, in that way I can get a block in the loaded chunks next to it. So even though I have a method that could help me out a lot I am still unsure on how to make the view work with multiple blocks, anyways here is the code:

C#
public void DrawWorld()
{
            Chunk Chunk = ThePlayer.LoadedChunks[4];

            foreach (Block Block in Chunk._Blocks)
            {
                // -- Color.
                Texture2D toDraw = null;

                // -- Get the Right Block.
                switch (Block.BlockID)
                {
                    case ID.BLOCK_AIR:
                        toDraw = BlockAir;
                        break;
                    case ID.BLOCK_DIRT:
                        toDraw = BlockDirt;
                        break;
                    case ID.BLOCK_GRASS:
                        toDraw = BlockGrass;
                        break;
                    case ID.BLOCK_SAND:
                        toDraw = BlockSand;
                        break;
                    case ID.BLOCK_STONE:
                        toDraw = BlockStone;
                        break;
                    case ID.BLOCK_COBBLESTONE:
                        toDraw = BlockCobblestone;
                        break;
                    case ID.BLOCK_BRICK:
                        toDraw = BlockBrick;
                        break;
                    case ID.BLOCK_WOODPLANK:
                        toDraw = BlockPlank;
                        break;

                }

                Rectangle _Rec = new Rectangle(Block.X * ID.BlockSize - ThePlayer.BlockX * ID.BlockSize, Block.Y * ID.BlockSize - ThePlayer.BlockY * ID.BlockSize, ID.BlockSize, ID.BlockSize);
                spriteBatch.Draw(toDraw, _Rec, Color.White);
                spriteBatch.DrawString(FontTerrasylum, "Pixel: " + ThePlayer.PixelX + " " + ThePlayer.PixelY, new Vector2(0, 0), Color.White);
                spriteBatch.DrawString(FontTerrasylum, "Block: " + ThePlayer.BlockX + " " + ThePlayer.BlockY, new Vector2(0, 32), Color.White);
                spriteBatch.DrawString(FontTerrasylum, "Chunk: " + ThePlayer.ChunkX + " " + ThePlayer.ChunkY, new Vector2(0, 64), Color.White);
    }
}


Also; There are some important variables:


Chunk
- X
- Y
- Blocks
- X
- Y

Player:

PixelX
PixelY
BlockX
BlockY
ChunkX
ChunkY
LoadedChunks[0-8]
Posted
Updated 8-Jan-13 1:35am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900