Last Updated: February 25, 2016
·
1.286K
· datamosh

Get nearest entity

findNearestEntity: function() {
    var nearestDistance = Infinity;
    var nearestEntity = null;
    for( var i = 0; i < ig.game.entities.length; i++ ) {
        var ent = ig.game.entities[i];
        var distance = this.distanceTo( ent );
        if( distance < nearestDistance && ent != this ) {
            nearestDistance = distance;
            nearestEntity = ent;
        }
    }
    return nearestEntity;
}

1 Response
Add your response

Sin loop:

var that = this;
return Math.min.apply(ig.game.entities.map(function () {
    return this.distanceTo(that);
}));
over 1 year ago ·