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

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