0 votes
by (170 points)

I'm super new to this and mostly testing things out but I've run into a bit of a snag I hope someone can help me out with. 

I'm working with a mapArray currently, and I'm trying to make it so that if the player gets around a certain point, a description shows up. An example is this- 

 

[[0,0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,3,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,2,0,0,0,0,0]]>>

I have it to where the text shows up when they get in front of the 3 (as wanted).

Now, I have something like this- 

<<if $positionX is 5 and $positionY is 4>>
<<set $text to "Blah blah.">>
<</if>>

But, that leaves mutiple other spots around it open. Do I really have to do 8 different $positionX and 8 $positionY's? or is there a cleaner way?

1 Answer

0 votes
by (44.7k points)

Math to the rescue!

Basically, you just need to measure the distance between the two points, and if it's less than some distance, then you're within that circle of positions around the point.  So, you could do this:

<<set _a = Math.pow($pointX - $positionX, 2)>>
<<set _b = Math.pow($pointY - $positionY, 2)>>
<<set _c = Math.sqrt(_a + _b)>>  /* dist = square root of (A^2 + B^2) */
<<if _c <= Math.sqrt(2)>>
	<<set $text to "Blah blah.">>
<</if>>

So that should set the $text value if the position is in one of the eight places around the point.

Hope that helps!  :-)

...