refactor the code to make it a bit more readable

This commit is contained in:
Gwenhael Le Moine 2011-07-12 18:16:42 +02:00
parent ce217fc216
commit cf14f8fa8c

View file

@ -208,29 +208,31 @@ function make_a_move( state, where ) {
function start_loop( state, elt ) { function start_loop( state, elt ) {
options.dom_place = elt; options.dom_place = elt;
display_level( state, options.dom_place ); display_level( state, options.dom_place );
$(document).focus( ); $(document).focus( );
$(document).click( $(document).click(
function( e ) { function( e ) {
var bpos = $("#blackboard").offset(); var board_infos = {};
var bdim = {}; board_infos.position = $("#blackboard").offset();
var celldim = {}; board_infos.dimensions = {};
var click = {}; board_infos.dimensions.width = $("#blackboard").width();
board_infos.dimensions.height = $("#blackboard").height();
board_infos.cell_dimensions = {};
board_infos.cell_dimensions.width = board_infos.dimensions.width / LEVEL_WIDTH;
board_infos.cell_dimensions.height = board_infos.dimensions.height / LEVEL_HEIGHT;
var movingpos = get_pos( state, state.moving ); var movingpos = get_pos( state, state.moving );
var notmovingpos = get_pos( state, ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE ); var notmovingpos = get_pos( state, ( state.moving != cell.BALL ) ? cell.BALL : cell.CUBE );
bdim.width = $("#blackboard").width(); var click = {};
bdim.height = $("#blackboard").height(); click.x = e.pageX - board_infos.position.left;
celldim.width = bdim.width / LEVEL_WIDTH; click.y = e.pageY - board_infos.position.top;
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 < board_infos.dimensions.width )
&& ( 0 <= click.y && click.y < bdim.height ) ) { && ( 0 <= click.y && click.y < board_infos.dimensions.height ) ) {
// coordinates in cell indexes // coordinates in cell indexes
click.x = Math.floor( click.x / celldim.width ); click.x = Math.floor( click.x / board_infos.cell_dimensions.width );
click.y = Math.floor( click.y / celldim.height ); click.y = Math.floor( click.y / board_infos.cell_dimensions.height );
// We're inside the board // We're inside the board
if ( click.x == notmovingpos[0] && click.y == notmovingpos[1] ) { if ( click.x == notmovingpos[0] && click.y == notmovingpos[1] ) {