Cleanup movement

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 00:59:34 +01:00
parent 46712370a4
commit a872b6fcfa
4 changed files with 76 additions and 65 deletions

View File

@@ -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;

View File

@@ -1,3 +1,5 @@
import 'package:wolf_dart/classes/coordinate_2d.dart';
enum EntityState { staticObj, idle, patrolling, shooting, pain, dead }
abstract class Entity<T> {
@@ -18,4 +20,6 @@ abstract class Entity<T> {
this.mapId = 0,
this.lastActionTime = 0,
});
Coordinate2D get position => Coordinate2D(x, y);
}