@@ -61,12 +61,10 @@ abstract class Enemy extends Entity {
|
||||
Coordinate2D playerPosition,
|
||||
bool Function(int x, int y) isWalkable,
|
||||
) {
|
||||
double dx = playerPosition.x - x;
|
||||
double dy = playerPosition.y - y;
|
||||
double distance = math.sqrt(dx * dx + dy * dy);
|
||||
double distance = position.distanceTo(playerPosition);
|
||||
|
||||
// 1. FOV Check
|
||||
double angleToPlayer = math.atan2(dy, dx);
|
||||
double angleToPlayer = position.angleTo(playerPosition);
|
||||
double diff = angle - angleToPlayer;
|
||||
|
||||
while (diff <= -math.pi) {
|
||||
@@ -79,14 +77,12 @@ abstract class Enemy extends Entity {
|
||||
if (diff.abs() > math.pi / 2) return false;
|
||||
|
||||
// 2. Map Check
|
||||
double dirX = dx / distance;
|
||||
double dirY = dy / distance;
|
||||
Coordinate2D dir = (playerPosition - position).normalized;
|
||||
double stepSize = 0.2;
|
||||
for (double i = 0; i < distance; i += stepSize) {
|
||||
int checkX = (x + dirX * i).toInt();
|
||||
int checkY = (y + dirY * i).toInt();
|
||||
|
||||
if (!isWalkable(checkX, checkY)) return false;
|
||||
for (double i = 0; i < distance; i += stepSize) {
|
||||
Coordinate2D checkPos = position + (dir * i);
|
||||
if (!isWalkable(checkPos.x.toInt(), checkPos.y.toInt())) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user