Added autosave
This commit is contained in:
@@ -2,7 +2,12 @@ package io.anuke.mindustry.io;
|
||||
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
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.core.GameState;
|
||||
import io.anuke.mindustry.core.GameState.State;
|
||||
import io.anuke.mindustry.world.GameMode;
|
||||
import io.anuke.mindustry.world.Map;
|
||||
import io.anuke.ucore.core.Settings;
|
||||
@@ -12,6 +17,10 @@ import java.io.IOException;
|
||||
public class Saves {
|
||||
private int nextSlot;
|
||||
private Array<SaveSlot> saves = new Array<>();
|
||||
private SaveSlot current;
|
||||
private boolean saving;
|
||||
|
||||
private AsyncExecutor exec = new AsyncExecutor(1);
|
||||
|
||||
public void load(){
|
||||
saves.clear();
|
||||
@@ -21,6 +30,26 @@ public class Saves {
|
||||
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(){
|
||||
@@ -46,6 +75,16 @@ public class Saves {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void load(){
|
||||
current = this;
|
||||
SaveIO.loadFromSlot(index);
|
||||
}
|
||||
|
||||
public void save(){
|
||||
current = this;
|
||||
SaveIO.isSaveValid(index);
|
||||
}
|
||||
|
||||
public String getDate(){
|
||||
return SaveIO.getTimeString(index);
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ public class LoadDialog extends FloatingDialog{
|
||||
Vars.ui.hideLoading();
|
||||
hide();
|
||||
try{
|
||||
SaveIO.loadFromSlot(slot.index);
|
||||
slot.load();
|
||||
GameState.set(State.playing);
|
||||
Vars.ui.hideMenu();
|
||||
}catch(Exception e){
|
||||
|
||||
@@ -44,19 +44,19 @@ public class SaveDialog extends LoadDialog{
|
||||
if(button.childrenPressed()) return;
|
||||
|
||||
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");
|
||||
|
||||
Timers.runTask(5f, () -> {
|
||||
hide();
|
||||
Vars.ui.hideLoading();
|
||||
try{
|
||||
SaveIO.saveToSlot(slot);
|
||||
slot.save();
|
||||
}catch(Throwable e){
|
||||
e = (e.getCause() == null ? e : e.getCause());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user