Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by emati

Hi Guys, i have wrote function which checks is the given entity in view.

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! :)

8 years ago by tyler

nice! this should return a boolean not a string. then you can write slightly cleaner more readable code

if(ig.isInView(EntityMyEntity)){
   //blabla
}

8 years ago by emati

Yeah thats right, don't know why i put there string ^^
Page 1 of 1
« first « previous next › last »