mirror of
https://github.com/gwenhael-le-moine/c-urs_-toil-s.git
synced 2024-12-26 09:58:48 +01:00
encapsulate everything in initialize_a_star()
This commit is contained in:
parent
fb35f9a22c
commit
42baf07f03
1 changed files with 292 additions and 292 deletions
|
@ -1,21 +1,22 @@
|
|||
var LEVEL_HEIGHT = 9;
|
||||
var LEVEL_WIDTH = 16;
|
||||
function initialize_a_star( dom_container ) {
|
||||
var LEVEL_HEIGHT = 9;
|
||||
var LEVEL_WIDTH = 16;
|
||||
|
||||
var cell= {
|
||||
var cell= {
|
||||
WALL : '#',
|
||||
BALL : '@',
|
||||
CUBE : 'H',
|
||||
VOID : ' ',
|
||||
GIFT : 'x'
|
||||
};
|
||||
var direction = {
|
||||
};
|
||||
var direction = {
|
||||
UP : 'u',
|
||||
DOWN : 'd',
|
||||
LEFT : 'l',
|
||||
RIGHT : 'r'
|
||||
};
|
||||
};
|
||||
|
||||
var levels = [ "#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################",
|
||||
var levels = [ "#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################",
|
||||
" # # # # # ### x @# #x #x x # # x x # # # x # # #H# x # # # # #xx## # # # # ",
|
||||
"################# x#@## ## ##H## #x x ## x x## x## #x x x# x### ##x #x x x####x ##x #################",
|
||||
"################# #H## # ###x#x x#x#x#x#x## # #x x# # # ####x#x#x x#x#x#x## # ## # #@ #################",
|
||||
|
@ -41,7 +42,7 @@ var levels = [ "#################@## x#H## x #### ##x #
|
|||
"################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ",
|
||||
"################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# " ];
|
||||
|
||||
function count_gifts( state ) {
|
||||
function count_gifts( state ) {
|
||||
var n = 0;
|
||||
for ( var i = LEVEL_HEIGHT * LEVEL_WIDTH ; i-- ; ) {
|
||||
if ( state.board[ i ] == cell.GIFT ) {
|
||||
|
@ -49,28 +50,28 @@ function count_gifts( state ) {
|
|||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
function get_pos( state, actor ) {
|
||||
function get_pos( state, actor ) {
|
||||
var p = state.board.indexOf( actor, state.board );
|
||||
var pos = { };
|
||||
pos[ 1 ] = Math.floor( p / LEVEL_WIDTH ); /* y */
|
||||
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH ); /* x */
|
||||
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
function get_cell( state, x, y ) {
|
||||
function get_cell( state, x, y ) {
|
||||
return state.board[ x + ( y * LEVEL_WIDTH ) ];
|
||||
}
|
||||
}
|
||||
|
||||
function set_cell( state, x, y, value ) {
|
||||
function set_cell( state, x, y, value ) {
|
||||
var p = x + ( y * LEVEL_WIDTH );
|
||||
state.board = [ state.board.substring( 0, p ), value, state.board.substring( p+1, state.board.length ) ].join( '' );
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
function switch_actor( state ) {
|
||||
function switch_actor( state ) {
|
||||
state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL;
|
||||
var ball_pos = get_pos( state, cell.BALL );
|
||||
var cube_pos = get_pos( state, cell.CUBE );
|
||||
|
@ -90,13 +91,13 @@ function switch_actor( state ) {
|
|||
state.board_infos.cell_dimensions.width,
|
||||
state.board_infos.cell_dimensions.height );
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
function won_or_not( state ) {
|
||||
function won_or_not( state ) {
|
||||
return count_gifts( state ) === 0;
|
||||
}
|
||||
}
|
||||
|
||||
function full_display_on_canvas( state ) {
|
||||
function full_display_on_canvas( state ) {
|
||||
var ctx= state.board_infos.canvas.getContext( '2d' ); //$() returns a jquery object, [0] to get the canvas itself
|
||||
for ( var i=0 ; i < LEVEL_HEIGHT ; i++ ) {
|
||||
for ( var j=0 ; j < LEVEL_WIDTH ; j++ ) {
|
||||
|
@ -116,40 +117,40 @@ function full_display_on_canvas( state ) {
|
|||
state.board_infos.cell_dimensions.height );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function update_infos( state ) {
|
||||
function update_infos( state ) {
|
||||
var infos = "<h1>Star5</h1><br />";
|
||||
infos += "Level <em>" + (state.level+1) + "</em> of <em>" + levels.length + "</em><br />";
|
||||
infos += "<em>" + count_gifts( state ) + "</em> gifts left<br />";
|
||||
infos += "<em>" + state.distance_travelled + "</em> meters travelled";
|
||||
|
||||
$( state.dom_container + " .gstar #infos" ).html( infos );
|
||||
}
|
||||
}
|
||||
|
||||
function format_help( ) {
|
||||
function format_help( ) {
|
||||
var help = "<em>←↑→↓</em> to move around<br />";
|
||||
help += "<em>Space</em> to switch actor<br />";
|
||||
help += "<em>r</em> to reload<br />";
|
||||
help += "<em>n</em> to pass to the next level<br />";
|
||||
help += "<em>p</em> to go back to the previous level<br />";
|
||||
return help;
|
||||
}
|
||||
}
|
||||
|
||||
function display_level( state ) {
|
||||
function display_level( state ) {
|
||||
update_infos( state );
|
||||
full_display_on_canvas( state, state.dom_container + " #starboard" );
|
||||
}
|
||||
}
|
||||
|
||||
function load_level( state, levelset, index ) {
|
||||
function load_level( state, levelset, index ) {
|
||||
state.level = index;
|
||||
state.board = levelset[ state.level ];
|
||||
state.distance_travelled = 0;
|
||||
state.moving = cell.BALL;
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
function make_a_move( state, where ) {
|
||||
function make_a_move( state, where ) {
|
||||
var motion = [ 0, 0 ];
|
||||
var item_coord = get_pos( state, state.moving );
|
||||
|
||||
|
@ -207,9 +208,9 @@ function make_a_move( state, where ) {
|
|||
}
|
||||
update_infos( state );
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
function start_loop( state ) {
|
||||
function start_loop( state ) {
|
||||
display_level( state );
|
||||
|
||||
$(document).focus( );
|
||||
|
@ -303,9 +304,9 @@ function start_loop( state ) {
|
|||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function load_sprites( theme ) {
|
||||
function load_sprites( theme ) {
|
||||
var sprites = { };
|
||||
sprites.ball = new Image();
|
||||
sprites.ball.src = "themes/" + theme + "/tex_ball.png";
|
||||
|
@ -323,9 +324,8 @@ function load_sprites( theme ) {
|
|||
sprites.gift.src = "themes/" + theme + "/tex_gift.png";
|
||||
|
||||
return sprites;
|
||||
}
|
||||
}
|
||||
|
||||
function initialize_a_star( dom_container ) {
|
||||
var state = {
|
||||
moving : cell.BALL,
|
||||
distance_travelled : 0,
|
||||
|
|
Loading…
Reference in a new issue