PDA

View Full Version : Enemy behavior control.



Master_of_Power
07-06-2007, 03:37 PM
Right now, I'm wanting to have a slightly more advanced enemy AI, and doing so would require functions that would cause the enemy to face link, and move towards Link.

Whats the easiest way to force enemies to face toward the player?

Dan Furst
07-06-2007, 06:18 PM
Once you have a pointer to the npc, use npc->Dir to set how it faces. You probably knew that.

Basically, you need to find the distance between the npc's (x,y) and Link's (x,y), as well as which distance (x or y) is greater.

Here ya go.


import "std.zh"

int x_dist = npc->X - Link->X;
int y_dist = npc->Y - Link->Y;

if(x_dist > 0)
{
if(y_dist > 0)
{
// at link's "bottom right"
if(Abs(x_dist) > Abs(y_dist))
{
// face left
npc->Dir = DIR_LEFT;
}
else
{
// face up
}
}
else
{
// at link's "top right"
if(Abs(x_dist) > Abs(y_dist))
{
// face left
}
else
{
// face down
}
}
}
else // x_dist <= 0
{
if(y_dist > 0)
{
// at link's "bottom left"
if(Abs(x_dist) > Abs(y_dist))
{
// face right
}
else
{
// face up
}
}
else
{
// at link's "top left"
if(Abs(x_dist) > Abs(y_dist))
{
// face right
}
else
{
// face down
}
}
}

Tell me if it works, I didn't try it.