hextris/js/save-state.js

64 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2014-08-23 02:10:55 +00:00
function exportSaveState() {
var state = {};
if(gameState == 1 || gameState == -1 || (gameState === 0 && localStorage.getItem('saveState') !== undefined)) {
state = {
hex: $.extend(true, {}, MainHex),
blocks: $.extend(true, [], blocks),
score: score,
wavegen: waveone,
gdx: gdx,
gdy: gdy,
comboTime:settings.comboTime
};
state.hex.blocks.map(function(a){
for (var i = 0; i < a.length; i++) {
a[i] = $.extend(true, {}, a[i]);
}
a.map(descaleBlock);
});
for (var i = 0; i < state.blocks.length; i++) {
state.blocks[i] = $.extend(true, {}, state.blocks[i]);
}
state.blocks.map(descaleBlock);
}
localStorage.setItem('highscores', JSON.stringify(highscores));
return JSONfn.stringify(state);
}
function descaleBlock(b) {
b.distFromHex /= settings.scale;
}
function writeHighScores() {
2015-05-24 14:54:12 +00:00
highscores.sort(
2014-08-23 02:10:55 +00:00
function(a,b){
a = parseInt(a, 10);
b = parseInt(b, 10);
if (a < b) {
return 1;
} else if (a > b) {
return -1;
}else {
return 0;
}
}
);
highscores = highscores.slice(0,3);
localStorage.setItem("highscores", JSON.stringify(highscores));
}
function clearSaveState() {
localStorage.setItem("saveState", "{}");
}
function isStateSaved() {
return localStorage.getItem("saveState") != "{}" && localStorage.getItem("saveState") != undefined;
}