

= Register Block Types

To add blocks to the framework, you just have to add them to the BlockManager - This happens by specifying a block object (has to extend *cubes.Block*, by overriding methods you can specify own behaviours). Each block needs at least one “skin, which you have to pass in the constructor of the *cubes.Block* class). A “skin contains all information to display the block - Texture-Index in the Atlas, Transparency and so on.


After initialising your object, you have to register it in the BlockManager:


[source,java]

----
BlockManager.register(myBlockObject);
----

_should_*CubesTestAssets.BLOCK_GRASS*


`CubesTestAssets.registerBlocks();`



== Single Texture Block


image::http///destroflyer.mania-community.de/other/imagehost/cubes/block_stone.png[block_stone.png,with="150",height="",align="right"]

Let's say, you want to add a simple block, which uses the same texture on every face. The framework recognizes, when only one texture is specified and therefore uses this for each face:


[source,java]

----
//The stone texture is in the 10th column and 1st row in the texture atlas
Block blockStone = new Block(new BlockSkin(new BlockSkin_TextureLocation(9, 0), false));
BlockManager.register(blockStone);
----


== Face-Dependant Textures Block


image::http///destroflyer.mania-community.de/other/imagehost/cubes/block_wood.png[block_wood.png,with="150",height="",align="right"]

Now it's time to get prettier blocks - The wood block as known in Minecraft has two textures: Top/Bottom (cross-section) and Left/Right/Front/Back (bark). A simple way to set the texture for each face is to just give 6 textures to the skin (in the right order :P):


[source,java]

----
Block blockWood = new Block(new BlockSkin[]{
    new BlockSkin(new BlockSkin_TextureLocation(4, 0), false),
    new BlockSkin(new BlockSkin_TextureLocation(4, 0), false),
    new BlockSkin(new BlockSkin_TextureLocation(3, 0), false),
    new BlockSkin(new BlockSkin_TextureLocation(3, 0), false),
    new BlockSkin(new BlockSkin_TextureLocation(3, 0), false),
    new BlockSkin(new BlockSkin_TextureLocation(3, 0), false)
});
BlockManager.register(blockWood);
----


== Dynamic Textures Block

Last but not least: What if a block wants to change its texture according to its environment?



image::http///destroflyer.mania-community.de/other/imagehost/cubes/block_grass.png[block_grass.png,with="150",height="",align="right"]

A nice example would be a grass block - If it's on the surface, it contains a nice grass texture at the top face and a little earth-grass-transition at the sides. Otherwise, all 6 sides should display an earth texture.
Special behaviours like this can be achieved by overwriting the *getSkinIndex* method:


[source,java]

----
Block blockGrass = new Block(new BlockSkin[]{
        //We specify the 3 skins we need:
        //Grass, Earth-Grass-Transition and Earth
        new BlockSkin(new BlockSkin_TextureLocation(0, 0), false),
        new BlockSkin(new BlockSkin_TextureLocation(1, 0), false),
        new BlockSkin(new BlockSkin_TextureLocation(2, 0), false)
    }){

    @Override
    //The number that's being returned specified the index
    //of the skin in the previous declared BlockSkin array
    protected int getSkinIndex(BlockChunkControl chunk, Vector3Int location, Block.Face face){
        if(chunk.isBlockOnSurface(location)){
            switch(face){
                case Top:
                    return 0;

                case Bottom:
                    return 2;
            }
            return 1;
        }
        return 2;
    }
};
BlockManager.register(blockGrass);
----
