2011-07-05 09:54:13 +02:00
|
|
|
var LEVEL_HEIGHT = 9;
|
|
|
|
var LEVEL_WIDTH = 16;
|
2011-07-04 16:11:42 +02:00
|
|
|
|
2011-07-05 09:54:13 +02:00
|
|
|
var cell= {
|
2011-07-05 13:46:17 +02:00
|
|
|
WALL : '#',
|
|
|
|
BALL : '@',
|
|
|
|
CUBE : 'H',
|
|
|
|
VOID : ' ',
|
|
|
|
GIFT : 'x'
|
2011-07-04 21:10:44 +02:00
|
|
|
};
|
2011-07-05 09:54:13 +02:00
|
|
|
var direction = {
|
2011-07-05 13:46:17 +02:00
|
|
|
UP : 'u',
|
|
|
|
DOWN : 'd',
|
|
|
|
LEFT : 'l',
|
|
|
|
RIGHT : 'r'
|
2011-07-04 21:10:44 +02:00
|
|
|
};
|
2011-07-13 22:42:48 +02:00
|
|
|
|
2011-07-05 09:54:13 +02:00
|
|
|
var state = {
|
2011-07-05 13:46:17 +02:00
|
|
|
moving : cell.BALL,
|
2011-07-06 23:42:40 +02:00
|
|
|
distance_travelled : 0,
|
2011-07-05 13:46:17 +02:00
|
|
|
level : 0,
|
2011-07-13 22:36:20 +02:00
|
|
|
board : "",
|
|
|
|
board_infos : { }
|
2011-07-04 21:10:44 +02:00
|
|
|
};
|
2011-07-04 16:11:42 +02:00
|
|
|
|
2011-07-05 09:54:13 +02:00
|
|
|
var levels = [ "#################@## x#H## x #### ##x ## ## x #### x x x ## x x## x ## ##x x#################",
|
2011-07-05 13:45:13 +02:00
|
|
|
" # # # # # ### 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 ################# " ];
|
2011-07-04 16:11:42 +02:00
|
|
|
|
|
|
|
function count_gifts( state ) {
|
2011-07-05 09:54:13 +02:00
|
|
|
var n = 0;
|
2011-07-05 11:37:15 +02:00
|
|
|
for ( var i = LEVEL_HEIGHT * LEVEL_WIDTH ; i-- ; ) {
|
|
|
|
if ( state.board[ i ] == cell.GIFT ) {
|
2011-07-04 16:11:42 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function get_pos( state, actor ) {
|
2011-07-05 09:54:13 +02:00
|
|
|
var p = state.board.indexOf( actor, state.board );
|
2011-07-13 22:19:35 +02:00
|
|
|
var pos = { };
|
2011-07-04 16:11:42 +02:00
|
|
|
pos[ 1 ] = Math.floor( p / LEVEL_WIDTH ); /* y */
|
|
|
|
pos[ 0 ] = p - ( pos[ 1 ] * LEVEL_WIDTH ); /* x */
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function get_cell( state, x, y ) {
|
2011-07-05 09:18:27 +02:00
|
|
|
return state.board[ x + ( y * LEVEL_WIDTH ) ];
|
2011-07-04 16:11:42 +02:00
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function set_cell( state, x, y, value ) {
|
2011-07-05 09:54:13 +02:00
|
|
|
var p = x + ( y * LEVEL_WIDTH );
|
2011-07-05 09:45:14 +02:00
|
|
|
state.board = [ state.board.substring( 0, p ), value, state.board.substring( p+1, state.board.length ) ].join( '' );
|
2011-07-04 16:40:15 +02:00
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:40:15 +02:00
|
|
|
function switch_actor( state ) {
|
|
|
|
state.moving = ( state.moving == cell.BALL ) ? cell.CUBE : cell.BALL;
|
2011-07-13 22:17:50 +02:00
|
|
|
var ball_pos = get_pos( state, cell.BALL );
|
|
|
|
var cube_pos = get_pos( state, cell.CUBE );
|
|
|
|
// we're going to update the canvas right here and there so we need it
|
2011-07-13 22:36:20 +02:00
|
|
|
var ctx= state.board_infos.canvas.getContext( '2d' ); //$() returns a jquery object, [0] to get the canvas itself
|
2011-07-13 22:17:50 +02:00
|
|
|
|
|
|
|
// redraw ball
|
2011-07-13 22:42:48 +02:00
|
|
|
ctx.drawImage( ( state.moving == cell.BALL ) ? state.board_infos.sprites.ball_selected : state.board_infos.sprites.ball,
|
2011-07-13 22:36:20 +02:00
|
|
|
ball_pos[ 0 ] * state.board_infos.cell_dimensions.width,
|
|
|
|
ball_pos[ 1 ] * state.board_infos.cell_dimensions.height,
|
|
|
|
state.board_infos.cell_dimensions.width,
|
|
|
|
state.board_infos.cell_dimensions.height );
|
2011-07-13 22:17:50 +02:00
|
|
|
// redraw cube
|
2011-07-13 22:42:48 +02:00
|
|
|
ctx.drawImage( ( state.moving == cell.CUBE ) ? state.board_infos.sprites.cube_selected : state.board_infos.sprites.cube,
|
2011-07-13 22:36:20 +02:00
|
|
|
cube_pos[ 0 ] * state.board_infos.cell_dimensions.width,
|
|
|
|
cube_pos[ 1 ] * state.board_infos.cell_dimensions.height,
|
|
|
|
state.board_infos.cell_dimensions.width,
|
|
|
|
state.board_infos.cell_dimensions.height );
|
2011-07-04 16:11:42 +02:00
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-04 16:11:42 +02:00
|
|
|
function won_or_not( state ) {
|
2011-07-05 11:12:30 +02:00
|
|
|
return count_gifts( state ) === 0;
|
2011-07-04 16:11:42 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
function full_display_on_canvas( state, canvas_elt ) {
|
2011-07-13 20:53:52 +02:00
|
|
|
var ctx= $( canvas_elt )[ 0 ].getContext( '2d' ); //$() returns a jquery object, [0] to get the canvas itself
|
2011-07-13 18:17:38 +02:00
|
|
|
for ( var i=0 ; i < LEVEL_HEIGHT ; i++ ) {
|
|
|
|
for ( var j=0 ; j < LEVEL_WIDTH ; j++ ) {
|
2011-07-13 18:34:03 +02:00
|
|
|
var c = get_cell( state, j, i );
|
2011-07-13 21:37:57 +02:00
|
|
|
var sprite;
|
2011-07-13 18:34:03 +02:00
|
|
|
switch( c ) {
|
2011-07-13 22:42:48 +02:00
|
|
|
case "@": sprite = ( state.moving == cell.BALL ) ? state.board_infos.sprites.ball_selected : state.board_infos.sprites.ball; break;
|
|
|
|
case "H": sprite = ( state.moving == cell.CUBE ) ? state.board_infos.sprites.cube_selected : state.board_infos.sprites.cube; break;
|
|
|
|
case "#": sprite = state.board_infos.sprites.wall; break;
|
|
|
|
case "x": sprite = state.board_infos.sprites.gift; break;
|
|
|
|
case " ": sprite = state.board_infos.sprites.void; break;
|
2011-07-13 18:34:03 +02:00
|
|
|
}
|
2011-07-13 20:51:27 +02:00
|
|
|
ctx.drawImage( sprite,
|
2011-07-13 22:36:20 +02:00
|
|
|
j * state.board_infos.cell_dimensions.width,
|
|
|
|
i * state.board_infos.cell_dimensions.height,
|
|
|
|
state.board_infos.cell_dimensions.width,
|
|
|
|
state.board_infos.cell_dimensions.height );
|
2011-07-13 18:17:38 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 18:10:16 +02:00
|
|
|
}
|
2011-07-13 20:48:37 +02:00
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
function update_infos( state, elt ) {
|
2011-07-06 23:28:36 +02:00
|
|
|
var infos = "<h1>Star5</h1><br />";
|
2011-07-13 16:31:47 +02:00
|
|
|
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";
|
2011-07-13 22:17:50 +02:00
|
|
|
|
|
|
|
$( elt + " .gstar #infos" ).html( infos );
|
2011-07-06 23:28:36 +02:00
|
|
|
}
|
2011-07-13 22:17:50 +02:00
|
|
|
|
2011-07-07 09:16:32 +02:00
|
|
|
function format_help( ) {
|
2011-07-07 14:31:09 +02:00
|
|
|
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 />";
|
2011-07-07 09:16:32 +02:00
|
|
|
return help;
|
|
|
|
}
|
2011-07-06 23:28:36 +02:00
|
|
|
|
2011-07-07 09:09:07 +02:00
|
|
|
function display_level( state, elt ) {
|
2011-07-13 22:17:50 +02:00
|
|
|
update_infos( state, elt );
|
|
|
|
full_display_on_canvas( state, elt + " #starboard" );
|
2011-07-07 09:09:07 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
function load_level( levelset, index ) {
|
|
|
|
state.level = index;
|
2011-07-04 16:40:15 +02:00
|
|
|
state.board = levelset[ state.level ];
|
2011-07-06 13:46:01 +02:00
|
|
|
state.distance_travelled = 0;
|
2011-07-04 16:11:42 +02:00
|
|
|
state.moving = cell.BALL;
|
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:51:03 +02:00
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
function make_a_move( state, elt, where ) {
|
2011-07-05 09:54:13 +02:00
|
|
|
var motion = [ 0, 0 ];
|
|
|
|
var item_coord = get_pos( state, state.moving );
|
2011-07-04 16:11:42 +02:00
|
|
|
|
2011-07-04 17:19:39 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
// we're going to update the canvas right here and there so we need it
|
2011-07-13 22:36:20 +02:00
|
|
|
var ctx= state.board_infos.canvas.getContext( '2d' ); //$() returns a jquery object, [0] to get the canvas itself
|
2011-07-13 22:17:50 +02:00
|
|
|
|
2011-07-04 17:19:39 +02:00
|
|
|
/* 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_WIDTH ) ) &&
|
|
|
|
( ( item_coord[ 1 ] + motion[ 1 ] >= 0 ) && ( item_coord[ 1 ] + motion[ 1 ] < LEVEL_HEIGHT ) ) &&
|
|
|
|
/* and target cell is empty */
|
|
|
|
( get_cell( state, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.VOID )
|
|
|
|
/* or, the ball will eat gifts so we can move it on one */
|
|
|
|
|| ( state.moving == cell.BALL && ( get_cell( state, item_coord[ 0 ] + motion[ 0 ], item_coord[ 1 ] + motion[ 1 ] ) == cell.GIFT ) )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
state = set_cell( state, item_coord[ 0 ], item_coord[ 1 ], cell.VOID ); /* void the origin cell */
|
2011-07-13 22:17:50 +02:00
|
|
|
// voiding origin cell on canvas
|
2011-07-13 22:42:48 +02:00
|
|
|
ctx.drawImage( state.board_infos.sprites.void,
|
2011-07-13 22:36:20 +02:00
|
|
|
item_coord[ 0 ] * state.board_infos.cell_dimensions.width,
|
|
|
|
item_coord[ 1 ] * state.board_infos.cell_dimensions.height,
|
|
|
|
state.board_infos.cell_dimensions.width,
|
|
|
|
state.board_infos.cell_dimensions.height );
|
2011-07-13 22:17:50 +02:00
|
|
|
|
2011-07-04 17:19:39 +02:00
|
|
|
item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */
|
|
|
|
item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */
|
|
|
|
|
|
|
|
state = set_cell( state, item_coord[ 0 ], item_coord[ 1 ], state.moving ); /* move actor into target cell */
|
2011-07-13 22:17:50 +02:00
|
|
|
// drawing target cell on canvas
|
2011-07-13 22:42:48 +02:00
|
|
|
ctx.drawImage( ( state.moving == cell.BALL ) ? state.board_infos.sprites.ball_selected : state.board_infos.sprites.cube_selected,
|
2011-07-13 22:36:20 +02:00
|
|
|
item_coord[ 0 ] * state.board_infos.cell_dimensions.width,
|
|
|
|
item_coord[ 1 ] * state.board_infos.cell_dimensions.height,
|
|
|
|
state.board_infos.cell_dimensions.width,
|
|
|
|
state.board_infos.cell_dimensions.height );
|
2011-07-04 17:19:39 +02:00
|
|
|
|
2011-07-06 13:46:01 +02:00
|
|
|
state.distance_travelled++; /* increment distance_travelled */
|
|
|
|
}
|
2011-07-13 22:17:50 +02:00
|
|
|
update_infos( state, elt );
|
2011-07-04 16:11:42 +02:00
|
|
|
return state;
|
|
|
|
}
|
2011-07-04 16:40:15 +02:00
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
function start_loop( state, elt ) {
|
2011-07-13 20:27:10 +02:00
|
|
|
display_level( state, elt );
|
2011-07-04 16:40:15 +02:00
|
|
|
|
2011-07-12 16:46:04 +02:00
|
|
|
$(document).focus( );
|
2011-07-12 17:31:09 +02:00
|
|
|
$(document).click(
|
|
|
|
function( e ) {
|
2011-07-12 18:17:13 +02:00
|
|
|
var movingpos = get_pos( state, state.moving );
|
|
|
|
var notmovingpos = get_pos( state, ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE );
|
2011-07-13 22:19:35 +02:00
|
|
|
var click = { };
|
2011-07-13 22:36:20 +02:00
|
|
|
click.x = e.pageX - state.board_infos.offset.left;
|
|
|
|
click.y = e.pageY - state.board_infos.offset.top;
|
2011-07-12 18:16:42 +02:00
|
|
|
|
2011-07-13 22:36:20 +02:00
|
|
|
if ( ( 0 <= click.x && click.x < state.board_infos.dimensions.width )
|
|
|
|
&& ( 0 <= click.y && click.y < state.board_infos.dimensions.height ) ) {
|
2011-07-12 18:05:52 +02:00
|
|
|
// coordinates in cell indexes
|
2011-07-13 22:36:20 +02:00
|
|
|
click.x = Math.floor( click.x / state.board_infos.cell_dimensions.width );
|
|
|
|
click.y = Math.floor( click.y / state.board_infos.cell_dimensions.height );
|
2011-07-12 18:05:52 +02:00
|
|
|
|
|
|
|
// We're inside the board
|
|
|
|
if ( click.x == notmovingpos[0] && click.y == notmovingpos[1] ) {
|
|
|
|
state.moving = ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE;
|
|
|
|
} else if ( click.x == movingpos[0] ) {
|
|
|
|
if ( click.y > movingpos[1] ) {
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.DOWN );
|
2011-07-12 18:05:52 +02:00
|
|
|
} else if ( click.y < movingpos[1] ) {
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.UP );
|
2011-07-12 18:05:52 +02:00
|
|
|
}
|
|
|
|
} else if ( click.y == movingpos[1] ) {
|
|
|
|
if ( click.x > movingpos[0] ) {
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.RIGHT );
|
2011-07-12 17:31:09 +02:00
|
|
|
} else {
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.LEFT );
|
2011-07-12 17:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 19:05:21 +02:00
|
|
|
if ( won_or_not( state ) ) {
|
|
|
|
if ( state.level < levels.length - 1 ) {
|
|
|
|
state = load_level( levels, state.level + 1 );
|
2011-07-13 22:17:50 +02:00
|
|
|
display_level( state, elt );
|
2011-07-12 19:05:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert( "You won!" );
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 17:31:09 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$(document).keydown(
|
|
|
|
function( e ) {
|
|
|
|
switch( e.keyCode ) {
|
|
|
|
case 38: // UP
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.UP );
|
2011-07-12 17:31:09 +02:00
|
|
|
break;
|
|
|
|
case 40: // DOWN
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.DOWN );
|
2011-07-12 17:31:09 +02:00
|
|
|
break;
|
|
|
|
case 37: // LEFT
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.LEFT );
|
2011-07-12 17:31:09 +02:00
|
|
|
break;
|
|
|
|
case 39: // RIGHT
|
2011-07-13 22:17:50 +02:00
|
|
|
state = make_a_move( state, elt, direction.RIGHT );
|
2011-07-12 17:31:09 +02:00
|
|
|
break;
|
|
|
|
case 32: // SPACE
|
|
|
|
state = switch_actor( state );
|
|
|
|
break;
|
|
|
|
case 78: // n
|
|
|
|
if ( state.level < levels.length - 1 ) {
|
|
|
|
state = load_level( levels, state.level + 1 );
|
2011-07-13 22:17:50 +02:00
|
|
|
display_level( state, elt );
|
2011-07-12 17:31:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 80: // p
|
|
|
|
if ( state.level > 0 ) {
|
|
|
|
state = load_level( levels, state.level - 1 );
|
2011-07-13 22:17:50 +02:00
|
|
|
display_level( state, elt );
|
2011-07-12 17:31:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 82: // r
|
|
|
|
state = load_level( levels, state.level );
|
2011-07-13 22:17:50 +02:00
|
|
|
display_level( state, elt );
|
2011-07-12 17:31:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2011-07-04 17:23:22 +02:00
|
|
|
}
|
2011-07-12 17:31:09 +02:00
|
|
|
|
|
|
|
if ( won_or_not( state ) ) {
|
|
|
|
if ( state.level < levels.length - 1 ) {
|
|
|
|
state = load_level( levels, state.level + 1 );
|
2011-07-13 22:17:50 +02:00
|
|
|
display_level( state, elt );
|
2011-07-12 17:31:09 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert( "You won!" );
|
|
|
|
}
|
2011-07-04 17:23:22 +02:00
|
|
|
}
|
2011-07-12 17:31:09 +02:00
|
|
|
});
|
2011-07-04 16:40:15 +02:00
|
|
|
}
|
2011-07-13 16:45:02 +02:00
|
|
|
|
2011-07-13 22:42:48 +02:00
|
|
|
function load_sprites( theme ) {
|
|
|
|
var sprites = { };
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.ball = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.ball.src = "themes/" + theme + "/tex_ball.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.ball_selected = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.ball_selected.src = "themes/" + theme + "/tex_ball_selected.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.cube = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.cube.src = "themes/" + theme + "/tex_cube.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.cube_selected = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.cube_selected.src = "themes/" + theme + "/tex_cube_selected.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.wall = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.wall.src = "themes/" + theme + "/tex_wall.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.void = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.void.src = "themes/" + theme + "/tex_empty.png";
|
2011-07-13 20:27:10 +02:00
|
|
|
sprites.gift = new Image();
|
2011-07-13 22:42:48 +02:00
|
|
|
sprites.gift.src = "themes/" + theme + "/tex_gift.png";
|
|
|
|
|
|
|
|
return sprites;
|
|
|
|
}
|
2011-07-13 20:27:10 +02:00
|
|
|
|
2011-07-13 22:42:48 +02:00
|
|
|
function initialize_a_star( elt ) {
|
2011-07-13 16:49:47 +02:00
|
|
|
var starhtml = '<div class="gstar">';
|
|
|
|
starhtml += '<aside id="help">' + format_help( state ) + '</aside>';
|
2011-07-13 18:34:03 +02:00
|
|
|
starhtml += '<canvas id="starboard" width="320" height="180"></canvas>';
|
2011-07-13 16:49:47 +02:00
|
|
|
starhtml += '<aside id="infos"></aside>';
|
2011-07-13 18:34:03 +02:00
|
|
|
starhtml += '</div>';
|
2011-07-13 18:10:16 +02:00
|
|
|
|
2011-07-13 20:18:39 +02:00
|
|
|
$( elt ).html( starhtml );
|
|
|
|
|
2011-07-13 22:42:48 +02:00
|
|
|
state.board_infos.sprites = load_sprites( "HP48" );
|
|
|
|
|
2011-07-13 22:36:20 +02:00
|
|
|
state.board_infos.canvas = $( elt + " #starboard" )[ 0 ];
|
|
|
|
state.board_infos.offset = $( elt + " #starboard" ).offset();
|
|
|
|
state.board_infos.dimensions = { };
|
|
|
|
state.board_infos.dimensions.width = $( elt + " #starboard" ).width();
|
|
|
|
state.board_infos.dimensions.height = $( elt + " #starboard" ).height();
|
|
|
|
state.board_infos.cell_dimensions = { };
|
|
|
|
state.board_infos.cell_dimensions.width = state.board_infos.dimensions.width / LEVEL_WIDTH;
|
|
|
|
state.board_infos.cell_dimensions.height = state.board_infos.dimensions.height / LEVEL_HEIGHT;
|
2011-07-13 18:10:16 +02:00
|
|
|
|
2011-07-13 22:36:20 +02:00
|
|
|
state = load_level( levels, 0 );
|
2011-07-13 18:38:28 +02:00
|
|
|
|
2011-07-13 22:17:50 +02:00
|
|
|
start_loop( state, elt );
|
|
|
|
|
|
|
|
// kinda ugly workaround around a bug causing the canvas
|
|
|
|
// not to refresh the first time (before any event)
|
2011-07-13 22:21:45 +02:00
|
|
|
setTimeout( function(){ display_level( state, elt ); }, 100 ); // 1/10 second
|
2011-07-13 16:45:02 +02:00
|
|
|
}
|