🎨 starts cleaning code before adding labyrinth generation

This commit is contained in:
2024-04-25 17:52:58 +02:00
parent 69b6cafb28
commit f7754632de
4 changed files with 74 additions and 89 deletions

View File

@@ -1,3 +1,8 @@
const KO = 0,
OK = 1,
WALL = 2,
LOSE = 3,
WIN = 4;
let clicked = false,
sounds = $.map(["ko.wav", "ok.wav", "undefined.wav", "gameover.wav", "welldone.wav"], function(sound) {
return 'assets/sounds/'+sound;
@@ -5,7 +10,7 @@ let clicked = false,
boardbg = '#FFFFFF',
score = 0,
time = 0,
compte, nbsnd, oksnd, kosnd, nosnd, failsnd, level, left, top, bottom, right, currentPosition, toleft, toright, totop, tobot;
compte, nbsnd, oksnd, kosnd, nosnd, failsnd, level, p_left, p_top, p_bottom, p_right, currentPosition, toright, totop, tobot;
//console.log('level :'+level);
$(document).ready(function(){
@@ -34,15 +39,15 @@ $(document).ready(function(){
$('#ok').live('click', function(e){
e.preventDefault();
playSound(1);
playSound(OK);
});
$('#ko').live('click', function(e){
e.preventDefault();
playSound(0);
playSound(KO);
});
$('#unknown').live('click', function(e){
e.preventDefault();
playSound(2);
playSound(WALL);
});
$('#retry').live('click', function(e){
@@ -59,16 +64,16 @@ $(document).ready(function(){
time = 0;
nbsnd = sounds.length;
//console.log('loading '+nbsnd+' sounds');
oksnd = new Audio(sounds[1]);
kosnd = new Audio(sounds[0]);
nosnd = new Audio(sounds[2]);
failsnd = new Audio(sounds[3]);
endsnd = new Audio(sounds[4]);
oksnd = new Audio(sounds[OK]);
kosnd = new Audio(sounds[KO]);
nosnd = new Audio(sounds[WALL]);
failsnd = new Audio(sounds[LOSE]);
endsnd = new Audio(sounds[WIN]);
left = 2;
right = 2;
top = 2;
bottom = 2;
p_left = 2;
p_right = 2;
p_top = 2;
p_bottom = 2;
$('body').html('').append('<nav><a href="editor.html" title="Create your own labyrinth" class="left pill button">Create your own labyrinth</a><a href="list" title="Custom laby list" class="right pill button">Custom laby list</a></nav>').append('<div id="board"></div>');
$('#board').append('<a href="#" id="start" title="click to start" class="primary positive button">Start?</a><br/><a href="#" id="instruction" title="Instructions to play" class="left button">Instruction</a> <a href="#" id="instructionfr" title="Instructions pour jouer" class="right button">Instructions en français</a>');
@@ -93,11 +98,11 @@ $(document).ready(function(){
function playSound(sound){
playColor(sound);
switch(sound){
case 0: kosnd.play(); break;
case 1: oksnd.play(); break;
case 2: nosnd.play(); break;
case 3: failsnd.play(); break;
case 4: endsnd.play(); break;
case KO: kosnd.play(); break;
case OK: oksnd.play(); break;
case WALL: nosnd.play(); break;
case LOSE: failsnd.play(); break;
case WIN: endsnd.play(); break;
default: /*failsnd.play();*/ break;
}
}
@@ -128,43 +133,18 @@ $(document).ready(function(){
clearTimeout(totop);
clearTimeout(toright);
clearTimeout(tobot);
left = level[currentPosition[1]][currentPosition[0]-1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]-1];
shikitop = level[currentPosition[1]-1] == undefined ? 2 : level[currentPosition[1]-1][currentPosition[0]];
right = level[currentPosition[1]][currentPosition[0]+1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]+1];
bottom = level[currentPosition[1]+1] == undefined ? 2 : level[currentPosition[1]+1][currentPosition[0]];
playSound(left); // left
totop = setTimeout(playSound, 800, shikitop); // top
toright = setTimeout(playSound, 1600, right); // right
tobot = setTimeout(playSound, 2400, bottom); // bottom
p_left = level[currentPosition[1]][currentPosition[0]-1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]-1];
p_top = level[currentPosition[1]-1] == undefined ? 2 : level[currentPosition[1]-1][currentPosition[0]];
p_right = level[currentPosition[1]][currentPosition[0]+1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]+1];
p_bottom = level[currentPosition[1]+1] == undefined ? 2 : level[currentPosition[1]+1][currentPosition[0]];
playSound(p_left); // left
totop = setTimeout(playSound, 800, p_top); // top
toright = setTimeout(playSound, 1600, p_right); // right
tobot = setTimeout(playSound, 2400, p_bottom); // bottom
}
function check1caseOnLeft(){
return currentPosition[0] > 0;
}
function check1caseOnTop(){
return currentPosition[1] > 0;
}
function check1caseOnRight(){
return currentPosition[0] + 1 < level[0].length;
}
function check1caseOnBottom(){
return currentPosition[1] + 1 <= level.length;
}
function check2caseOnLeft(){
return !(currentPosition[0] <= 1 || level[currentPosition[1]][currentPosition[0] - 1] == 1);
}
function check2caseOnTop(){
return !(currentPosition[1] <= 1 || level[currentPosition[1] - 1][currentPosition[0]] == 1);
}
function check2caseOnRight(){
}
function check2caseOnBottom(){
}
function generateLevel(){
// generer largeur / hauteur
// generate largeur / hauteur
var min = 5;
var max = 15;
var largeur = Math.floor(Math.random() * (max - min + 1)) + min;
@@ -237,9 +217,8 @@ $(document).ready(function(){
function gameover(){
clearInterval(compte);
clicked = false;
//console.log('game over');
playSound(3);
var finalTime = parseFloat(time/10);
playSound(LOSE);
let finalTime = Math.round(time / 10);
$('#board').html('You lost... <a href="#" id="retry" title="Click to try again">Do you want to try again?</a> You played : '+finalTime+'seconds');
}
@@ -256,7 +235,7 @@ $(document).ready(function(){
function winningGame(){
clicked = false;
//console.log('You won !');
playSound(4);
playSound(WIN);
$('#board').html('<a href="#" id="next" title="Load next level">Load next level</a>');
}
@@ -307,7 +286,7 @@ $(document).ready(function(){
}
break; // bas
case 32: // barre espace
playKey();
playKey();
break;
default: break;
}