Page 1 of 1

[Browser MMORPG] isInRange function

Posted: Thu May 05, 2011 8:10 am
by toby
Description:
This function can be used to tell if one div is in range of another div... both horizontally and vertically!
Took 3 days to figure out, please don't be harsh lmao.

Usage:
isInRange("div232","div235",4);
This would return true.
isInRange("div230","div235",4);
This would return false.

Code:
(paste into netmap.js)
function isInRange(tag1,tag2,sqs) {

/*
If you go up one row in the map, the div tag is += 30 automatically
Likewise, if you go down, the div tag is -= 30, therefore,
the offset is 30
*/

var offset = 30;

//Getting the number on its own; changes 'div213' to '213'
var tag_1 = parseInt(tag1.replace("div", ""));
var tag_2 = parseInt(tag2.replace("div", ""));
var totalOffset = offset * sqs; //The total offset (furthest away)
var currentX = tag_1 - totalOffset; //Furthest north directly above tag1 (default)

//How many loops to go through, e.g sqs=2 .. 2 rows above , 2 below and the 1 middle row
var loops2 = sqs * 2; //
var total_loops = loops2 += 1; //Add on the row tag1 is on (mid row)

//Creating some default variables which are used inside the loop
var minX = 0;
var maxX = 0;
var i=0;
for (i=0;i<=total_loops;i++)
{
minX = currentX - sqs; //The smallest in range for this row
maxX = currentX + sqs; //The largest in range for this row

if(tag_2 >= minX && tag_2 <= maxX)
{
return true; //Houston, we are in range
}

currentX = currentX + offset; //Go up one row for the next loop (30 divs)
}

return false; //Houston, we have a problem
}
I will be releasing a lot more of my code from now on.

I was going to release keyboard movement, but it was already done by somebody.

I also promise to release my combat, chat and possibly trading systems when I finish them.

Toby

Re: [Browser MMORPG] isInRange function

Posted: Sat Jun 04, 2011 7:21 pm
by Mustardy
so what does this do lol?

Re: [Browser MMORPG] isInRange function

Posted: Sat Jun 04, 2011 8:30 pm
by Jackolantern
What do you mean by "the div is in range of another div"?

Re: [Browser MMORPG] isInRange function

Posted: Sun Jun 05, 2011 10:08 pm
by Torniquet
by the looks of it, its for a real time rpg. it detects if div a (the player) is in range of div b (the enemy) by 4 squares (30px by 30px?)

if true then action, if false no action.

well atleast thats what i took from it lol.

Re: [Browser MMORPG] isInRange function

Posted: Sun Jun 05, 2011 11:00 pm
by 62896dude
yeah, that sounds right Torniquet, could actually be pretty useful!

Re: [Browser MMORPG] isInRange function

Posted: Mon Jun 06, 2011 1:20 am
by Jackolantern
Ahh, for a map system lol. I was thinking of divs in the design of the page hehe

Re: [Browser MMORPG] isInRange function

Posted: Mon Jun 06, 2011 2:12 am
by 62896dude
Hahaha nice one Jack :p