Use the distance formula:
Therefore, to get distance
PHP Code:
function dist(x1, y1, x2, y2) {
return (((x1 - x2) ^ 2) + ((y1 - y2) ^ 2)) ^ 0.5;
}
So you can just do...
PHP Code:
if (this.dist(player.x, player.y, this.x, this.y) < 5) {
player.chat = "I'm within 5 tiles!";
} else {
player.chat = "I'm not within 5 tiles!";
}
If you'd prefer to check based on tiles (e.g. check in a square around the NPC as opposed to a circle with distance), you can do:
PHP Code:
if (player.x in |this.x - 2.5, this.x + 2.5| && player.y in |this.y - 2.5, this.y + 2.5|) {
player.chat = "I'm in the 5-tile box around the NPC.";
} else {
player.chat = "I'm not in the 5-tile box around the NPC.";
}
I would typically choose the first, but it obviously depends on what you're trying to accomplish.
You'll probably want to adjust the player's x/y and the object's x/y to reach the center of the object.