Added autosave

This commit is contained in:
Anuken
2017-12-29 16:20:20 -05:00
parent 036a246769
commit 152dc515db
4 changed files with 44 additions and 5 deletions

View File

@@ -25,7 +25,7 @@ io.anuke.ucore.scene.Skin$TintedDrawable: {
dialogDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.9} }, dialogDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.9} },
invis: {name: white, color: {r: 0, g: 0, b: 0, a: 0} } invis: {name: white, color: {r: 0, g: 0, b: 0, a: 0} }
loadDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.8} }, loadDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.8} },
clear: {name: white, color: {r: 0.1, g: 0.1, b: 0.1, a: 0.7}}, clear: {name: white, color: {r: 0.1, g: 0.1, b: 0.1, a: 0.75}},
clear-over: {name: white, color: {r: 1, g: 1, b: 1, a: 0.2} }, clear-over: {name: white, color: {r: 1, g: 1, b: 1, a: 0.2} },
clear-down: {name: white, color: {r: 1, g: 1, b: 1, a: 0.4} } clear-down: {name: white, color: {r: 1, g: 1, b: 1, a: 0.4} }
}, },

View File

@@ -2,7 +2,12 @@ package io.anuke.mindustry.io;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.badlogic.gdx.utils.async.AsyncExecutor;
import io.anuke.mindustry.Vars; import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.world.GameMode; import io.anuke.mindustry.world.GameMode;
import io.anuke.mindustry.world.Map; import io.anuke.mindustry.world.Map;
import io.anuke.ucore.core.Settings; import io.anuke.ucore.core.Settings;
@@ -12,6 +17,10 @@ import java.io.IOException;
public class Saves { public class Saves {
private int nextSlot; private int nextSlot;
private Array<SaveSlot> saves = new Array<>(); private Array<SaveSlot> saves = new Array<>();
private SaveSlot current;
private boolean saving;
private AsyncExecutor exec = new AsyncExecutor(1);
public void load(){ public void load(){
saves.clear(); saves.clear();
@@ -21,6 +30,26 @@ public class Saves {
nextSlot = i + 1; nextSlot = i + 1;
} }
} }
Timer.schedule(new Task() {
@Override
public void run() {
if(!GameState.is(State.menu) && !GameState.is(State.dead) && current != null && current.isAutosave()){
saving = true;
exec.submit(() -> {
SaveIO.saveToSlot(current.index);
saving = false;
return true;
});
}
}
}, 0f, 60f*2);
}
public boolean isSaving(){
return saving;
} }
public boolean canAddSave(){ public boolean canAddSave(){
@@ -46,6 +75,16 @@ public class Saves {
this.index = index; this.index = index;
} }
public void load(){
current = this;
SaveIO.loadFromSlot(index);
}
public void save(){
current = this;
SaveIO.isSaveValid(index);
}
public String getDate(){ public String getDate(){
return SaveIO.getTimeString(index); return SaveIO.getTimeString(index);
} }

View File

@@ -162,7 +162,7 @@ public class LoadDialog extends FloatingDialog{
Vars.ui.hideLoading(); Vars.ui.hideLoading();
hide(); hide();
try{ try{
SaveIO.loadFromSlot(slot.index); slot.load();
GameState.set(State.playing); GameState.set(State.playing);
Vars.ui.hideMenu(); Vars.ui.hideMenu();
}catch(Exception e){ }catch(Exception e){

View File

@@ -44,19 +44,19 @@ public class SaveDialog extends LoadDialog{
if(button.childrenPressed()) return; if(button.childrenPressed()) return;
Vars.ui.showConfirm("$text.overwrite", "$text.save.overwrite", () -> { Vars.ui.showConfirm("$text.overwrite", "$text.save.overwrite", () -> {
save(slot.index); save(slot);
}); });
}); });
} }
void save(int slot){ void save(SaveSlot slot){
Vars.ui.showLoading("$text.saveload"); Vars.ui.showLoading("$text.saveload");
Timers.runTask(5f, () -> { Timers.runTask(5f, () -> {
hide(); hide();
Vars.ui.hideLoading(); Vars.ui.hideLoading();
try{ try{
SaveIO.saveToSlot(slot); slot.save();
}catch(Throwable e){ }catch(Throwable e){
e = (e.getCause() == null ? e : e.getCause()); e = (e.getCause() == null ? e : e.getCause());