Create an angle for each of your 8 directions (0-7).
For example, 0 = 0 degrees, 1 = 45, 2 = 90, 3 = 135, 4 = 190...
Keep in mind those are unit circle degrees (where angle=0 degrees at perfect horizontal line).
1st thing. Get the actual angle between the player and enemy. Use Arctan(
[what are the arguments for this function? Xdifference, Ydifference? try this:]
enemy.x-player.x, enemy.y-player.y).
2nd thing. Anyway, once you get the angle, then check the player's direction. First give the player a range of sight. Say, I'll say that he has a 180 degree field of version (so he's a frog or he's got a panoramic eye or something...heh).
3rd thing. Check the player's facing direction (0-7). If he's at zero, that means his orientation is zero degrees (since we said facing slot 0 = 0 degrees).
4th thing. Check the orientation of his current field with the enemy. IE, if the field of vision is 180 degrees, then his current field of view is
0 - (fieldangle/2)
0 + (fieldangle/2)
IE, he can see between -90 and +90 degrees relative to himself. (A 180 degree span).
5th thing. Check the arctan angle between the player and the enemy (what we got in step one). If the enemy is exactly at zero degrees, he'll be to the player's right. (remember, we said zero degrees = perfect horizontal line).
Anyway. If the player-enemy angle is within the current field of vision, then we can see him.
Try setting an upper and lower bound on the field of vision. Like so.
upperfieldbound = 0 + (fieldangle/2)
lowerfieldbound = 0 - (fiendangle/2)
If the player-enemy-angle is
less than the upperfieldbound and
greater than the lowerfieldbound, he's in our field of vision.
...A lot of steps, but shouldn't be too hard to understand. In addition, I'm not sure if Verge provides a built-in Arctan2() function, but they're very useful as they fix problems with using the original Arctan() function. (Technically there's nothing wrong with Arctan. It just has a weird angle range that isn't exactly what we need for a full 360 degree circle).
So, here's an Arctan2 function. I think I might have gotten this from Mythril, perhaps...
int Math_Atan2(int rise, int run) {
/*Rise is the y-delta, run is the x-delta. */
int t;
if (run == 0)
{
if (rise > 0) return 90;
if (rise < 0) return 270;
else return 0;
}
if (rise == 0)
{
if (run < 0) return 180;
else return 0;
}
t = atan((rise*65536)/run);
if (run > 0 && rise < 0) return t + 360;
if (run < 0) return t + 180;
return t;
}
Hope that helps.