diff --git a/src/main/Game.cs b/src/main/Game.cs index 199080c..d1ce22c 100644 --- a/src/main/Game.cs +++ b/src/main/Game.cs @@ -667,21 +667,21 @@ private void actualRender(double dt, double interp) { //GD.BlendingEnabled = false; } - public TimerAction setInterval(long interval, Action action) { + public static TimerAction setInterval(long interval, Action action) { var now = permanentStopwatch.ElapsedMilliseconds; var ta = new TimerAction(action, now, true, interval); timerQueue.Add(ta); return ta; } - public TimerAction setTimeout(long timeout, Action action) { + public static TimerAction setTimeout(long timeout, Action action) { var now = permanentStopwatch.ElapsedMilliseconds; var ta = new TimerAction(action, now, false, timeout); timerQueue.Add(ta); return ta; } - private void handleTimers() { + private static void handleTimers() { foreach (var timerAction in timerQueue) { var now = permanentStopwatch.ElapsedMilliseconds; if (timerAction.enabled && now - timerAction.lastCalled > timerAction.interval) { diff --git a/src/ui/screen/GameScreen.cs b/src/ui/screen/GameScreen.cs index 440558c..16d9c2d 100644 --- a/src/ui/screen/GameScreen.cs +++ b/src/ui/screen/GameScreen.cs @@ -35,8 +35,8 @@ public override void activate() { switchToMenu(INGAME_MENU); - updateMemory = Game.instance.setInterval(200, INGAME_MENU.updateMemoryMethod); - updateDebugText = Game.instance.setInterval(50, INGAME_MENU.updateDebugTextMethod); + updateMemory = Game.setInterval(200, INGAME_MENU.updateMemoryMethod); + updateDebugText = Game.setInterval(50, INGAME_MENU.updateDebugTextMethod); } public void setWorld(World inWorld) { diff --git a/src/world/World.cs b/src/world/World.cs index a762ce8..5590e4e 100644 --- a/src/world/World.cs +++ b/src/world/World.cs @@ -54,6 +54,7 @@ public class World : IDisposable { public int worldTick; public Random random; + private readonly TimerAction saveWorld; private const long MAX_CHUNKLOAD_FRAMETIME = 20; private const long MAX_LIGHT_FRAMETIME = 10; @@ -101,6 +102,14 @@ public World(string name, int seed, bool loadingSave = false) { // if we don't save the world, some of the chunks might get saved but no level.xnbt // so the world is corrupted and we have horrible chunkglitches worldIO.save(this, name, false); + + + // setup world saving every 5 mins + saveWorld = Game.setInterval(5 * 60 * 1000, saveWorldMethod); + } + + private void saveWorldMethod() { + worldIO.saveWorldData(); } public void startMeshing() { @@ -401,6 +410,7 @@ private void ReleaseUnmanagedResources() { public void Dispose() { // of course, we can save it here since WE call it and not the GC worldIO.save(this, name); + saveWorld.enabled = false; ReleaseUnmanagedResources(); GC.SuppressFinalize(this); } diff --git a/src/world/WorldIO.cs b/src/world/WorldIO.cs index b1d85ea..8c8eb9e 100644 --- a/src/world/WorldIO.cs +++ b/src/world/WorldIO.cs @@ -27,12 +27,7 @@ public void save(World world, string filename, bool saveChunks = true) { Directory.CreateDirectory($"level/{filename}"); } - var tag = new NBTCompound("world"); - tag.addInt("seed", world.seed); - tag.addDouble("posX", world.player.position.X); - tag.addDouble("posY", world.player.position.Y); - tag.addDouble("posZ", world.player.position.Z); - NBT.writeFile(tag, $"level/{filename}/level.xnbt"); + saveWorldData(); // save chunks if (saveChunks) { @@ -44,6 +39,15 @@ public void save(World world, string filename, bool saveChunks = true) { //regionCache.Clear(); } + public void saveWorldData() { + var tag = new NBTCompound("world"); + tag.addInt("seed", world.seed); + tag.addDouble("posX", world.player.position.X); + tag.addDouble("posY", world.player.position.Y); + tag.addDouble("posZ", world.player.position.Z); + NBT.writeFile(tag, $"level/{world.name}/level.xnbt"); + } + public void saveChunk(World world, Chunk chunk) { var nbt = serialiseChunkIntoNBT(chunk); // ensure directory is created @@ -110,6 +114,9 @@ public static World load(string filename) { var tag = NBT.readFile($"level/{filename}/level.xnbt"); var seed = tag.getInt("seed"); var world = new World(filename, seed, true); + Console.Out.WriteLine(tag.getDouble("posX")); + Console.Out.WriteLine(tag.getDouble("posY")); + Console.Out.WriteLine(tag.getDouble("posZ")); world.player.position.X = tag.getDouble("posX"); world.player.position.Y = tag.getDouble("posY"); world.player.position.Z = tag.getDouble("posZ");