simplify on-click if{}

This commit is contained in:
Gwenhael Le Moine 2011-07-12 18:05:52 +02:00
parent e106f42fc3
commit c4965fca1e

View file

@ -213,44 +213,39 @@ function start_loop( state, elt ) {
$(document).focus( ); $(document).focus( );
$(document).click( $(document).click(
function( e ) { function( e ) {
var bpos = $("#blackboard").offset(); var bpos = $("#blackboard").offset();
var bdim = {}; var bdim = {};
bdim.width = $("#blackboard").width(); var celldim = {};
bdim.height = $("#blackboard").height(); var click = {};
var celldim = {}; var movingpos = get_pos( state, state.moving );
celldim.width = bdim.width / LEVEL_WIDTH; var notmovingpos = get_pos( state, ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE );
celldim.height = bdim.height / LEVEL_HEIGHT; bdim.width = $("#blackboard").width();
var click = {}; bdim.height = $("#blackboard").height();
click.x = Math.floor( ( e.pageX - bpos.left ) / celldim.width ); celldim.width = bdim.width / LEVEL_WIDTH;
click.y = Math.floor( ( e.pageY - bpos.top ) / celldim.height ); celldim.height = bdim.height / LEVEL_HEIGHT;
click.x = e.pageX - bpos.left;
click.y = e.pageY - bpos.top;
if ( ( 0 <= click.x && click.x < bdim.width ) if ( ( 0 <= click.x && click.x < bdim.width )
&& ( 0 <= click.y && click.y < bdim.height ) ) { && ( 0 <= click.y && click.y < bdim.height ) ) {
var ballpos = get_pos( state, cell.BALL ); // coordinates in cell indexes
var cubepos = get_pos( state, cell.CUBE ); click.x = Math.floor( click.x / celldim.width );
click.y = Math.floor( click.y / celldim.height );
if ( click.x == ballpos[0] && click.y == ballpos[1] ) { // We're inside the board
state.moving = cell.BALL; if ( click.x == notmovingpos[0] && click.y == notmovingpos[1] ) {
} else { state.moving = ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE;
if ( click.x == cubepos[0] && click.y == cubepos[1] ) { } else if ( click.x == movingpos[0] ) {
state.moving = cell.CUBE; if ( click.y > movingpos[1] ) {
state = make_a_move( state, direction.DOWN );
} else if ( click.y < movingpos[1] ) {
state = make_a_move( state, direction.UP );
}
} else if ( click.y == movingpos[1] ) {
if ( click.x > movingpos[0] ) {
state = make_a_move( state, direction.RIGHT );
} else { } else {
var movingpos = ( state.moving == cell.BALL ) ? ballpos : cubepos; state = make_a_move( state, direction.LEFT );
if ( click.x == movingpos[0] ) {
if ( click.y > movingpos[1] ) {
state = make_a_move( state, direction.DOWN );
} else {
state = make_a_move( state, direction.UP );
}
} else {
if ( click.y == movingpos[1] ) {
if ( click.x > movingpos[0] ) {
state = make_a_move( state, direction.RIGHT );
} else {
state = make_a_move( state, direction.LEFT );
}
}
}
} }
} }