Search in sources :

Example 1 with BlockModeled

use of org.asassecreations.voxelgame.world.block.BlockModeled in project Voxel_Game by ASasseCreations.

the class Chunk method save.

public final boolean save() {
    if (operation == null || !operation.equals(ChunkOperation.SAVING)) {
        if (!generated)
            return true;
        System.out.println("CANT SAVE");
        return false;
    }
    if (modified.isEmpty())
        return true;
    final ADatabase database = new ADatabase("Chunk");
    final List<Block> list = new ArrayList<>(Block.BLOCKS.values());
    for (final Vector3f vector : modified.keySet()) {
        final int x = (int) vector.x;
        final int y = (int) vector.y;
        final int z = (int) vector.z;
        final BlockModeled block = blocks[x][y][z];
        final Block texture;
        int id;
        if (block == null || block.preset == null || block.preset.texture == null)
            id = -1;
        else {
            texture = block.preset;
            id = list.indexOf(texture);
        }
        final AObject object = new AObject(x + ":" + y + ":" + z);
        object.addField(AField.Integer("id", id));
        database.addObject(object);
    }
    database.serializeToFile(Content.WORLD_FOLDER + "chunk_" + x + "_" + z + ".dat");
    System.out.println("Saved chunk at [" + x + ", " + z + "]");
    operation = null;
    return true;
}
Also used : BlockModeled(org.asassecreations.voxelgame.world.block.BlockModeled) Vector3f(org.lwjgl.util.vector.Vector3f) ArrayList(java.util.ArrayList) Block(org.asassecreations.voxelgame.world.block.Block) AObject(org.asassecreations.engine.serializer.AObject) ADatabase(org.asassecreations.engine.serializer.ADatabase)

Example 2 with BlockModeled

use of org.asassecreations.voxelgame.world.block.BlockModeled in project Voxel_Game by ASasseCreations.

the class Chunk method load.

public final void load() {
    System.out.println("Loaded at [" + x + ", " + z + "]");
    final ADatabase database = ADatabase.DeserializeFromFile(Content.WORLD_FOLDER + "chunk_" + x + "_" + z + ".dat");
    final List<AObject> modifications = database.objects;
    System.out.println(modifications.size());
    final List<Block> list = new ArrayList<>(Block.BLOCKS.values());
    for (final AObject object : modifications) {
        final String[] parsed = object.getName().split(":");
        final int x = Integer.parseInt(parsed[0]);
        final int y = Integer.parseInt(parsed[1]);
        final int z = Integer.parseInt(parsed[2]);
        final int id = object.findField("id").getInt();
        modified.put(new Vector3f(x, y, z), id);
        if (id == -1)
            blocks[x][y][z] = null;
        else
            blocks[x][y][z] = new BlockModeled(list.get(id));
    }
    System.out.println("Generated modifications chunk at [" + x + ", " + z + "]");
}
Also used : BlockModeled(org.asassecreations.voxelgame.world.block.BlockModeled) Vector3f(org.lwjgl.util.vector.Vector3f) ArrayList(java.util.ArrayList) Block(org.asassecreations.voxelgame.world.block.Block) AObject(org.asassecreations.engine.serializer.AObject) ADatabase(org.asassecreations.engine.serializer.ADatabase)

Example 3 with BlockModeled

use of org.asassecreations.voxelgame.world.block.BlockModeled in project Voxel_Game by ASasseCreations.

the class Chunk method generate.

// Generate terrain
public final boolean generate() {
    if (operation == null)
        return false;
    if (!operation.equals(ChunkOperation.GENERATING)) {
        operation = null;
        return false;
    }
    for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
        final float dirtHeight = ChunkGenerator.getDirtHeight(this, x, z);
        for (int y = 0; y < dirtHeight; y++) {
            final Block texture = Block.BLOCKS.get("dirt");
            blocks[x][y][z] = new BlockModeled(texture);
        }
        blocks[x][(int) dirtHeight][z] = new BlockModeled(Block.BLOCKS.get("grass"));
        if (ChunkGenerator.canTreeSpawn(x + this.x * Chunk.H_SIZE, z + this.z * Chunk.H_SIZE))
            BlockGroup.GROUP_TREE.insert(this, Mathi.constrain(x, 1, H_SIZE - 2), (int) dirtHeight, Mathi.constrain(z, 1, H_SIZE - 2));
        final int bedrockHeight = ChunkGenerator.getBedrockHeight(x + this.x * Chunk.H_SIZE, z + this.z * Chunk.H_SIZE);
        final Block bedrock = Block.BLOCKS.get("bedrock");
        for (int y = 0; y <= bedrockHeight; y++) blocks[x][y][z] = new BlockModeled(bedrock);
    }
    if (new File(Content.WORLD_FOLDER + "chunk_" + x + "_" + z + ".dat").exists())
        load();
    generated = true;
    operation = null;
    return true;
}
Also used : BlockModeled(org.asassecreations.voxelgame.world.block.BlockModeled) Block(org.asassecreations.voxelgame.world.block.Block) File(java.io.File)

