var initialize_a_star = function( dom_container, level_index, theme ) { // kinda enums var cell = { WALL: '#', BALL: '@', CUBE: 'H', EMPTY: ' ', GIFT: 'x' }; var direction = { UP: 38, DOWN: 40, LEFT: 37, RIGHT: 39 }; var assets = { 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## # ## # #@ #################", " ############## #@ # # # ## #x # x x # ### # # ##x #x# #### # x # ##x# # # # #H## # x# #x# ############## ", " ############ # x #x x# # x # ## # x ##@ x ### x # ### x # ##H # x ##x #################", "################# # ## ### #x ##x# #x #x # # # # # # ### ## ## # #x# #x# # ## @#x H #x#################", "############### # x## ### #x ## x ## x## # #x ### ## #x# ### # x#x ##xHx# x #@# ### # ###############", " # ########### #x#x # @##x x# x # # # x## x# ## #x #xHx x## x## # #x#x # # # ############ ", " ########### #### x ## H ###x x# x## x #x #x # # # x # x##x#x # x# #@# #x ### ### # # # # ######### # #", "################# # @## #xx xx #### x ## x##x #x#xx ##### ## ## ##x x# x H x###x### # ## ## ########### ", "## ## #### #@#####x ### x### xx x ## ## ##x #x# ## # x ###x ## ## ## ## #H# ## x ## x #################", " ############## # @# x ### # #x x## ## x # ## x #x## # x ### x x #x##H # x # # # ############## ", "#################x#x x#x## x#@ ## ## H x ## x# ## x ## x# # ##x#x x#x#################", " ###### ####### # x# x ## # x # # x ## @# #xx #x # # # # x H# ##x # #x # # x # #x x# ############## ", "################## H#x x x##x @x#x #### ### x #### x#x# ##xx x#x ### x ####x ###x# # #################", "################# x# #@ ## # x#xx#x # ## #x##x# x ## x# x# ## x#x x# ## # # ##x# # ## x #x H #################", "################# x x H# ## #x#x #x ## #x# #x ## x # x#x ## #x# # x# ## x#x # x # ##x#@ # # #################", "#################x ## ##x## # # #x ## x# x## x ## # #x ## # x# ## ## x# ##x #H## 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 @ H x xx################# ", "#################x# #x# #x # ## # ##x # #x x ### #x x #### x # ###x ## #@#H x ################# ", " ############## # # #x# #x # ## x # ### # x #x ## #x # xx x ###x # ## x ## #@#H x # ############## ", "################# # ### ##x x ##x### #x x# #### xx x# ## ## #x x # ## ## ## @#H###xx################# ", "################# # ## x ##x x ## #x x ## ## x ## #x ## #x x# x ## ##x #@ H ################# " ] }; ////// FUNCTIONS ////// var count_gifts = function( ) { var n = 0; for ( var i = level_infos.height * level_infos.width ; i-- ; ) { if ( state.board[ i ] == cell.GIFT ) { n++; } } return n; }; var get_pos = function( actor ) { var p = state.board.indexOf( actor, state.board ); var pos = [ ]; pos[ 1 ] = Math.floor( p / level_infos.width ); /* y */ pos[ 0 ] = p - ( pos[ 1 ] * level_infos.width ); /* x */ return pos; }; var get_cell = function( x, y ) { return state.board[ x + ( y * level_infos.width ) ]; }; var set_cell = function( x, y, value ) { var p = x + ( y * level_infos.width ); state.board = [ state.board.substring( 0, p ), value, state.board.substring( p+1, state.board.length ) ].join( '' ); }; var switch_actor = function( ) { state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL; }; var won_or_not = function( ) { return count_gifts( ) === 0; }; var load_level = function( index ) { return( { moving: cell.BALL, distance_travelled: 0, level: index, board: assets.levels[ index ], it_s_over: false } ); }; var make_a_move = function( where ) { var motion = [ 0, 0 ]; var item_coord = get_pos( state.moving ); /* Setup the motion vector according to direction.*/ switch( where ) { case direction.UP: motion[ 1 ]--; break; case direction.DOWN: motion[ 1 ]++; break; case direction.LEFT: motion[ 0 ]--; break; case direction.RIGHT: motion[ 0 ]++; break; default: break; } var path = [ ]; path[ 0 ] = [ ]; path[ 0 ][ 0 ] = item_coord[ 0 ]; path[ 0 ][ 1 ] = item_coord[ 1 ]; /* Calculating arrival coordinates */ while ( /* Hairy conditions ahead */ /* target cell is within level's boundaries */ ( ( item_coord[ 0 ] + motion[ 0 ] >= 0 ) && ( item_coord[ 0 ] + motion[ 0 ] < level_infos.width ) ) && ( ( item_coord[ 1 ] + motion[ 1 ] >= 0 ) && ( item_coord[ 1 ] + motion[ 1 ] < level_infos.height ) ) && /* and target cell is empty */ ( get_cell( item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.EMPTY ) /* or, the ball will eat gifts so we can move it on one */ || ( state.moving == cell.BALL && ( get_cell( item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.GIFT ) ) ) { set_cell( item_coord[ 0 ], item_coord[ 1 ], cell.EMPTY ); /* empty the origin cell */ item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */ item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */ var push_pos = path.length; path[ push_pos ] = [ ]; path[ push_pos ][ 0 ] = item_coord[ 0 ]; path[ push_pos ][ 1 ] = item_coord[ 1 ]; set_cell( item_coord[ 0 ], item_coord[ 1 ], state.moving ); /* move actor into target cell */ } return path; }; ////// HTML/Canvas version specific functions var load_sprites = function( theme ) { assets.sprites = { }; assets.sprites.ball = new Image(); assets.sprites.ball.src = "themes/" + theme + "/tex_ball.png"; assets.sprites.ball_selected = new Image(); assets.sprites.ball_selected.src = "themes/" + theme + "/tex_ball_selected.png"; assets.sprites.cube = new Image(); assets.sprites.cube.src = "themes/" + theme + "/tex_cube.png"; assets.sprites.cube_selected = new Image(); assets.sprites.cube_selected.src = "themes/" + theme + "/tex_cube_selected.png"; assets.sprites.wall = new Image(); assets.sprites.wall.src = "themes/" + theme + "/tex_wall.png"; assets.sprites.empty = new Image(); assets.sprites.empty.src = "themes/" + theme + "/tex_empty.png"; assets.sprites.gift = new Image(); assets.sprites.gift.src = "themes/" + theme + "/tex_gift.png"; }; var draw_cell = function( sprite, x, y ) { DOM_infos.canvas.context.drawImage( sprite, x * level_infos.cell.width, y * level_infos.cell.height, level_infos.cell.width, level_infos.cell.height ); }; var display_switch_actor = function( ) { var ball_pos = get_pos( cell.BALL ); var cube_pos = get_pos( cell.CUBE ); // redraw ball draw_cell( ( state.moving == cell.BALL ) ? assets.sprites.ball_selected : assets.sprites.ball, ball_pos[ 0 ], ball_pos[ 1 ] ); // redraw cube draw_cell( ( state.moving == cell.CUBE ) ? assets.sprites.cube_selected : assets.sprites.cube, cube_pos[ 0 ], cube_pos[ 1 ] ); }; var full_display_on_canvas = function( ) { for ( var i=0 ; i < level_infos.height ; i++ ) { for ( var j=0 ; j < level_infos.width ; j++ ) { var c = get_cell( j, i ); var sprite; switch( c ) { case "@": sprite = ( state.moving == cell.BALL ) ? assets.sprites.ball_selected : assets.sprites.ball; break; case "H": sprite = ( state.moving == cell.CUBE ) ? assets.sprites.cube_selected : assets.sprites.cube; break; case "#": sprite = assets.sprites.wall; break; case "x": sprite = assets.sprites.gift; break; case " ": sprite = assets.sprites.empty; break; default: break; } draw_cell( sprite, j, i ); } } }; var update_infos = function( ) { var infos = "