Hi guys, I’ve created Random movement for my enemies on a tilemap.
Now I want that some enemies can “see” the player and therefore follow it.
For this reason I make a straight line from my enemy to my player.
The line can
hits obstacles (blocked tiles) so enemy can’t see the player and implement random movement
don’t hits obstacles so enemy can see the player and follow it.
to trace the straight line I’ve thought this code:
float startX, startY, endX, endY
float p, xp, yp;
startX = enemy.getX();
startY = enemy.getY();
endX = player.getX();
endY = player.getY();
p = ((float) endY - startY) / (endX - startX);
for (x=startX; x<=endX; x++)
{
xp = x;
yp = startY + (x - startX) * p;
map.canMove(xp / map.TILE_SIZE, yp / map.TILE_SIZE);
}
this code can trace only lines where x is positive.
you can suggest me any other solution??
I think there can be something better!