This prototype demonstrates a free player movement in a tilemap environment with automatic adjust even for small lodes. Note that the "lineOfSight Algorithm" checks if the 'enemy' can see the player.
Click on the movie and control player with the cursor keys.
Here is the source code of the algorithm:
// FAST LINE OF SIGHT ALGORITHM BY ANDRE MICHELLE FOR FLASH
//
// http://www.andre-michelle.com
//
// map format is: map = [[1,0,1],[1,1,1],[1,0,1]];
// example - 2d Array
//
// size is the width / height of a tile in px
// tested with 8 px
var round = Math.round;
var floor = Math.floor;
var abs = Math.abs;
var line_of_sight = function ( x0 , y0 , x1 , y1 , map , size ) {
var lty , tmp;
var dx = x1 - x0;
var dy = y1 - y0;
var dm = dy / dx;
var acute = abs( dm ) > 1;
if ( acute ) {
tmp = x0;
x0 = y0;
y0 = tmp;
tmp = dx;
dx = dy;
dy = tmp;
dm = dy / dx;
};
if ( dx > 0 ) {
for ( var x = size - x0 % size ; x < dx ; x += size ) {
var tx = x0 + x;
var ty = y0 + x * dm;
if ( tx % size == 0 && ty % size == 0 ) { // exact diagonal
tx = round ( tx / size );
if ( dy > 0 ) { ty = round ( ty / size ) }
else { ty = floor ( ty / size ) - 1 };
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true;
} else {
if ( map[tx][ty] == 1 ) return true;
}
} else {
tx = round ( tx / size );
ty = floor ( ty / size );
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true;
} else {
if ( map[tx][ty] == 1 ) return true;
}
if ( ty != lty ) {
tx = round ( tx ) - 1;
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true;
} else {
if ( map[tx][ty] == 1 ) return true;
}
}
}
lty = ty;
}
} else {
for ( var x = x0 % size ; x < -dx ; x += size ) {
var tx = x0 - x - size;
var ty = y0 - x * dm;
if ( tx % size == 0 && ty % size == 0 ) { // exact diagonal
tx = round ( tx / size );
if ( dy > 0 ) { ty = round ( ty / size ) }
else { ty = floor ( ty / size ) - 1 };
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true;
} else {
if ( map[tx][ty] == 1 ) return true;
}
} else {
tx = round ( tx / size );
ty = floor ( ty / size );
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true; )
} else {
if ( map[tx][ty] == 1 ) return true; )
}
if ( ty != lty ) {
tx = floor ( tx ) + 1;
if ( !acute ) {
if ( map[ty][tx] == 1 ) return true;
} else {
if ( map[tx][ty] == 1 ) return true;
}
}
}
lty = ty;
}
}
return false;
}
Name:
André Michelle
Location:
Germany
Age:
31
Flash experience:
Just dealing 5 years now with flash and can't leave my fingers off.
Job:
Jointfounder of Extrajetzt, a german flashangency from Berlin,specialized on flashgames.