mirror of
https://github.com/gwenhael-le-moine/c-urs_-toil-s.git
synced 2025-01-14 08:01:19 +01:00
format_level using regexp replace (a bit over-the-top maybe)
This commit is contained in:
parent
59108f4491
commit
c0d82707cc
1 changed files with 28 additions and 7 deletions
|
@ -96,15 +96,36 @@ function won_or_not( state ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function format_level( state ) {
|
function format_level( state ) {
|
||||||
var dl = "";
|
function Replacer( conversionObject ) {
|
||||||
for each ( c in state.board ) {
|
|
||||||
var classes = [ "starcell ", css_classes[ c ] ].join( '' );
|
var regexpStr = '';
|
||||||
if ( state.moving == c) {
|
for ( var k in conversionObject ) {
|
||||||
classes = [ classes, " selected " ].join( '' );
|
regexpStr += ( regexpStr.length ? '|' : '' ) + k;
|
||||||
}
|
}
|
||||||
dl = [ dl, "<span class=\"", classes, "\">", c, "</span>"].join( '' );
|
var regexpr = new RegExp( regexpStr, 'mig' ); // g: global, m:multi-line i: ignore case
|
||||||
|
return function(s) {
|
||||||
|
return s.replace( regexpr, function(str, p1, p2, offset, s) {
|
||||||
|
var a = conversionObject[ str ];
|
||||||
|
return a == undefined ? str : a;
|
||||||
|
} );
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return dl;
|
|
||||||
|
var substitutions = {
|
||||||
|
'#': '<span class="starcell wall">#</span>',
|
||||||
|
'x': '<span class="starcell gift">x</span>',
|
||||||
|
' ': '<span class="starcell void"> </span>'
|
||||||
|
};
|
||||||
|
if ( state.moving == cell.BALL ) {
|
||||||
|
substitutions[ '@' ] = '<span class="starcell ball selected">@</span>';
|
||||||
|
substitutions[ 'H' ] = '<span class="starcell cube">H</span>';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
substitutions[ '@' ] = '<span class="starcell ball">@</span>';
|
||||||
|
substitutions[ 'H' ] = '<span class="starcell cube selected">H</span>';
|
||||||
|
}
|
||||||
|
var myReplacer = Replacer( substitutions );
|
||||||
|
return myReplacer( state.board );
|
||||||
}
|
}
|
||||||
|
|
||||||
function load_level( levelset, nb ) {
|
function load_level( levelset, nb ) {
|
||||||
|
|
Loading…
Reference in a new issue