Cleanup movement and collision

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 01:00:55 +01:00
parent a872b6fcfa
commit d58f799aa7

View File

@@ -183,7 +183,7 @@ class _WolfRendererState extends State<WolfRenderer>
// 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<WolfRenderer>
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;
}
}
}