mirror of https://github.com/espruino/BangleApps
v0.09 added settings menu, removed symbols button,
added highscore reset, clockmode optionalpull/1652/head
parent
5d76a5cd46
commit
164683fce5
|
@ -6,3 +6,4 @@
|
|||
0.06: Fixed issue 1609 added a message popup state handler to control unwanted screen redraw
|
||||
0.07: Optimized the mover algorithm for efficiency (work in progress)
|
||||
0.08: Bug fix at end of the game with victorious splash and glorious orchestra
|
||||
0.09: Added settings menu, removed symbol selection button (*), added highscore reset
|
|
@ -1,6 +1,20 @@
|
|||
const debugMode = 'off'; // valid values are: off, test, production, development
|
||||
let settings = Object.assign({
|
||||
// default values
|
||||
maxUndoLevels: 4,
|
||||
charIndex: 0,
|
||||
clockMode: true,
|
||||
debugMode: false,
|
||||
}, require('Storage').readJSON("game1024.settings.json", true) || {});
|
||||
|
||||
const clockMode = settings.clockMode!==undefined ? settings.clockMode : true;
|
||||
const debugMode = settings.debugMode!==undefined ? settings.debugMode : false; // #settings -- valid values are: true or false
|
||||
const maxUndoLevels = settings.maxUndoLevels!==undefined ? settings.maxUndoLevels : 4; // #settings
|
||||
const charIndex = settings.charIndex!==undefined ? settings.charIndex : 0; // #settings -- plain numbers on the grid
|
||||
|
||||
delete settings; // remove unneeded settings from memory
|
||||
|
||||
const middle = {x:Math.floor(g.getWidth()/2)-20, y: Math.floor(g.getHeight()/2)};
|
||||
const rows = 4, cols = 4;
|
||||
const rows = 4, cols = 4; // #settings
|
||||
const borderWidth = 6;
|
||||
const sqWidth = (Math.floor(Bangle.appRect.w - 48) / rows) - borderWidth;
|
||||
const cellColors = [{bg:'#00FFFF', fg: '#000000'},
|
||||
|
@ -13,12 +27,8 @@ const cellChars = [
|
|||
['0','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
|
||||
['0','I', 'II', 'III', 'IV', 'V', 'VI', 'VII','VIII', 'IX', 'X']
|
||||
];
|
||||
// const numInitialCells = 2;
|
||||
const maxUndoLevels = 4;
|
||||
const noExceptions = true;
|
||||
let charIndex = 0; // plain numbers on the grid
|
||||
const themeBg = g.theme.bg;
|
||||
|
||||
const themeBg = g.theme.bg;
|
||||
|
||||
const scores = {
|
||||
currentScore: 0,
|
||||
|
@ -78,12 +88,12 @@ const snapshot = {
|
|||
updCounter: function() {
|
||||
this.counter = ++this.counter > this.interval ? 0 : this.counter;
|
||||
},
|
||||
dump: {gridsize: rows * cols, expVals: [], score: 0, highScore: 0, charIndex: charIndex},
|
||||
dump: {gridsize: rows * cols, expVals: [], score: 0, highScore: 0},
|
||||
write: function() {
|
||||
require("Storage").writeJSON(this.snFileName, this.dump);
|
||||
},
|
||||
read: function () {
|
||||
let sn = require("Storage").readJSON(this.snFileName, noExceptions);
|
||||
let sn = require("Storage").readJSON(this.snFileName, true);
|
||||
if ((typeof sn == "undefined") || (sn.gridsize !== rows * cols)) {
|
||||
require("Storage").writeJSON(this.snFileName, this.dump);
|
||||
return false;
|
||||
|
@ -101,7 +111,6 @@ const snapshot = {
|
|||
});
|
||||
this.dump.score = scores.currentScore;
|
||||
this.dump.highScore = scores.highScore;
|
||||
this.dump.charIndex = charIndex;
|
||||
},
|
||||
make: function () {
|
||||
this.updCounter();
|
||||
|
@ -118,7 +127,7 @@ const snapshot = {
|
|||
});
|
||||
scores.currentScore = this.dump.score ? this.dump.score : 0;
|
||||
scores.highScore = this.dump.highScore ? this.dump.highScore : 0 ;
|
||||
charIndex = this.dump.charIndex ? this.dump.charIndex : 0 ;
|
||||
if (this.dump.hasOwnProperty('charIndex')) delete this.dump.charIndex; // depricated in v0.09
|
||||
}
|
||||
},
|
||||
reset: function () {
|
||||
|
@ -129,12 +138,11 @@ const snapshot = {
|
|||
}
|
||||
this.dump.score = 0;
|
||||
this.dump.highScore = scores.highScore;
|
||||
this.dump.charIndex = charIndex;
|
||||
this.write();
|
||||
debug(() => console.log("reset D U M P E D!", this.dump));
|
||||
}
|
||||
};
|
||||
const btnAtribs = {x: 134, w: 42, h: 42, fg:'#C0C0C0', bg:'#800000'};
|
||||
const btnAtribs = {x: 134, w: 42, h: 50, fg:'#C0C0C0', bg:'#800000'};
|
||||
const buttons = {
|
||||
all: [],
|
||||
draw: function () {
|
||||
|
@ -314,7 +322,7 @@ class Cell {
|
|||
}
|
||||
drawBg() {
|
||||
debug(()=>console.log("Drawbg!!"));
|
||||
if (this.isRndm == true) {
|
||||
if (this.isRndm) {
|
||||
debug(()=>console.log('Random: (ax)', this.ax));
|
||||
g.setColor(this.getColor(this.expVal).bg)
|
||||
.fillRect(this.x0, this.y0, this.x1, this.y1)
|
||||
|
@ -365,7 +373,7 @@ class Cell {
|
|||
this.isRndm = true;
|
||||
}
|
||||
drawRndmIndicator(){
|
||||
if (this.isRndm == true) {
|
||||
if (this.isRndm) {
|
||||
debug(()=>console.log('Random: (ax)', this.ax));
|
||||
g.setColor(this.getColor(0).bg)
|
||||
.fillPoly(this.ax,this.ay,this.bx,this.by,this.cx,this.cy);
|
||||
|
@ -374,8 +382,9 @@ class Cell {
|
|||
}
|
||||
|
||||
function undoGame() {
|
||||
g.clear();
|
||||
|
||||
if (scores.lastScores.length > 0) {
|
||||
g.clear();
|
||||
allSquares.forEach(sq => {
|
||||
sq.popFromUndo();
|
||||
sq.drawBg();
|
||||
|
@ -386,9 +395,9 @@ function undoGame() {
|
|||
buttons.draw();
|
||||
updUndoLvlIndex();
|
||||
snapshot.make();
|
||||
}
|
||||
Bangle.loadWidgets();
|
||||
Bangle.drawWidgets();
|
||||
}
|
||||
}
|
||||
function addToUndo() {
|
||||
allSquares.forEach(sq => {
|
||||
|
@ -487,8 +496,8 @@ function initGame() {
|
|||
drawGrid();
|
||||
scores.draw();
|
||||
buttons.draw();
|
||||
// Clock mode allows short-press on button to exit
|
||||
Bangle.setUI("clock");
|
||||
// #settings Clock mode allows short-press on button to exit
|
||||
if(clockMode) Bangle.setUI("clock");
|
||||
// Load widgets
|
||||
Bangle.loadWidgets();
|
||||
Bangle.drawWidgets();
|
||||
|
@ -560,14 +569,8 @@ function resetGame() {
|
|||
* @param {function} func function to call like console.log()
|
||||
*/
|
||||
const debug = (func) => {
|
||||
switch (debugMode) {
|
||||
case "development":
|
||||
if (typeof func === 'function') {
|
||||
func();
|
||||
}
|
||||
break;
|
||||
case "off":
|
||||
default: break;
|
||||
if (debugMode) {
|
||||
if (typeof func === 'function') func();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -690,13 +693,9 @@ function updUndoLvlIndex() {
|
|||
.drawString(scores.lastScores.length, x, y);
|
||||
}
|
||||
}
|
||||
function incrCharIndex() {
|
||||
charIndex++;
|
||||
if (charIndex >= cellChars.length) charIndex = 0;
|
||||
drawGrid();
|
||||
}
|
||||
|
||||
buttons.add(new Button('undo', btnAtribs.x, 25, btnAtribs.w, btnAtribs.h, 'U', btnAtribs.fg, btnAtribs.bg, undoGame, true));
|
||||
buttons.add(new Button('chars', btnAtribs.x, 71, btnAtribs.w, 31, '*', btnAtribs.fg, btnAtribs.bg, function(){incrCharIndex();}, true));
|
||||
|
||||
buttons.add(new Button('restart', btnAtribs.x, 106, btnAtribs.w, btnAtribs.h, 'R', btnAtribs.fg, btnAtribs.bg, function(){drawPopUp('Do you want\nto restart?',handlePopUpClicks);}, true));
|
||||
|
||||
initGame();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{ "id": "game1024",
|
||||
"name": "1024 Game",
|
||||
"shortName" : "1024 Game",
|
||||
"version": "0.08",
|
||||
"version": "0.09",
|
||||
"icon": "game1024.png",
|
||||
"screenshots": [ {"url":"screenshot.png" } ],
|
||||
"readme":"README.md",
|
||||
|
@ -12,6 +12,7 @@
|
|||
"supports" : ["BANGLEJS2"],
|
||||
"storage": [
|
||||
{"name":"game1024.app.js","url":"app.js"},
|
||||
{"name":"game1024.settings.js","url":"settings.js"},
|
||||
{"name":"game1024.img","url":"app-icon.js","evaluate":true}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
(function(back) {
|
||||
var FILE = "game1024.settings.json";
|
||||
var scoreFile = "game1024.json";
|
||||
// Load settings
|
||||
var settings = Object.assign({
|
||||
maxUndoLevels: 5,
|
||||
charIndex: 0,
|
||||
clockMode: true,
|
||||
debugMode: false,
|
||||
}, require('Storage').readJSON(FILE, true) || {});
|
||||
|
||||
function writeSettings() {
|
||||
require('Storage').writeJSON(FILE, settings);
|
||||
}
|
||||
var symbols = ["1 2 3 ...", "A B C ...", "I II III..."];
|
||||
var settingsMenu = {
|
||||
"" : { "title" : "1024 Game" },
|
||||
"< Back" : () => back(),
|
||||
"Symbols": {
|
||||
value: 0|settings.charIndex,
|
||||
min:0,max:symbols.length-1,
|
||||
format: v=>symbols[v],
|
||||
onchange: v=> { settings.charIndex=v; writeSettings();}
|
||||
}
|
||||
,
|
||||
"Undo levels:": {
|
||||
value: 0|settings.maxUndoLevels, // 0| converts undefined to 0
|
||||
min: 0, max: 9,
|
||||
onchange: v => {
|
||||
settings.maxUndoLevels = v;
|
||||
writeSettings();
|
||||
}
|
||||
},
|
||||
"Exit press:": {
|
||||
value: !settings.debugMode, // ! converts undefined to true
|
||||
format: v => v?"short":"long",
|
||||
onchange: v => {
|
||||
settings.debugMode = v;
|
||||
writeSettings();
|
||||
},
|
||||
},
|
||||
"Debug mode:": {
|
||||
value: !!settings.debugMode, // !! converts undefined to false
|
||||
format: v => v?"On":"Off",
|
||||
onchange: v => {
|
||||
settings.debugMode = v;
|
||||
writeSettings();
|
||||
}
|
||||
},
|
||||
"Reset Highscore": () => {
|
||||
E.showPrompt('Reset Highscore?').then((v) => {
|
||||
let delay = 50;
|
||||
if (v) {
|
||||
delay = 500;
|
||||
let sF = require("Storage").readJSON(scoreFile, true);
|
||||
if (typeof sF !== "undefined") {
|
||||
E.showMessage('Resetting');
|
||||
sF.highScore = 0;
|
||||
require("Storage").writeJSON(scoreFile, sF);
|
||||
} else {
|
||||
E.showMessage('No highscore!');
|
||||
}
|
||||
}
|
||||
setTimeout(() => E.showMenu(settingsMenu), delay);
|
||||
});
|
||||
}
|
||||
}
|
||||
// Show the menu
|
||||
E.showMenu(settingsMenu);
|
||||
})
|
Loading…
Reference in New Issue