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);
}

View File

@@ -151,15 +151,12 @@ class _WolfRendererState extends State<WolfRenderer>
for (int y = 0; y < currentLevel.length; y++) {
for (int x = 0; x < currentLevel[y].length; x++) {
if (currentLevel[y][x] == 0) {
double safeX = x + 0.5;
double safeY = y + 0.5;
double dist = math.sqrt(
math.pow(safeX - player.x, 2) + math.pow(safeY - player.y, 2),
);
Coordinate2D safeSpot = Coordinate2D(x + 0.5, y + 0.5);
double dist = safeSpot.distanceTo(player.position);
if (dist < shortestDist) {
shortestDist = dist;
nearestSafeSpot = Coordinate2D(safeX, safeY);
nearestSafeSpot = safeSpot;
}
}
}
@@ -180,13 +177,13 @@ class _WolfRendererState extends State<WolfRenderer>
// --- ORCHESTRATOR ---
void _tick(Duration elapsed) {
// 1. Process intentions and receive movement vectors
final movement = _processInputs(elapsed);
final inputResult = _processInputs(elapsed);
doorManager.update(elapsed);
// 2. Explicit State Updates
player.updateWeaponSwitch();
_applyMovementAndCollision(movement.x, movement.y);
_applyMovementAndCollision(inputResult.movement.x, inputResult.movement.y);
_updateEntities(elapsed);
// Explicit reassignment from a pure(r) function
@@ -208,14 +205,15 @@ class _WolfRendererState extends State<WolfRenderer>
damageFlashOpacity = 0.5;
}
// Returns movement deltas instead of modifying class variables
Coordinate2D _processInputs(Duration elapsed) {
// Returns a Record containing both movement delta and rotation delta
({Coordinate2D movement, double dAngle}) _processInputs(Duration elapsed) {
inputManager.update();
const double moveSpeed = 0.16;
const double turnSpeed = 0.12;
double dx = 0;
double dy = 0;
Coordinate2D movement = const Coordinate2D(0, 0);
double dAngle = 0.0;
if (inputManager.requestedWeapon != null) {
player.requestWeaponSwitch(inputManager.requestedWeapon!);
@@ -225,21 +223,23 @@ class _WolfRendererState extends State<WolfRenderer>
player.fire(elapsed.inMilliseconds);
}
// Calculate intended rotation
if (inputManager.isTurningLeft) dAngle -= turnSpeed;
if (inputManager.isTurningRight) dAngle += turnSpeed;
// Calculate intended movement based on CURRENT angle
Coordinate2D forwardVec = Coordinate2D(
math.cos(player.angle),
math.sin(player.angle),
);
if (inputManager.isMovingForward) {
dx += math.cos(player.angle) * moveSpeed;
dy += math.sin(player.angle) * moveSpeed;
movement += forwardVec * moveSpeed;
}
if (inputManager.isMovingBackward) {
dx -= math.cos(player.angle) * moveSpeed;
dy -= math.sin(player.angle) * moveSpeed;
movement -= forwardVec * moveSpeed;
}
if (inputManager.isTurningLeft) player.angle -= turnSpeed;
if (inputManager.isTurningRight) player.angle += turnSpeed;
if (player.angle < 0) player.angle += 2 * math.pi;
if (player.angle > 2 * math.pi) player.angle -= 2 * math.pi;
if (inputManager.isInteracting) {
doorManager.handleInteraction(
player.x,
@@ -248,7 +248,7 @@ class _WolfRendererState extends State<WolfRenderer>
);
}
return Coordinate2D(dx, dy);
return (movement: movement, dAngle: dAngle);
}
// Now receives dx and dy explicitly
@@ -282,11 +282,7 @@ class _WolfRendererState extends State<WolfRenderer>
onDamagePlayer: _takeDamage,
);
} else if (entity is Collectible) {
double dx = player.x - entity.x;
double dy = player.y - entity.y;
double dist = math.sqrt(dx * dx + dy * dy);
if (dist < 0.5) {
if (player.position.distanceTo(entity.position) < 0.5) {
if (player.tryPickup(entity)) {
itemsToRemove.add(entity);
}