🎨 starts cleaning code before adding labyrinth generation

This commit is contained in:
Clement Desmidt 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, let clicked = false,
sounds = $.map(["ko.wav", "ok.wav", "undefined.wav", "gameover.wav", "welldone.wav"], function(sound) { sounds = $.map(["ko.wav", "ok.wav", "undefined.wav", "gameover.wav", "welldone.wav"], function(sound) {
return 'assets/sounds/'+sound; return 'assets/sounds/'+sound;
@ -5,7 +10,7 @@ let clicked = false,
boardbg = '#FFFFFF', boardbg = '#FFFFFF',
score = 0, score = 0,
time = 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); //console.log('level :'+level);
$(document).ready(function(){ $(document).ready(function(){
@ -34,15 +39,15 @@ $(document).ready(function(){
$('#ok').live('click', function(e){ $('#ok').live('click', function(e){
e.preventDefault(); e.preventDefault();
playSound(1); playSound(OK);
}); });
$('#ko').live('click', function(e){ $('#ko').live('click', function(e){
e.preventDefault(); e.preventDefault();
playSound(0); playSound(KO);
}); });
$('#unknown').live('click', function(e){ $('#unknown').live('click', function(e){
e.preventDefault(); e.preventDefault();
playSound(2); playSound(WALL);
}); });
$('#retry').live('click', function(e){ $('#retry').live('click', function(e){
@ -59,16 +64,16 @@ $(document).ready(function(){
time = 0; time = 0;
nbsnd = sounds.length; nbsnd = sounds.length;
//console.log('loading '+nbsnd+' sounds'); //console.log('loading '+nbsnd+' sounds');
oksnd = new Audio(sounds[1]); oksnd = new Audio(sounds[OK]);
kosnd = new Audio(sounds[0]); kosnd = new Audio(sounds[KO]);
nosnd = new Audio(sounds[2]); nosnd = new Audio(sounds[WALL]);
failsnd = new Audio(sounds[3]); failsnd = new Audio(sounds[LOSE]);
endsnd = new Audio(sounds[4]); endsnd = new Audio(sounds[WIN]);
left = 2; p_left = 2;
right = 2; p_right = 2;
top = 2; p_top = 2;
bottom = 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>'); $('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>'); $('#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){ function playSound(sound){
playColor(sound); playColor(sound);
switch(sound){ switch(sound){
case 0: kosnd.play(); break; case KO: kosnd.play(); break;
case 1: oksnd.play(); break; case OK: oksnd.play(); break;
case 2: nosnd.play(); break; case WALL: nosnd.play(); break;
case 3: failsnd.play(); break; case LOSE: failsnd.play(); break;
case 4: endsnd.play(); break; case WIN: endsnd.play(); break;
default: /*failsnd.play();*/ break; default: /*failsnd.play();*/ break;
} }
} }
@ -128,43 +133,18 @@ $(document).ready(function(){
clearTimeout(totop); clearTimeout(totop);
clearTimeout(toright); clearTimeout(toright);
clearTimeout(tobot); clearTimeout(tobot);
left = level[currentPosition[1]][currentPosition[0]-1] == undefined ? 2 : level[currentPosition[1]][currentPosition[0]-1]; p_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]]; p_top = 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]; p_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]]; p_bottom = level[currentPosition[1]+1] == undefined ? 2 : level[currentPosition[1]+1][currentPosition[0]];
playSound(left); // left playSound(p_left); // left
totop = setTimeout(playSound, 800, shikitop); // top totop = setTimeout(playSound, 800, p_top); // top
toright = setTimeout(playSound, 1600, right); // right toright = setTimeout(playSound, 1600, p_right); // right
tobot = setTimeout(playSound, 2400, bottom); // bottom 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(){ function generateLevel(){
// generer largeur / hauteur // generate largeur / hauteur
var min = 5; var min = 5;
var max = 15; var max = 15;
var largeur = Math.floor(Math.random() * (max - min + 1)) + min; var largeur = Math.floor(Math.random() * (max - min + 1)) + min;
@ -237,9 +217,8 @@ $(document).ready(function(){
function gameover(){ function gameover(){
clearInterval(compte); clearInterval(compte);
clicked = false; clicked = false;
//console.log('game over'); playSound(LOSE);
playSound(3); let finalTime = Math.round(time / 10);
var finalTime = parseFloat(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'); $('#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(){ function winningGame(){
clicked = false; clicked = false;
//console.log('You won !'); //console.log('You won !');
playSound(4); playSound(WIN);
$('#board').html('<a href="#" id="next" title="Load next level">Load next level</a>'); $('#board').html('<a href="#" id="next" title="Load next level">Load next level</a>');
} }

View File

@ -1,5 +1,5 @@
<?php <?php
$file = isset($_GET['level']) ? $_GET['level'] : null; $file = $_GET['level'] ?? null;
$custom = 'false'; $custom = 'false';
$niveau = '1'; $niveau = '1';
if ($file !== null && file_exists(__DIR__ .'/custom/'.$file.'.js')){ if ($file !== null && file_exists(__DIR__ .'/custom/'.$file.'.js')){
@ -8,8 +8,9 @@ if ($file !== null && file_exists(__DIR__ .'/custom/'.$file.'.js')){
} }
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en">
<head> <head>
<title>Blind Laby</title>
<link rel="stylesheet" type="text/css" href="assets/css/index.css" /> <link rel="stylesheet" type="text/css" href="assets/css/index.css" />
<link rel="stylesheet" type="text/css" href="assets/css/css3buttons.css" /> <link rel="stylesheet" type="text/css" href="assets/css/css3buttons.css" />
<style> <style>

View File

@ -3,7 +3,7 @@ session_start();
$currentFile = !empty($_SESSION['file']) ? $_SESSION['file'] : null; $currentFile = !empty($_SESSION['file']) ? $_SESSION['file'] : null;
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="en">
<head> <head>
<link rel="stylesheet" type="text/css" href="index.css" /> <link rel="stylesheet" type="text/css" href="index.css" />
<style> <style>
@ -11,16 +11,17 @@ $currentFile = !empty($_SESSION['file']) ? $_SESSION['file'] : null;
#start{display:block;} #start{display:block;}
.current{background-color:yellow;} .current{background-color:yellow;}
</style> </style>
<title>Blind Laby - Create your own</title>
</head> </head>
<body> <body>
<div id="board"> <div id="board">
<?php <?php
$folder =new DirectoryIterator('../custom/'); $folder = new DirectoryIterator('../custom/');
foreach($folder as $file){ foreach($folder as $file){
if(!$file->isDir()){ if (!$file->isDir() && $file->getExtension() === 'js') {
echo '<a href="http://labs.shikiryu.com/experimental-games/2/?level='.substr($file->getFileName(), 0, -3).'" title="laby '.substr($file->getFileName(), 0, -3).'"'; echo '<a href="http://labs.shikiryu.com/experimental-games/2/?level='.substr($file->getFileName(), 0, -3).'" title="laby '.substr($file->getFileName(), 0, -3).'"';
if($currentFile !== null && $currentFile == $file->getFileName()) echo ' class="current"'; if($currentFile !== null && $currentFile === $file->getFileName()) echo ' class="current"';
echo '>laby '.substr($file->getFileName(), 0, -3).'</a><br/>'; echo '>laby '.substr($file->getFileName(), 0, -3).'</a><br/>';
} }
} }

View File

@ -2,24 +2,28 @@
session_start(); session_start();
$startingPoint = $_POST['startingPoint']; $startingPoint = $_POST['startingPoint'];
$endingPoint = $_POST['endingPoint']; $endingPoint = $_POST['endingPoint'];
$largeur = (int)$_POST['largeur']; $largeur = (int)$_POST['largeur'];
$hauteur = (int)$_POST['hauteur']; $hauteur = (int)$_POST['hauteur'];
$cases = array(); $cases = [];
foreach($_POST as $n=>$v){ foreach ($_POST as $n => $v) {
if(!in_array($n, array('startingPoint','endingPoint','largeur','hauteur'))){ if (!in_array($n, ['startingPoint', 'endingPoint', 'largeur', 'hauteur'])) {
$coord = getCoordForCase($n); $coord = getCoordForCase($n);
$cases[$coord[0]][$coord[1]] = 1; $cases[$coord[0]][$coord[1]] = 1;
} }
} }
function getCoordForPt($t){ function getCoordForPt($t)
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { {
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) {
return '[' . $m[2] . ',' . $m[1] . ']'; return '[' . $m[2] . ',' . $m[1] . ']';
} }
return null;
return null;
} }
function getCoordForCase($t){
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) { function getCoordForCase($t)
{
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) {
return [$m[1], $m[2]]; return [$m[1], $m[2]];
} }
@ -27,24 +31,24 @@ function getCoordForCase($t){
} }
$level = '['; $level = '[';
for($i=0; $i<$hauteur; $i++){ for ($i = 0; $i < $hauteur; $i++) {
$level .= '['; $level .= '[';
for($j=0; $j<$largeur; $j++){ for ($j = 0; $j < $largeur; $j++) {
if(!empty($cases[$i][$j])) { if (!empty($cases[$i][$j])) {
$level .= '1,'; $level .= '1,';
} else { } else {
$level .= '0,'; $level .= '0,';
} }
} }
$level = substr($level, 0, -1).'],'; $level = substr($level, 0, -1) . '],';
} }
$level = substr($level, 0, -1).'];'; $level = substr($level, 0, -1) . '];';
$file = ''; $file = '';
$file .= 'currentPosition = '.getCoordForPt($startingPoint).';'; $file .= 'currentPosition = ' . getCoordForPt($startingPoint) . ';';
$file .= 'finalPosition ='.getCoordForPt($endingPoint).';'; $file .= 'finalPosition =' . getCoordForPt($endingPoint) . ';';
$file .= 'level = '.$level; $file .= 'level = ' . $level;
$tod = gettimeofday(); $tod = gettimeofday();
$finalFilename = $tod['sec'].mt_rand(0,50).'.js'; $finalFilename = $tod['sec'] . mt_rand(0, 50) . '.js';
file_put_contents(__DIR__ .'/custom/'.$finalFilename, $file); file_put_contents(__DIR__ . '/custom/' . $finalFilename, $file);
$_SESSION['file'] = $finalFilename; $_SESSION['file'] = $finalFilename;
header('Location: list/'); header('Location: list/');