Remove some unused globals & functions, add back functionality to help screens, fix some "else" statements, remove some unused commented code,

pull/3021/head
Willems Davy 2023-09-14 21:16:49 +02:00
parent c1043379f2
commit d49ea7333e
1 changed files with 139 additions and 229 deletions

View File

@ -54,22 +54,15 @@ const DEBUGMODEINPUT = 0;
const DEBUGMODESPEED = 1; const DEBUGMODESPEED = 1;
const DEBUGMODERAMUSE = 1; const DEBUGMODERAMUSE = 1;
const SCREENWIDTH = 176;
const SCREENHEIGHT = 176;
const TILESIZE = 10; const TILESIZE = 10;
const SCREENWIDTH = g.getWidth();
const SCREENHEIGHT = g.getHeight();
const SCREENOFFSETX = ((SCREENWIDTH - 16 * TILESIZE) >> 1); const SCREENOFFSETX = ((SCREENWIDTH - 16 * TILESIZE) >> 1);
const MAXBOARDWIDTH = 10; const MAXBOARDWIDTH = 10;
const MAXBOARDHEIGHT = 8; const MAXBOARDHEIGHT = 8;
const MAXBOARDBGWIDTH = 10;
const MAXBOARDBGHEIGHT = 8;
const MAXBOARDSIZE = MAXBOARDWIDTH * MAXBOARDHEIGHT; const MAXBOARDSIZE = MAXBOARDWIDTH * MAXBOARDHEIGHT;
const GSGAME = 0; const GSGAME = 0;
const GSTITLE = 1; const GSTITLE = 1;
const GSLEVELSELECT = 2; const GSLEVELSELECT = 2;
@ -104,7 +97,6 @@ const GSINITHELPSLIDE2 = GSINITDIFF + GSHELPSLIDE2;
const GSINITHELPSLIDE3 = GSINITDIFF + GSHELPSLIDE3; const GSINITHELPSLIDE3 = GSINITDIFF + GSHELPSLIDE3;
const GSINITINTRO = GSINITDIFF + GSINTRO; const GSINITINTRO = GSINITDIFF + GSINTRO;
const DIFFVERYEASY = 0; const DIFFVERYEASY = 0;
const DIFFEASY = 1; const DIFFEASY = 1;
const DIFFNORMAL = 2; const DIFFNORMAL = 2;
@ -145,8 +137,6 @@ const ARROWRIGHT = 121;
const LEFTMENU = 118; const LEFTMENU = 118;
const EMPTY = 61; const EMPTY = 61;
const FRAMERATE = 15;
let startPos; let startPos;
let menuPos; let menuPos;
let maxLevel; let maxLevel;
@ -174,17 +164,13 @@ let redrawPartial;
let screenOffsetY; let screenOffsetY;
// Cursor // Cursor
const maxCursorFrameCount = (10 * FRAMERATE / 60);
const cursorAnimCount = 2; //blink on & off
const cursorNumTiles = 16; //for the max 2 cursors shown at once (on help screens) const cursorNumTiles = 16; //for the max 2 cursors shown at once (on help screens)
let showCursor;
let cursorFrameCount, cursorFrame, showCursor;
let spritePos = []; let spritePos = [];
//intro //intro
let frames; let frames;
let titlePosY; let titlePosY;
const frameDelay = 16 * FRAMERATE / 15;
//savestate //savestate
let levelLocks = new Uint8Array(GMCOUNT * DIFFCOUNT); let levelLocks = new Uint8Array(GMCOUNT * DIFFCOUNT);
@ -193,9 +179,7 @@ let options = new Uint8Array(OPCOUNT);
//sound //sound
let soundon = 1; let soundon = 1;
//game //game
let paused; let paused;
let wasSoundOn; let wasSoundOn;
let redrawLevelDoneBit; let redrawLevelDoneBit;
@ -213,13 +197,12 @@ let btnb = false;
// random stuff // random stuff
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#72732727 // https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript#72732727
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
let randfunc; let randfunc;
function srand(seed) { function srand(seed) {
let m = Math.pow(2, 35) - 31; var m = Math.pow(2, 35) - 31;
let a = 185852; var a = 185852;
let s = seed % m; var s = seed % m;
randfunc = function() { randfunc = function() {
return (s = s * a % m); return (s = s * a % m);
}; };
@ -234,7 +217,6 @@ srand(Date().getTime());
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Sound stuff // Sound stuff
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function setSoundOn(val) { function setSoundOn(val) {
soundOn = val; soundOn = val;
} }
@ -282,15 +264,13 @@ function playGameAction() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Cursor stuff // Cursor stuff
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function move_sprite(sprite, x, y) { function move_sprite(sprite, x, y) {
spritePos[sprite][0] = x; spritePos[sprite][0] = x;
spritePos[sprite][1] = y; spritePos[sprite][1] = y;
} }
function drawCursors(clear) { function drawCursors(clear) {
if ((showCursor == 0) || (cursorFrame & 1)) // 2nd or to add blink effect, it will skip drawing if bit 1 is set if (showCursor == 0)
return; return;
for (let i = 0; i < cursorNumTiles; i++) for (let i = 0; i < cursorNumTiles; i++)
if (spritePos[i][1] < SCREENHEIGHT) if (spritePos[i][1] < SCREENHEIGHT)
@ -299,19 +279,6 @@ function drawCursors(clear) {
}); });
} }
//returns 1 if cursor has changed / needs redraw
function updateCursorFrame() {
cursorFrameCount++;
if (cursorFrameCount >= maxCursorFrameCount) {
cursorFrame++;
cursorFrameCount = 0;
if (cursorFrame >= cursorAnimCount)
cursorFrame = 0;
return 1;
}
return 0;
}
function hideCursors() { function hideCursors() {
//HIDE CURSOR SPRITES //HIDE CURSOR SPRITES
//cursor 0 //cursor 0
@ -344,15 +311,11 @@ function setCursorPos(cursorNr, xPos, yPos) {
function initCursors() { function initCursors() {
hideCursors(); hideCursors();
cursorFrameCount = 0;
cursorFrame = 0;
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// helper funcs // helper funcs
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function set_bkg_tile_xy(x, y, tile) { function set_bkg_tile_xy(x, y, tile) {
g.drawImage(currentTiles, SCREENOFFSETX + x * TILESIZE, screenOffsetY + y * TILESIZE, { g.drawImage(currentTiles, SCREENOFFSETX + x * TILESIZE, screenOffsetY + y * TILESIZE, {
frame: tile frame: tile
@ -378,8 +341,6 @@ function setBlockTilesAsBackground() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// help screens // help screens
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
//LEGEND STATE //LEGEND STATE
function inithelpLegend() { function inithelpLegend() {
setBlockTilesAsBackground(); setBlockTilesAsBackground();
@ -400,6 +361,11 @@ function helpLegend(nextState) {
gameState = nextState; gameState = nextState;
} }
if (btnb) {
playMenuBackSound();
gameState = GSINITTITLE;
}
if (needRedraw) { if (needRedraw) {
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
switch (gameState) { switch (gameState) {
@ -414,27 +380,27 @@ function helpLegend(nextState) {
break; break;
} }
set_bkg_tile_xy(0, 1, 33); set_bkg_tile_xy(0, 2, 33);
printMessage(1, 1, ":WATER SOURCE"); printMessage(1, 2, ":WATER SOURCE");
set_bkg_tile_xy(0, 2, 11); set_bkg_tile_xy(0, 3, 11);
set_bkg_tile_xy(1, 2, 6); set_bkg_tile_xy(1, 3, 6);
set_bkg_tile_xy(2, 2, 12); set_bkg_tile_xy(2, 3, 12);
printMessage(3, 2, ":NOT FILLED"); printMessage(3, 3, ":NOT FILLED");
set_bkg_tile_xy(0, 3, 27); set_bkg_tile_xy(0, 4, 27);
set_bkg_tile_xy(1, 3, 22); set_bkg_tile_xy(1, 4, 22);
set_bkg_tile_xy(2, 3, 28); set_bkg_tile_xy(2, 4, 28);
printMessage(3, 3, ":FILLED"); printMessage(3, 4, ":FILLED");
if ((gameState == GSHELPROTATESLIDE) || if ((gameState == GSHELPROTATESLIDE) ||
(gameState == GSHELPSLIDE)) { (gameState == GSHELPSLIDE)) {
set_bkg_tile_xy(0, 4, 121); set_bkg_tile_xy(0, 5, 121);
printMessage(1, 4, ":SLID ROW RIGHT"); printMessage(1, 5, ":SLID ROW RIGHT");
set_bkg_tile_xy(0, 5, 123); set_bkg_tile_xy(0, 6, 123);
printMessage(1, 5, ":SLID ROW LEFT"); printMessage(1, 6, ":SLID ROW LEFT");
set_bkg_tile_xy(0, 6, 122); set_bkg_tile_xy(0, 7, 122);
printMessage(1, 6, ":SLID COL DOWN"); printMessage(1, 7, ":SLID COL DOWN");
set_bkg_tile_xy(0, 7, 120); set_bkg_tile_xy(0, 8, 120);
printMessage(1, 7, ":SLID COL UP"); printMessage(1, 8, ":SLID COL UP");
} }
needRedraw = 0; needRedraw = 0;
requiresFlip = 1; requiresFlip = 1;
@ -461,6 +427,11 @@ function helpFinishLevel(nextState) {
gameState = nextState; gameState = nextState;
} }
if (btnb) {
playMenuBackSound();
gameState = GSINITTITLE;
}
if (needRedraw) { if (needRedraw) {
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
switch (gameState) { switch (gameState) {
@ -554,6 +525,11 @@ function helpDoSlideRotate(nextState) {
hideCursors(); hideCursors();
} }
if (btnb) {
playMenuBackSound();
gameState = GSINITTITLE;
}
if (needRedraw) { if (needRedraw) {
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
@ -659,7 +635,6 @@ function helpDoSlideRotate(nextState) {
set_bkg_tile_xy(3, 6, 7); set_bkg_tile_xy(3, 6, 7);
} }
//2nd grid //2nd grid
set_bkg_tile_xy(12, 4, 25); set_bkg_tile_xy(12, 4, 25);
@ -678,11 +653,8 @@ function helpDoSlideRotate(nextState) {
needRedraw = 0; needRedraw = 0;
requiresFlip = 1; requiresFlip = 1;
} }
//needRedraw = updateCursorFrame();
} }
//LEGEND STATE //LEGEND STATE
function helpRotateSlide() { function helpRotateSlide() {
helpLegend(GSINITHELPROTATESLIDE2); helpLegend(GSINITHELPROTATESLIDE2);
@ -735,7 +707,6 @@ function helpSlide3() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Intro // Intro
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function initIntro() { function initIntro() {
setBlockTilesAsBackground(); setBlockTilesAsBackground();
titlePosY = g.getHeight(); titlePosY = g.getHeight();
@ -754,12 +725,12 @@ function intro() {
frames++; frames++;
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
if (frames < frameDelay) { if (frames < 16) {
//16-12 //16-12
printMessage(4 >> 1, 4, "WILLEMS DAVY"); printMessage(4 >> 1, 4, "WILLEMS DAVY");
requiresFlip = 1; requiresFlip = 1;
} else { } else {
if (frames < frameDelay * 2) { if (frames < 16 * 2) {
//16-8 //16-8
printMessage(8 >> 1, 4, "PRESENTS"); printMessage(8 >> 1, 4, "PRESENTS");
requiresFlip = 1; requiresFlip = 1;
@ -767,7 +738,7 @@ function intro() {
requiresFlip = 1; requiresFlip = 1;
g.drawImage(TITLE, SCREENOFFSETX, titlePosY); g.drawImage(TITLE, SCREENOFFSETX, titlePosY);
if (titlePosY > screenOffsetY) { if (titlePosY > screenOffsetY) {
titlePosY -= 60 / FRAMERATE; titlePosY -= 4;
} else { } else {
gameState = GSINITTITLE; gameState = GSINITTITLE;
} }
@ -778,8 +749,6 @@ function intro() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// Level Stuff // Level Stuff
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function moveBlockDown(aTile) { function moveBlockDown(aTile) {
let tmp = level[aTile + boardSize - boardWidth]; let tmp = level[aTile + boardSize - boardWidth];
for (let i = boardSize - boardWidth; i != 0; i -= boardWidth) for (let i = boardSize - boardWidth; i != 0; i -= boardWidth)
@ -942,7 +911,7 @@ function shuffleLevel() {
function handleConnectPoint(currentPoint, cellStack, cc) { function handleConnectPoint(currentPoint, cellStack, cc) {
let lookUpX = currentPoint % boardWidth; let lookUpX = currentPoint % boardWidth;
let lookUpY = Math.floor(currentPoint / boardWidth); let lookUpY = (currentPoint / boardWidth) | 0;
let tmp; let tmp;
let tmp2; let tmp2;
if ((lookUpY > 0) && (!(level[currentPoint] & 1))) { if ((lookUpY > 0) && (!(level[currentPoint] & 1))) {
@ -1021,7 +990,7 @@ function handleConnectPoint(currentPoint, cellStack, cc) {
} }
function updateConnected() { function updateConnected() {
let cellStack = []; let cellStack = [];//new Uint8Array(boardSize + 1);
//reset all tiles to default not filled one //reset all tiles to default not filled one
for (let i = 0; i != boardSize; i++) { for (let i = 0; i != boardSize; i++) {
if (level[i] > 31) { if (level[i] > 31) {
@ -1044,16 +1013,17 @@ function updateConnected() {
} }
//add start pos special tile //add start pos special tile
if (level[startPos] > 15) if (level[startPos] > 15) {
level[startPos] += 16; level[startPos] += 16;
else } else {
if (level[startPos] < 16) if (level[startPos] < 16)
level[startPos] += 32; level[startPos] += 32;
} }
}
function generateLevel() { function generateLevel() {
let neighbours = new Uint8Array(4); let neighbours = new Uint8Array(4);
let cellStack = new Uint8Array(MAXBOARDSIZE + 1); let cellStack = [];//new Uint8Array(boardSize + 1);
let cc = 0; let cc = 0;
let currentPoint = 0; let currentPoint = 0;
let visitedRooms = 1; let visitedRooms = 1;
@ -1062,17 +1032,14 @@ function generateLevel() {
let neighboursFound; let neighboursFound;
let lookUpX, lookUpY; let lookUpX, lookUpY;
let rnd; let rnd;
//generate a lookup table so we don't have to use modulus or divide constantly
//generateLookupTable(boardWidth, boardHeight);
//intial all walls value in every room we will remove bits of this value to remove walls //intial all walls value in every room we will remove bits of this value to remove walls
for (tmp = 0; tmp < boardSize; tmp++) level.fill(0xf, 0, boardSize);
level[tmp] = 0xf;
while (visitedRooms != boardSize) { while (visitedRooms != boardSize) {
neighboursFound = 0; neighboursFound = 0;
lookUpX = currentPoint % boardWidth; lookUpX = currentPoint % boardWidth;
lookUpY = Math.floor(currentPoint / boardWidth); lookUpY = (currentPoint / boardWidth) | 0;
tmp = currentPoint + 1; tmp = currentPoint + 1;
//tile has neighbour to the right which we did not handle yet //tile has neighbour to the right which we did not handle yet
@ -1094,14 +1061,14 @@ function generateLevel() {
if ((lookUpY + 1 < boardHeight) && (level[tmp] == 0xf)) if ((lookUpY + 1 < boardHeight) && (level[tmp] == 0xf))
neighbours[neighboursFound++] = tmp; neighbours[neighboursFound++] = tmp;
switch (neighboursFound) { if (neighboursFound == 0)
case 0: {
currentPoint = cellStack[--cc]; currentPoint = cellStack[--cc];
continue; continue;
default: } else {
rnd = random(neighboursFound); rnd = random(neighboursFound);
break;
} }
selectedNeighbour = neighbours[rnd]; selectedNeighbour = neighbours[rnd];
tmp = (selectedNeighbour % boardWidth); tmp = (selectedNeighbour % boardWidth);
//tile has neighbour to the east //tile has neighbour to the east
@ -1110,23 +1077,23 @@ function generateLevel() {
level[selectedNeighbour] &= ~(8); level[selectedNeighbour] &= ~(8);
//remove east wall tile //remove east wall tile
level[currentPoint] &= ~(2); level[currentPoint] &= ~(2);
} else // tile has neighbour to the west } else {
{ // tile has neighbour to the west
if (tmp < lookUpX) { if (tmp < lookUpX) {
//remove east wall neighbour //remove east wall neighbour
level[selectedNeighbour] &= ~(2); level[selectedNeighbour] &= ~(2);
//remove west wall tile //remove west wall tile
level[currentPoint] &= ~(8); level[currentPoint] &= ~(8);
} else // tile has neighbour to the north } else {
{ // tile has neighbour to the north
tmp2 = selectedNeighbour / boardWidth; tmp2 = selectedNeighbour / boardWidth;
if (tmp2 < lookUpY) { if (tmp2 < lookUpY) {
//remove south wall neighbour //remove south wall neighbour
level[selectedNeighbour] &= ~(4); level[selectedNeighbour] &= ~(4);
//remove north wall tile //remove north wall tile
level[currentPoint] &= ~(1); level[currentPoint] &= ~(1);
} else // tile has neighbour to the south } else {
{ // tile has neighbour to the south
if (tmp2 > lookUpY) { if (tmp2 > lookUpY) {
//remove north wall neighbour //remove north wall neighbour
level[selectedNeighbour] &= ~(1); level[selectedNeighbour] &= ~(1);
@ -1160,12 +1127,9 @@ function isLevelDone() {
function initLevel(aRandomSeed, noLoading) { function initLevel(aRandomSeed, noLoading) {
let startTime = Date().getTime(); let startTime = Date().getTime();
if (!noLoading) { if (!noLoading) {
//g.setColor(0,0,0); printMessage(((16 - 10) >> 1), (MAXBOARDHEIGHT >> 1) - 1, "[*********]");
//g.fillRect(SCREENOFFSETX + ((16 - 10) >> 1) * TILESIZE, screenOffsetY + ((MAXBOARDBGHEIGHT >> 1) - 1) * TILESIZE, SCREENOFFSETX + (((16 - 10) >> 1) * TILESIZE) + (10*TILESIZE), screenOffsetY + (((MAXBOARDBGHEIGHT >> 1) - 1) * TILESIZE) +(3*TILESIZE)); printMessage(((16 - 10) >> 1), (MAXBOARDHEIGHT >> 1) - 0, "| LOADING +");
//g.setColor(1,1,1); printMessage(((16 - 10) >> 1), (MAXBOARDHEIGHT >> 1) + 1, "<#########>");
printMessage(((16 - 10) >> 1), (MAXBOARDBGHEIGHT >> 1) - 1, "[*********]");
printMessage(((16 - 10) >> 1), (MAXBOARDBGHEIGHT >> 1) - 0, "| LOADING +");
printMessage(((16 - 10) >> 1), (MAXBOARDBGHEIGHT >> 1) + 1, "<#########>");
g.flip(); g.flip();
} }
levelDone = 0; levelDone = 0;
@ -1215,8 +1179,8 @@ function initLevel(aRandomSeed, noLoading) {
//generate the level //generate the level
generateLevel(); generateLevel();
//startpoint of of level in center of screen //startpoint of of level in center of screen
boardX = (MAXBOARDBGWIDTH - boardWidth) >> 1; boardX = (MAXBOARDWIDTH - boardWidth) >> 1;
boardY = (MAXBOARDBGHEIGHT - boardHeight) >> 1; boardY = (MAXBOARDHEIGHT - boardHeight) >> 1;
startPos = (boardWidth >> 1) + (boardHeight >> 1) * (boardWidth); startPos = (boardWidth >> 1) + (boardHeight >> 1) * (boardWidth);
//startpoint of tile with water and our cursor //startpoint of tile with water and our cursor
selectionX = boardWidth >> 1; selectionX = boardWidth >> 1;
@ -1233,8 +1197,6 @@ function initLevel(aRandomSeed, noLoading) {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// levels cleared // levels cleared
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function initLevelsCleared() { function initLevelsCleared() {
set_bkg_data(CONGRATSTILES); set_bkg_data(CONGRATSTILES);
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
@ -1278,17 +1240,14 @@ function levelsCleared() {
needRedraw = 0; needRedraw = 0;
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// level select // level select
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function drawLevelSelect(partial) { function drawLevelSelect(partial) {
if(partial > 2) if (partial > 2) {
{
g.clearRect(Bangle.appRect); g.clearRect(Bangle.appRect);
//LEVEL: //LEVEL:
printMessage(MAXBOARDBGWIDTH, 0, "LEVEL:"); printMessage(MAXBOARDWIDTH, 0, "LEVEL:");
} }
if (partial == 2) { if (partial == 2) {
@ -1299,30 +1258,28 @@ function drawLevelSelect(partial) {
//[LEVEL NR] 2 chars //[LEVEL NR] 2 chars
if (partial == 2) if (partial == 2)
set_bkg_tile_xy(MAXBOARDBGWIDTH + 4, 1, EMPTY); set_bkg_tile_xy(MAXBOARDWIDTH + 4, 1, EMPTY);
printNumber(MAXBOARDBGWIDTH + 4, 1, selectedLevel, 2); printNumber(MAXBOARDWIDTH + 4, 1, selectedLevel, 2);
if(partial > 2) if (partial > 2) {
{
//B:BACK //B:BACK
printMessage(MAXBOARDBGWIDTH, 6, "BTN:"); printMessage(MAXBOARDWIDTH, 6, "BTN:");
printMessage(MAXBOARDBGWIDTH, 7, "BACK"); printMessage(MAXBOARDWIDTH, 7, "BACK");
} }
if (partial > 1) { if (partial > 1) {
//A:PLAY //A:PLAY
printMessage(MAXBOARDBGWIDTH, 4, "TOUCH:"); printMessage(MAXBOARDWIDTH, 4, "TOUCH:");
printMessage(MAXBOARDBGWIDTH, 5, "PLAY"); printMessage(MAXBOARDWIDTH, 5, "PLAY");
} }
//Locked & Unlocked keywoard //Locked & Unlocked keywoard
let tmpUnlocked = levelUnlocked(gameMode, difficulty, selectedLevel - 1); let tmpUnlocked = levelUnlocked(gameMode, difficulty, selectedLevel - 1);
if (!tmpUnlocked) if (!tmpUnlocked)
printMessage(MAXBOARDBGWIDTH, 2, "LOCKED"); printMessage(MAXBOARDWIDTH, 2, "LOCKED");
else else
printMessage(MAXBOARDBGWIDTH, 2, "OPEN "); printMessage(MAXBOARDWIDTH, 2, "OPEN ");
if (partial > 2) { if (partial > 2) {
//Draw arrows for vertical / horizontal movement //Draw arrows for vertical / horizontal movement
@ -1377,7 +1334,6 @@ function levelSelect() {
let tmpUnlocked = levelUnlocked(gameMode, difficulty, selectedLevel - 1); let tmpUnlocked = levelUnlocked(gameMode, difficulty, selectedLevel - 1);
if (btnb) { if (btnb) {
playMenuBackSound(); playMenuBackSound();
gameState = GSINITTITLE; gameState = GSINITTITLE;
@ -1438,7 +1394,6 @@ function levelSelect() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// printing functions // printing functions
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function setCharAt(str, index, chr) { function setCharAt(str, index, chr) {
if (index > str.length - 1) return str; if (index > str.length - 1) return str;
return str.substring(0, index) + chr + str.substring(index + 1); return str.substring(0, index) + chr + str.substring(index + 1);
@ -1450,10 +1405,8 @@ function formatInteger(valinteger) {
const maxCharacters = (maxDigits); const maxCharacters = (maxDigits);
const lastIndex = (maxCharacters - 1); const lastIndex = (maxCharacters - 1);
if (valinteger == 0) { if (valinteger == 0) {
array = setCharAt(array, lastIndex, '0'); array = setCharAt(array, lastIndex, '0');
return { return {
@ -1494,31 +1447,14 @@ function printNumber(ax, ay, aNumber, maxDigits) {
} }
} }
function printDebug(ax, ay, amsg) {
if (DEBUGMODE) {
//rememvber current tiles
let tiles = get_bkg_data();
setBlockTilesAsBackground();
g.clearRect(Bangle.appRect);
printMessage(ax, ay, amsg);
setTimeout(() => {
g.flip();
}, 2500);
//restore the previous tiles
set_bkg_data(tiles);
}
}
//print a message on the title screen on ax,ay, the tileset from titlescreen contains an alphabet //print a message on the title screen on ax,ay, the tileset from titlescreen contains an alphabet
function printMessage(ax, ay, amsg) { function printMessage(ax, ay, amsg) {
let index = 0;
let p = 0;
let aCode = 'A'.charCodeAt(0); let aCode = 'A'.charCodeAt(0);
let zCode = 'Z'.charCodeAt(0); let zCode = 'Z'.charCodeAt(0);
let zeroCode = '0'.charCodeAt(0); let zeroCode = '0'.charCodeAt(0);
let nineCode = '9'.charCodeAt(0); let nineCode = '9'.charCodeAt(0);
while (p < amsg.length) { for (let p = 0; p < amsg.length; p++) {
let fCharCode = amsg.charCodeAt(p++); let fCharCode = amsg.charCodeAt(p);
let tile = 61; let tile = 61;
switch (fCharCode) { switch (fCharCode) {
case -1: case -1:
@ -1589,34 +1525,29 @@ function printMessage(ax, ay, amsg) {
} }
break; break;
} }
set_bkg_tile_xy(ax + index, ay, tile); set_bkg_tile_xy(ax + p, ay, tile);
++index;
} }
} }
//print a message on the CongratsScreen on ax,ay, the tileset from Congrats Screen contains an alphabet in another font //print a message on the CongratsScreen on ax,ay, the tileset from Congrats Screen contains an alphabet in another font
function printCongratsScreen(ax, ay, amsg) { function printCongratsScreen(ax, ay, amsg) {
// based on input form @Pharap // based on input from @Pharap
let index = 0;
let p = 0;
let aCode = 'A'.charCodeAt(0); let aCode = 'A'.charCodeAt(0);
let zCode = 'Z'.charCodeAt(0); let zCode = 'Z'.charCodeAt(0);
while (p < amsg.length) { for (let p = 0; p < amsg.length; p++) {
let fCharCode = amsg.charCodeAt(p++); let fCharCode = amsg.charCodeAt(p);
let tile = 26; let tile = 26;
if ((fCharCode == 0) || (fCharCode == -1)) if ((fCharCode == 0) || (fCharCode == -1))
return; return;
if ((fCharCode >= aCode) && (fCharCode <= zCode)) if ((fCharCode >= aCode) && (fCharCode <= zCode))
tile = fCharCode - aCode; tile = fCharCode - aCode;
set_bkg_tile_xy(ax + index, ay, tile); set_bkg_tile_xy(ax + p, ay, tile);
++index;
} }
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// save state // save state
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function validateSaveState() { function validateSaveState() {
for (let j = 0; j < GMCOUNT; j++) { for (let j = 0; j < GMCOUNT; j++) {
for (let i = 0; i < DIFFCOUNT; i++) { for (let i = 0; i < DIFFCOUNT; i++) {
@ -1727,7 +1658,6 @@ function unlockLevel(mode, diff, level) {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// titlescreen // titlescreen
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function drawMenuSelector(tile) { function drawMenuSelector(tile) {
//set menu tile //set menu tile
switch (titleStep) { switch (titleStep) {
@ -1809,8 +1739,9 @@ function drawMenuItems(clear) {
} }
if (isWidgetsOnSaveState()) { if (isWidgetsOnSaveState()) {
printMessage(3, 7, "WIDGETS ON"); printMessage(3, 7, "WIDGETS ON");
} else } else {
printMessage(3, 7, "WIDGETS OFF"); printMessage(3, 7, "WIDGETS OFF");
}
printMessage(1, 9, "RESTART NEEDED"); printMessage(1, 9, "RESTART NEEDED");
printMessage(2, 10, "FOR WIDGETS"); printMessage(2, 10, "FOR WIDGETS");
break; break;
@ -2097,7 +2028,6 @@ function titleScreen() {
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// game // game
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function drawGame(partial) { function drawGame(partial) {
//background //background
if (!paused && !redrawLevelDoneBit) { if (!paused && !redrawLevelDoneBit) {
@ -2106,40 +2036,40 @@ function drawGame(partial) {
//LEVEL: //LEVEL:
if (partial > 2) { if (partial > 2) {
printMessage(MAXBOARDBGWIDTH, 0, "LEVEL:"); printMessage(MAXBOARDWIDTH, 0, "LEVEL:");
//[LEVEL NR] 2 chars //[LEVEL NR] 2 chars
printNumber(MAXBOARDBGWIDTH + 4, 1, selectedLevel, 2); printNumber(MAXBOARDWIDTH + 4, 1, selectedLevel, 2);
} }
//MOVES: //MOVES:
if (partial > 2) if (partial > 2)
printMessage(MAXBOARDBGWIDTH, 2, "MOVES:"); printMessage(MAXBOARDWIDTH, 2, "MOVES:");
if (partial > 1) if (partial > 1)
printNumber(MAXBOARDBGWIDTH + 1, 3, moves, 5); printNumber(MAXBOARDWIDTH + 1, 3, moves, 5);
//A:XXXXXX (XXXXXX="ROTATE" or XXXXXX="SLIDE " or XXXXXX="ROSLID") //A:XXXXXX (XXXXXX="ROTATE" or XXXXXX="SLIDE " or XXXXXX="ROSLID")
if (partial > 2) { if (partial > 2) {
switch (gameMode) { switch (gameMode) {
case GMROTATE: case GMROTATE:
printMessage(MAXBOARDBGWIDTH, 4, "TOUCH:"); printMessage(MAXBOARDWIDTH, 4, "TOUCH:");
printMessage(MAXBOARDBGWIDTH, 5, "ROTATE"); printMessage(MAXBOARDWIDTH, 5, "ROTATE");
break; break;
case GMSLIDE: case GMSLIDE:
printMessage(MAXBOARDBGWIDTH, 4, "TOUCH:"); printMessage(MAXBOARDWIDTH, 4, "TOUCH:");
printMessage(MAXBOARDBGWIDTH, 5, "SLIDE"); printMessage(MAXBOARDWIDTH, 5, "SLIDE");
break; break;
case GMROTATESLIDE: case GMROTATESLIDE:
printMessage(MAXBOARDBGWIDTH, 4, "TOUCH:"); printMessage(MAXBOARDWIDTH, 4, "TOUCH:");
printMessage(MAXBOARDBGWIDTH, 5, "ROSLID"); printMessage(MAXBOARDWIDTH, 5, "ROSLID");
break; break;
} }
} }
if (partial > 2) { if (partial > 2) {
//B:BACK //B:BACK
printMessage(MAXBOARDBGWIDTH, 6, "BTN:"); printMessage(MAXBOARDWIDTH, 6, "BTN:");
printMessage(MAXBOARDBGWIDTH, 7, "BACK"); printMessage(MAXBOARDWIDTH, 7, "BACK");
} }
if (partial > 2) { if (partial > 2) {
@ -2188,21 +2118,16 @@ function initGame() {
} }
function doPause() { function doPause() {
//drawGame();
//drawCursors();
paused = 1; paused = 1;
wasSoundOn = isSoundOn(); wasSoundOn = isSoundOn();
setSoundOn(0); setSoundOn(0);
hideCursors(); hideCursors();
//g.setColor(0,0,0); printMessage(0, (MAXBOARDHEIGHT >> 1) - 3, "[**************]");
// g.fillRect(SCREENOFFSETX, screenOffsetY + ((MAXBOARDBGHEIGHT >> 1) - 3) * TILESIZE, SCREENOFFSETX + 16* TILESIZE, screenOffsetY + ((MAXBOARDBGHEIGHT >> 1) - 3) * TILESIZE + (6* TILESIZE)); printMessage(0, (MAXBOARDHEIGHT >> 1) - 2, "|PLEASE CONFIRM+");
//g.setColor(1,1,1); printMessage(0, (MAXBOARDHEIGHT >> 1) - 1, "| +");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) - 3, "[**************]"); printMessage(0, (MAXBOARDHEIGHT >> 1) + 0, "| TOUCH PLAY +");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) - 2, "|PLEASE CONFIRM+"); printMessage(0, (MAXBOARDHEIGHT >> 1) + 1, "| BTN TO QUIT +");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) - 1, "| +"); printMessage(0, (MAXBOARDHEIGHT >> 1) + 2, "<##############>");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) + 0, "| TOUCH PLAY +");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) + 1, "| BTN TO QUIT +");
printMessage(0, (MAXBOARDBGHEIGHT >> 1) + 2, "<##############>");
requiresFlip = 1; requiresFlip = 1;
} }
@ -2219,8 +2144,6 @@ function game() {
gameState -= GSINITDIFF; gameState -= GSINITDIFF;
} }
//needRedraw = updateCursorFrame();
if (dragdown) { if (dragdown) {
if (!levelDone && !paused) { if (!levelDone && !paused) {
playGameMoveSound(); playGameMoveSound();
@ -2231,9 +2154,8 @@ function game() {
selectionY += 1; selectionY += 1;
needRedraw = 1; needRedraw = 1;
redrawPartial = 0; redrawPartial = 0;
} else } else {
//set to border on top //set to border on top
{
//clear cursor //clear cursor
drawCursors(true); drawCursors(true);
selectionY = -posAdd; selectionY = -posAdd;
@ -2253,9 +2175,8 @@ function game() {
selectionY -= 1; selectionY -= 1;
needRedraw = 1; needRedraw = 1;
redrawPartial = 0; redrawPartial = 0;
} else } else {
//set to border on bottom //set to border on bottom
{
//clear cursor //clear cursor
drawCursors(true); drawCursors(true);
selectionY = boardHeight - 1 + posAdd; selectionY = boardHeight - 1 + posAdd;
@ -2275,9 +2196,8 @@ function game() {
selectionX += 1; selectionX += 1;
needRedraw = 1; needRedraw = 1;
redrawPartial = 0; redrawPartial = 0;
} else } else {
//set to border on left //set to border on left
{
//clear cursor //clear cursor
drawCursors(true); drawCursors(true);
selectionX = -posAdd; selectionX = -posAdd;
@ -2297,9 +2217,7 @@ function game() {
selectionX -= 1; selectionX -= 1;
needRedraw = 1; needRedraw = 1;
redrawPartial = 0; redrawPartial = 0;
} } else { //set to border on right
//set to border on right
else {
//clear cursor //clear cursor
drawCursors(true); drawCursors(true);
selectionX = boardWidth - 1 + posAdd; selectionX = boardWidth - 1 + posAdd;
@ -2380,14 +2298,11 @@ function game() {
drawGame(2); drawGame(2);
//hide cursor it's only sprite we use //hide cursor it's only sprite we use
hideCursors(); hideCursors();
//g.setColor(0,0,0); printMessage(((16 - 13) >> 1), (MAXBOARDHEIGHT >> 1) - 2, "[************]");
//g.fillRect(SCREENOFFSETX + ((16 - 13) >> 1) * TILESIZE, screenOffsetY + ((MAXBOARDBGHEIGHT >> 1) - 2) * TILESIZE, SCREENOFFSETX + (((16 - 13) >> 1) * TILESIZE) + (14*TILESIZE), screenOffsetY + (((MAXBOARDBGHEIGHT >> 1) - 2) * TILESIZE) +(5*TILESIZE)); printMessage(((16 - 13) >> 1), (MAXBOARDHEIGHT >> 1) - 1, "| LEVEL DONE +");
//g.setColor(1,1,1); printMessage(((16 - 13) >> 1), (MAXBOARDHEIGHT >> 1) - 0, "| TOUCH TO +");
printMessage(((16 - 13) >> 1), (MAXBOARDBGHEIGHT >> 1) - 2, "[************]"); printMessage(((16 - 13) >> 1), (MAXBOARDHEIGHT >> 1) + 1, "| CONTINUE +");
printMessage(((16 - 13) >> 1), (MAXBOARDBGHEIGHT >> 1) - 1, "| LEVEL DONE +"); printMessage(((16 - 13) >> 1), (MAXBOARDHEIGHT >> 1) + 2, "<############>");
printMessage(((16 - 13) >> 1), (MAXBOARDBGHEIGHT >> 1) - 0, "| TOUCH TO +");
printMessage(((16 - 13) >> 1), (MAXBOARDBGHEIGHT >> 1) + 1, "| CONTINUE +");
printMessage(((16 - 13) >> 1), (MAXBOARDBGHEIGHT >> 1) + 2, "<############>");
redrawLevelDoneBit = 1; redrawLevelDoneBit = 1;
} }
} else { } else {
@ -2413,8 +2328,7 @@ function game() {
showCursors(); showCursors();
needRedraw = 1; needRedraw = 1;
redrawPartial = 3; redrawPartial = 3;
} else //Goto some congrats screen } else {
{
gameState = GSINITLEVELSCLEARED; gameState = GSINITLEVELSCLEARED;
} }
} }
@ -2459,12 +2373,10 @@ function game() {
} }
} }
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
// main game start // main game start
// -------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------
function setThemingOn(value) function setThemingOn(value) {
{
//change color palette to theming options for the images //change color palette to theming options for the images
if (value && (g.theme.bg != g.theme.fg)) { if (value && (g.theme.bg != g.theme.fg)) {
SELECTORTILES.palette[1] = g.theme.bg; SELECTORTILES.palette[1] = g.theme.bg;
@ -2532,7 +2444,6 @@ function loop() {
g.setBgColor(0x0000); g.setBgColor(0x0000);
} }
//gamestate handling //gamestate handling
let prevGameState = gameState; let prevGameState = gameState;
@ -2689,7 +2600,6 @@ let memStart;
if (DEBUGMODERAMUSE) if (DEBUGMODERAMUSE)
memStart = process.memory(true); memStart = process.memory(true);
//initialize spritepos arrays //initialize spritepos arrays
for (let i = 0; i < cursorNumTiles; i++) for (let i = 0; i < cursorNumTiles; i++)
spritePos.push(new Int8Array(2)); spritePos.push(new Int8Array(2));