1 decade ago by emati
Hi Guys, i have wrote function which checks is the given entity in view.
Here is the code:
Usage:
Legend:
Returns string 'in' or 'out'
Enjoy! :)
Here is the code:
isInView: function( entity, offset ) {
if(typeof offset == 'undefined') offset = {x: 0, y: 0};
// Count visible coords of view.
var visibleCoords = {
xLeft: ig.game.screen.x - offset.x,
yTop: ig.game.screen.y - offset.y
};
visibleCoords.xRight = visibleCoords.xLeft + ig.system.width;
visibleCoords.yBottom = visibleCoords.yTop + ig.system.height;
if( (entity.pos.x.toInt() > visibleCoords.xRight.toInt()) ||
(entity.pos.x.toInt() < visibleCoords.xLeft.toInt()) ||
(entity.pos.y.toInt() < visibleCoords.yTop) ||
(entity.pos.y.toInt() > visibleCoords.yBottom)
) {
return false;
} else {
return true;
}
}
Usage:
isInView(entity, offset);
Legend:
var *entity* - entity which you want to check, var *offset* - by this you can make it more sensitive.
Returns string 'in' or 'out'
Enjoy! :)
