67 lines
1.5 KiB
Dart
67 lines
1.5 KiB
Dart
import 'package:wolf_dart/classes/coordinate_2d.dart';
|
|
|
|
enum EntityState { staticObj, idle, patrolling, shooting, pain, dead }
|
|
|
|
abstract class Entity<T> {
|
|
double x;
|
|
double y;
|
|
int spriteIndex;
|
|
double angle;
|
|
EntityState state;
|
|
int mapId;
|
|
int lastActionTime;
|
|
|
|
Entity({
|
|
required this.x,
|
|
required this.y,
|
|
required this.spriteIndex,
|
|
this.angle = 0.0,
|
|
this.state = EntityState.staticObj,
|
|
this.mapId = 0,
|
|
this.lastActionTime = 0,
|
|
});
|
|
|
|
set position(Coordinate2D pos) {
|
|
x = pos.x;
|
|
y = pos.y;
|
|
}
|
|
|
|
Coordinate2D get position => Coordinate2D(x, y);
|
|
|
|
// NEW: Checks if a projectile or sightline from 'source' can reach this entity
|
|
bool hasLineOfSightFrom(
|
|
Coordinate2D source,
|
|
double sourceAngle,
|
|
double distance,
|
|
bool Function(int x, int y) isWalkable,
|
|
) {
|
|
// Corrected Integer Bresenham Algorithm
|
|
int currentX = source.x.toInt();
|
|
int currentY = source.y.toInt();
|
|
int targetX = x.toInt();
|
|
int targetY = y.toInt();
|
|
|
|
int dx = (targetX - currentX).abs();
|
|
int dy = -(targetY - currentY).abs();
|
|
int sx = currentX < targetX ? 1 : -1;
|
|
int sy = currentY < targetY ? 1 : -1;
|
|
int err = dx + dy;
|
|
|
|
while (true) {
|
|
if (!isWalkable(currentX, currentY)) return false;
|
|
if (currentX == targetX && currentY == targetY) break;
|
|
|
|
int e2 = 2 * err;
|
|
if (e2 >= dy) {
|
|
err += dy;
|
|
currentX += sx;
|
|
}
|
|
if (e2 <= dx) {
|
|
err += dx;
|
|
currentY += sy;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|