mirror of
https://github.com/gwenhael-le-moine/c-urs_-toil-s.git
synced 2024-12-26 09:58:48 +01:00
factorize event handling
This commit is contained in:
parent
257ff69520
commit
7d2bf35c5a
1 changed files with 83 additions and 92 deletions
|
@ -223,14 +223,12 @@ function initialize_a_star( dom_container ) {
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
state = set_cell( item_coord[ 0 ], item_coord[ 1 ], cell.VOID ); /* void the origin cell */
|
state = set_cell( item_coord[ 0 ], item_coord[ 1 ], cell.VOID ); /* void the origin cell */
|
||||||
// voiding origin cell on canvas
|
|
||||||
draw_cell( assets.sprites.void, item_coord[ 0 ], item_coord[ 1 ] );
|
draw_cell( assets.sprites.void, item_coord[ 0 ], item_coord[ 1 ] );
|
||||||
|
|
||||||
item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */
|
item_coord[ 0 ] += motion[ 0 ]; /* move coordinate */
|
||||||
item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */
|
item_coord[ 1 ] += motion[ 1 ]; /* to those of target cells */
|
||||||
|
|
||||||
state = set_cell( item_coord[ 0 ], item_coord[ 1 ], state.moving ); /* move actor into target cell */
|
state = set_cell( item_coord[ 0 ], item_coord[ 1 ], state.moving ); /* move actor into target cell */
|
||||||
// drawing target cell on canvas
|
|
||||||
draw_cell( ( state.moving == cell.BALL ) ? assets.sprites.ball_selected : assets.sprites.cube_selected, item_coord[ 0 ], item_coord[ 1 ] );
|
draw_cell( ( state.moving == cell.BALL ) ? assets.sprites.ball_selected : assets.sprites.cube_selected, item_coord[ 0 ], item_coord[ 1 ] );
|
||||||
|
|
||||||
state.distance_travelled++; /* increment distance_travelled */
|
state.distance_travelled++; /* increment distance_travelled */
|
||||||
|
@ -239,100 +237,93 @@ function initialize_a_star( dom_container ) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function event_handler( e ) {
|
||||||
|
if ( e.type === "click" ) {
|
||||||
|
var movingpos = get_pos( state.moving );
|
||||||
|
var notmovingpos = get_pos( ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE );
|
||||||
|
var click = { };
|
||||||
|
click.x = e.pageX - DOM_infos.canvas.offset.left;
|
||||||
|
click.y = e.pageY - DOM_infos.canvas.offset.top;
|
||||||
|
|
||||||
|
if ( ( 0 <= click.x && click.x < DOM_infos.canvas.width )
|
||||||
|
&& ( 0 <= click.y && click.y < DOM_infos.canvas.height ) ) {
|
||||||
|
// coordinates in cell indexes
|
||||||
|
click.x = Math.floor( click.x / level_infos.cell.width );
|
||||||
|
click.y = Math.floor( click.y / level_infos.cell.height );
|
||||||
|
|
||||||
|
// 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] ) {
|
||||||
|
state = make_a_move( direction.DOWN );
|
||||||
|
} else if ( click.y < movingpos[1] ) {
|
||||||
|
state = make_a_move( direction.UP );
|
||||||
|
}
|
||||||
|
} else if ( click.y == movingpos[1] ) {
|
||||||
|
if ( click.x > movingpos[0] ) {
|
||||||
|
state = make_a_move( direction.RIGHT );
|
||||||
|
} else {
|
||||||
|
state = make_a_move( direction.LEFT );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if ( e.type === "keydown" ) {
|
||||||
|
switch( e.keyCode ) {
|
||||||
|
case 38: // UP
|
||||||
|
state = make_a_move( direction.UP );
|
||||||
|
break;
|
||||||
|
case 40: // DOWN
|
||||||
|
state = make_a_move( direction.DOWN );
|
||||||
|
break;
|
||||||
|
case 37: // LEFT
|
||||||
|
state = make_a_move( direction.LEFT );
|
||||||
|
break;
|
||||||
|
case 39: // RIGHT
|
||||||
|
state = make_a_move( direction.RIGHT );
|
||||||
|
break;
|
||||||
|
case 32: // SPACE
|
||||||
|
state = switch_actor( );
|
||||||
|
break;
|
||||||
|
case 78: // n
|
||||||
|
if ( state.level < assets.levels.length - 1 ) {
|
||||||
|
state = load_level( state.level + 1 );
|
||||||
|
display_level( );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 80: // p
|
||||||
|
if ( state.level > 0 ) {
|
||||||
|
state = load_level( state.level - 1 );
|
||||||
|
display_level( );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 82: // r
|
||||||
|
state = load_level( state.level );
|
||||||
|
display_level( );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( won_or_not( ) ) {
|
||||||
|
if ( state.level < assets.levels.length - 1 ) {
|
||||||
|
state = load_level( state.level + 1 );
|
||||||
|
display_level( );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
alert( "You won!" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function start_loop( ) {
|
function start_loop( ) {
|
||||||
display_level( );
|
display_level( );
|
||||||
|
|
||||||
$(document).focus( );
|
$(document).focus( );
|
||||||
$(document).click(
|
$(document).click( event_handler );
|
||||||
function( e ) {
|
$(document).keydown( event_handler );
|
||||||
var movingpos = get_pos( state.moving );
|
|
||||||
var notmovingpos = get_pos( ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE );
|
|
||||||
var click = { };
|
|
||||||
click.x = e.pageX - DOM_infos.canvas.offset.left;
|
|
||||||
click.y = e.pageY - DOM_infos.canvas.offset.top;
|
|
||||||
|
|
||||||
if ( ( 0 <= click.x && click.x < DOM_infos.canvas.width )
|
|
||||||
&& ( 0 <= click.y && click.y < DOM_infos.canvas.height ) ) {
|
|
||||||
// coordinates in cell indexes
|
|
||||||
click.x = Math.floor( click.x / level_infos.cell.width );
|
|
||||||
click.y = Math.floor( click.y / level_infos.cell.height );
|
|
||||||
|
|
||||||
// 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] ) {
|
|
||||||
state = make_a_move( direction.DOWN );
|
|
||||||
} else if ( click.y < movingpos[1] ) {
|
|
||||||
state = make_a_move( direction.UP );
|
|
||||||
}
|
|
||||||
} else if ( click.y == movingpos[1] ) {
|
|
||||||
if ( click.x > movingpos[0] ) {
|
|
||||||
state = make_a_move( direction.RIGHT );
|
|
||||||
} else {
|
|
||||||
state = make_a_move( direction.LEFT );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( won_or_not( ) ) {
|
|
||||||
if ( state.level < assets.levels.length - 1 ) {
|
|
||||||
state = load_level( state.level + 1 );
|
|
||||||
display_level( );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
alert( "You won!" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
$(document).keydown(
|
|
||||||
function( e ) {
|
|
||||||
switch( e.keyCode ) {
|
|
||||||
case 38: // UP
|
|
||||||
state = make_a_move( direction.UP );
|
|
||||||
break;
|
|
||||||
case 40: // DOWN
|
|
||||||
state = make_a_move( direction.DOWN );
|
|
||||||
break;
|
|
||||||
case 37: // LEFT
|
|
||||||
state = make_a_move( direction.LEFT );
|
|
||||||
break;
|
|
||||||
case 39: // RIGHT
|
|
||||||
state = make_a_move( direction.RIGHT );
|
|
||||||
break;
|
|
||||||
case 32: // SPACE
|
|
||||||
state = switch_actor( );
|
|
||||||
break;
|
|
||||||
case 78: // n
|
|
||||||
if ( state.level < assets.levels.length - 1 ) {
|
|
||||||
state = load_level( state.level + 1 );
|
|
||||||
display_level( );
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 80: // p
|
|
||||||
if ( state.level > 0 ) {
|
|
||||||
state = load_level( state.level - 1 );
|
|
||||||
display_level( );
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 82: // r
|
|
||||||
state = load_level( state.level );
|
|
||||||
display_level( );
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( won_or_not( ) ) {
|
|
||||||
if ( state.level < assets.levels.length - 1 ) {
|
|
||||||
state = load_level( state.level + 1 );
|
|
||||||
display_level( );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
alert( "You won!" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////// MAIN (so to speak) //////
|
////// MAIN (so to speak) //////
|
||||||
|
|
Loading…
Reference in a new issue