diff --git a/lib/features/renderer/renderer.dart b/lib/features/renderer/renderer.dart index 9183866..20f7acc 100644 --- a/lib/features/renderer/renderer.dart +++ b/lib/features/renderer/renderer.dart @@ -183,7 +183,7 @@ class _WolfRendererState extends State // 2. Explicit State Updates player.updateWeaponSwitch(); - _applyMovementAndCollision(inputResult.movement.x, inputResult.movement.y); + _applyMovementAndCollision(inputResult.movement); _updateEntities(elapsed); // Explicit reassignment from a pure(r) function @@ -251,22 +251,32 @@ class _WolfRendererState extends State return (movement: movement, dAngle: dAngle); } - // Now receives dx and dy explicitly - void _applyMovementAndCollision(double dx, double dy) { + void _applyMovementAndCollision(Coordinate2D movement) { const double margin = 0.3; - double newX = player.x + dx; - int checkX = (dx > 0) ? (newX + margin).toInt() : (newX - margin).toInt(); + // Calculate the intended new position + Coordinate2D newPos = player.position + movement; - if (_isWalkable(checkX, player.y.toInt())) { - player.x = newX; + // Check X axis independently (allows for sliding along walls) + if (movement.x != 0) { + int checkX = (movement.x > 0) + ? (newPos.x + margin).toInt() + : (newPos.x - margin).toInt(); + + if (_isWalkable(checkX, player.position.y.toInt())) { + player.x = newPos.x; + } } - double newY = player.y + dy; - int checkY = (dy > 0) ? (newY + margin).toInt() : (newY - margin).toInt(); + // Check Y axis independently + if (movement.y != 0) { + int checkY = (movement.y > 0) + ? (newPos.y + margin).toInt() + : (newPos.y - margin).toInt(); - if (_isWalkable(player.x.toInt(), checkY)) { - player.y = newY; + if (_isWalkable(player.position.x.toInt(), checkY)) { + player.y = newPos.y; + } } }