player save dat

pull/12/head
valoeghese 2020-02-28 16:17:20 +13:00
parent 9d477d1304
commit 1d35eef9c0
3 changed files with 45 additions and 10 deletions

View File

@ -1,5 +1,7 @@
package com.github.halotroop.litecraft;
import java.io.IOException;
import org.joml.Vector4i;
import org.lwjgl.glfw.GLFW;
@ -30,6 +32,7 @@ public class Litecraft extends Game
private LitecraftSave save;
private Ginger ginger3D;
private static Litecraft INSTANCE;
private static Player player;
//temp stuff to test out fbo fixes
int oldWindowWidth = Window.width;
int oldWindowHeight = Window.height;
@ -56,7 +59,7 @@ public class Litecraft extends Game
Window.setBackgroundColour(0.2f, 0.2f, 0.6f);
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
StaticCube.scaleCube(1f);
Player player = new Player(dirtModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
player = new Player(dirtModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
Camera camera = new FirstPersonCamera(player);
player.isVisible = false;
ginger3D = new Ginger();
@ -85,7 +88,18 @@ public class Litecraft extends Game
public void exit()
{
if (this.world != null)
{
this.world.unloadAllChunks();
try
{ this.save.saveGlobalData(this.world.getSeed(), this.player); }
catch (IOException e)
{
System.err.println("A critical error occurred while trying to save world data!");
e.printStackTrace();
}
}
ginger3D.cleanup();
System.exit(0);
}

View File

@ -5,6 +5,8 @@ import java.util.Random;
import com.github.halotroop.litecraft.world.*;
import com.github.halotroop.litecraft.world.gen.*;
import com.github.hydos.ginger.engine.elements.objects.Player;
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
import tk.valoeghese.sod.*;
@ -110,15 +112,7 @@ public final class LitecraftSave
try
{
globalDataFile.createNewFile(); // create world file
BinaryData data = new BinaryData(); // create empty binary data
DataSection properties = new DataSection(); // create empty data section for properties
properties.writeLong(seed); // write seed at index 0
DataSection playerData = new DataSection();
playerData.writeFloat(0); // default spawn player x/y/z
playerData.writeFloat(0);
playerData.writeFloat(-3);
data.put("properties", properties); // add properties section
data.write(globalDataFile); // write to file
this.writeGlobalData(globalDataFile, seed, new Vector3f(0, 0, -3)); // write global data with default player pos
}
catch (IOException e)
{
@ -132,5 +126,27 @@ public final class LitecraftSave
}
}
public void saveGlobalData(long seed, Player player) throws IOException
{
File globalDataFile = new File(this.file.getPath() + "/global_data.sod");
globalDataFile.createNewFile(); // create world file if it doesn't exist.
writeGlobalData(globalDataFile, seed, player.getPosition());
}
private void writeGlobalData(File globalDataFile, long seed, Vector3f playerPos)
{
BinaryData data = new BinaryData(); // create empty binary data
DataSection properties = new DataSection(); // create empty data section for properties
properties.writeLong(seed); // write seed at index 0
DataSection playerData = new DataSection();
playerData.writeFloat(playerPos.x); // default spawn player x/y/z
playerData.writeFloat(playerPos.y);
playerData.writeFloat(playerPos.z);
data.put("properties", properties); // add properties section to data
data.put("player", playerData); // add player section to data
data.write(globalDataFile); // write to file
}
private static final String SAVE_DIR = "./saves/";
}

View File

@ -152,6 +152,11 @@ public class World implements BlockAccess, WorldGenConstants
chunkPositions.forEach((LongConsumer) (pos -> this.chunks.remove(pos))); // remove all chunks
}
public long getSeed()
{
return this.seed;
}
private static final class GenerationWorld implements BlockAccess, WorldGenConstants
{
GenerationWorld(World parent)