Example 4 with BlockModeled

use of org.asassecreations.voxelgame.world.block.BlockModeled in project Voxel_Game by ASasseCreations.

the class Chunk method modelUpdate.

private void modelUpdate() {
    if (!generated)
        return;
    final FloatHolder positions = new FloatHolder();
    final FloatHolder textures = new FloatHolder();
    final FloatHolder normals = new FloatHolder();
    final FloatHolder lighting = new FloatHolder();
    for (int y = 0; y < V_SIZE; y++) {
        if (!layers[y])
            continue;
        for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
            final BlockModeled b = blocks[x][y][z];
            if (b == null || !b.visible)
                continue;
            final float[][] data = generateCube(b, x, y, z);
            positions.add(data[0]);
            textures.add(data[1]);
            normals.add(data[2]);
            lighting.add(data[3]);
        }
    }
    ModelLoader.appendData(positions.get(), 3);
    ModelLoader.appendData(textures.get(), 2);
    ModelLoader.appendData(normals.get(), 3);
    ModelLoader.appendData(lighting.get(), 1);
    model = ModelLoader.loadToVAO(0);
}
Also used : BlockModeled(org.asassecreations.voxelgame.world.block.BlockModeled) FloatHolder(org.asassecreations.engine.math.FloatHolder)

Example 5 with BlockModeled

use of org.asassecreations.voxelgame.world.block.BlockModeled in project Voxel_Game by ASasseCreations.

the class Chunk method occlude.

// Occlude terrain
public final boolean occlude() {
    if (!generated || operation == null || !operation.equals(ChunkOperation.OCCLUDING)) {
        operation = null;
        return false;
    }
    calculateOcclusion();
    calculateSideOcclusion();
    final List<Vec3> lights = new ArrayList<>();
    {
        final List<Chunk> chunksToCheck = new ArrayList<>();
        Chunk cc = null;
        if ((cc = Chunk.getChunk(x - 1, z - 1)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x - 1, z + 0)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x - 1, z + 1)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 0, z - 1)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 0, z + 0)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 0, z + 1)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 1, z - 1)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 1, z + 0)) != null)
            chunksToCheck.add(cc);
        if ((cc = Chunk.getChunk(x + 1, z + 1)) != null)
            chunksToCheck.add(cc);
        cc = null;
        for (final Chunk c : chunksToCheck) for (int y = 0; y < V_SIZE; y++) for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
            final BlockModeled b = c.blocks[x][y][z];
            if (b == null)
                continue;
            if (!b.visible)
                continue;
            if (b.preset.equals(Block.BLOCKS.get("lamp"))) {
                final Vec3 pos = new Vec3(x + .5f, y + .5f, z + .5f);
                Vec3.add(pos, new Vec3(c.x * H_SIZE, 0, c.z * H_SIZE), pos);
                lights.add(pos);
            }
        }
    }
    final float[] dist = new float[lights.size()];
    for (int y = 0; y < V_SIZE; y++) for (int z = 0; z < H_SIZE; z++) for (int x = 0; x < H_SIZE; x++) {
        final BlockModeled b = blocks[x][y][z];
        if (b == null)
            continue;
        if (!b.visible)
            continue;
        if (b.preset.equals(Block.BLOCKS.get("lamp"))) {
            b.brightness = 1f;
            continue;
        }
        final Vec3 Bpos = new Vec3(x + .5f, y + .5f, z + .5f);
        Vec3.add(Bpos, new Vec3(this.x * H_SIZE, 0, this.z * H_SIZE), Bpos);
        for (int i = 0; i < lights.size(); i++) {
            final Vec3 Lpos = lights.get(i);
            dist[i] = Mathf.distance(Lpos, Bpos);
        }
        float total = 0;
        for (final float distance : dist) total += Mathf.constrain(Mathf.map(distance, 1, 8, 1, 0), 0f, 1f);
        b.brightness = total;
    }
    // calculate each block light value
    occluded = true;
    operation = null;
    return true;
}
Also used : BlockModeled(org.asassecreations.voxelgame.world.block.BlockModeled) Vec3(org.asassecreations.engine.math.vector.Vec3) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BlockModeled (org.asassecreations.voxelgame.world.block.BlockModeled)8 ArrayList (java.util.ArrayList)4 Block (org.asassecreations.voxelgame.world.block.Block)3 Vector3f (org.lwjgl.util.vector.Vector3f)3 Vec3 (org.asassecreations.engine.math.vector.Vec3)2 ADatabase (org.asassecreations.engine.serializer.ADatabase)2 AObject (org.asassecreations.engine.serializer.AObject)2 File (java.io.File)1 List (java.util.List)1 FloatHolder (org.asassecreations.engine.math.FloatHolder)1 Chunk (org.asassecreations.voxelgame.world.chunk.Chunk)1