Search in sources :

Example 1 with ADatabase

use of org.asassecreations.engine.serializer.ADatabase 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 ADatabase

use of org.asassecreations.engine.serializer.ADatabase 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 ADatabase

use of org.asassecreations.engine.serializer.ADatabase in project Voxel_Game by ASasseCreations.

the class Inventory method load.

public void load() {
    if (!new File(Content.WORLD_FOLDER + "inventory.dat").exists())
        return;
    final ADatabase database = ADatabase.DeserializeFromFile(Content.WORLD_FOLDER + "inventory.dat");
    final List<AObject> objects = database.objects;
    for (final AObject object : objects) {
        final int index = Integer.parseInt(object.getName().split("SLOT")[1]);
        final int id = object.findField("id").getInt();
        final int amount = object.findField("amount").getInt();
        inventory[index] = new ItemStack(ItemPreset.get(id), amount);
    }
    for (int i = 0; i < inventory.length; i++) {
        if (inventory[i] == null)
            continue;
        final AObject object = new AObject("SLOT" + i);
        object.addField(AField.Integer("id", inventory[i].item.id));
        object.addField(AField.Integer("amount", inventory[i].amount));
        database.addObject(object);
    }
}
Also used : AObject(org.asassecreations.engine.serializer.AObject) File(java.io.File) ADatabase(org.asassecreations.engine.serializer.ADatabase)

Example 4 with ADatabase

use of org.asassecreations.engine.serializer.ADatabase in project Voxel_Game by ASasseCreations.

the class PlayerSave method save.

public static final void save(final Player player) {
    final ADatabase database = new ADatabase("Player");
    final AObject object = new AObject("Player");
    object.addField(AField.Float("x", player.position.x));
    object.addField(AField.Float("y", player.position.y));
    object.addField(AField.Float("z", player.position.z));
    object.addField(AField.Float("rotation", player.rotation));
    object.addField(AField.Long("seed", ChunkGenerator.SEED));
    object.addField(AField.Float("time", Time.rawTime));
    database.addObject(object);
    database.serializeToFile(Content.WORLD_FOLDER + "Player.dat");
}
Also used : AObject(org.asassecreations.engine.serializer.AObject) ADatabase(org.asassecreations.engine.serializer.ADatabase)

Example 5 with ADatabase

use of org.asassecreations.engine.serializer.ADatabase in project Voxel_Game by ASasseCreations.

the class PlayerSave method load.

public static final void load(final Player player) {
    player.position.x = 8;
    player.position.y = 120;
    player.position.z = 8;
    player.rotation = 25;
    ChunkGenerator.init(new Random().nextLong());
    Time.rawTime = .1f;
    if (!new File(Content.WORLD_FOLDER + "Player.dat").exists())
        return;
    final ADatabase database = ADatabase.DeserializeFromFile(Content.WORLD_FOLDER + "Player.dat");
    final AObject object = database.findObject("Player");
    AField temp;
    if ((temp = object.findField("x")) != null)
        player.position.x = temp.getFloat();
    if ((temp = object.findField("y")) != null)
        player.position.y = temp.getFloat();
    if ((temp = object.findField("z")) != null)
        player.position.z = temp.getFloat();
    if ((temp = object.findField("rotation")) != null)
        player.rotation = temp.getFloat();
    if ((temp = object.findField("seed")) != null)
        ChunkGenerator.init(temp.getLong());
    if ((temp = object.findField("time")) != null)
        Time.rawTime = temp.getFloat();
    temp = null;
}
Also used : Random(java.util.Random) AObject(org.asassecreations.engine.serializer.AObject) File(java.io.File) ADatabase(org.asassecreations.engine.serializer.ADatabase) AField(org.asassecreations.engine.serializer.AField)

Aggregations

ADatabase (org.asassecreations.engine.serializer.ADatabase)6 AObject (org.asassecreations.engine.serializer.AObject)6 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Block (org.asassecreations.voxelgame.world.block.Block)2 BlockModeled (org.asassecreations.voxelgame.world.block.BlockModeled)2 Vector3f (org.lwjgl.util.vector.Vector3f)2 Random (java.util.Random)1 AField (org.asassecreations.engine.serializer.AField)1