Hi,
Implementing line of sight at the moment so mobs will only attack if can see you. I’m using the following code
to do a ray, where xo,x1 is the bad guy and x1,y1 will be the player, this look efficient enough, seems ok?
public static void RayCast(int x0, int y0, int x1, int y1)
{
int dx = Math.Abs(x1 - x0);
int dy = Math.Abs(y1 - y0);
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int err = dx - dy;
while (true)
{
if (x0 == x1 && y0 == y1)
break;
int e2 = err * 2;
if (e2 > -dx)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
Am I right in thinking only need to cast the one ray as not aiming for pixel perfectness?..