net.maxiebyte.voxel.Main1.java Source code

Java tutorial

Introduction

Here is the source code for net.maxiebyte.voxel.Main1.java

Source

package net.maxiebyte.voxel;
/*
 This file is part of Voxel.
    
 Copyright  2015 Maxime Devos
    
 Voxel is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 Voxel is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with Voxel.  If not, see <http://www.gnu.org/licenses/>.
 */

import ch.qos.logback.classic.LoggerContext;

import java.nio.file.Paths;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import net.maxiebyte.voxel.world.DefaultWorld;
import net.maxiebyte.voxel.world.World;
import net.maxiebyte.voxel.world.block.BlockType;
import net.maxiebyte.voxel.inject.Inject;
import net.maxiebyte.voxel.world.gen.SphereGen;
import net.maxiebyte.voxel.world.item.ItemType;

import org.slf4j.LoggerFactory;

/**
 *
 * @author Maxime Devos
 */
public class Main1 {

    static class A {

        @Inject.Item(value = "default:resource")
        ItemType item;

        @Inject.Item(value = "default:stone")
        BlockType stone;
    }

    public static void main(String args[]) throws Throwable {
        try {
            A a = new A();
            Utils.init();
            VoxelContext context = new VoxelContext();
            new DefaultInit(context).init();
            new Inject().inject(context, a);
            //  new Main1().run();
        } finally {
            Thread.sleep(1000);
            ((LoggerContext) LoggerFactory.getILoggerFactory()).stop();
        }
    }

    public void run() throws Exception {
        double rotation = 0;
        double x = 0;
        double y = 0;
        double z = -8;
        VoxelContext context = new VoxelContext();
        new DefaultInit(context).init();
        context.registerBlock(new BlockType("test:diamond", context).setColour(new GLColor(0, 0, 1, 0.8)));
        context.registerBlock(new BlockType("test:gold", context).setColour(new GLColor(0.8, 0.8, 0.8, 0.96)));
        Utils.initClient();
        World world = new DefaultWorld(context, Paths.get("target"));
        world.loadChunk(0, 0);
        new SphereGen(16, (sworld, sx, sy, sz) -> sworld.setBlockAt(sx, sy, sz,
                context.blockRegistry.getObject("default:stone"))).generate(world, 0, 20, 0, null);
        BlockType[] blocks = new BlockType[] { context.blockRegistry.getObject("default:stone"),
                context.blockRegistry.getObject("default:dirt"), context.blockRegistry.getObject("default:grass") };
        for (int k = 0; k < 16 / 2; k++) {
            for (int i = k; i < 16 - k; i++) {
                for (int j = k; j < 16 - k; j++) {
                    world.setBlockAt(i, k, j, blocks[k % blocks.length]);
                }
            }
        }

        for (int i = 3; i < 14; i++) {
            for (int j = 3; j < 14; j++) {
                world.setBlockAt(i, 3, j, context.blockRegistry.getObject("default:grass"));
            }
        }
        while (!Display.isCloseRequested()) {
            Keyboard.poll();
            if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
                x += (double) 1 / 20;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
                x -= (double) 1 / 20;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
                y -= (double) 1 / 5;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
                y += (double) 1 / 5;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                z += (double) 1 / 20;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_Z)) {
                z -= (double) 1 / 20;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_0)) {
                rotation += (double) 1 / 2;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_1)) {
                rotation -= (double) 1 / 2;
            }
            context.clientLoopInnerStart();
            GL11.glTranslated(x, y, z);
            GL11.glRotated(rotation, 0, 1, 0);
            context.renderWorld(world, (chunk) -> true);
            context.clientLoopInnerEnd();
        }
        Utils.destroyClient();
    }
}