Cleanup movement and collision
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -183,7 +183,7 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
|
|
||||||
// 2. Explicit State Updates
|
// 2. Explicit State Updates
|
||||||
player.updateWeaponSwitch();
|
player.updateWeaponSwitch();
|
||||||
_applyMovementAndCollision(inputResult.movement.x, inputResult.movement.y);
|
_applyMovementAndCollision(inputResult.movement);
|
||||||
_updateEntities(elapsed);
|
_updateEntities(elapsed);
|
||||||
|
|
||||||
// Explicit reassignment from a pure(r) function
|
// Explicit reassignment from a pure(r) function
|
||||||
@@ -251,22 +251,32 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
return (movement: movement, dAngle: dAngle);
|
return (movement: movement, dAngle: dAngle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now receives dx and dy explicitly
|
void _applyMovementAndCollision(Coordinate2D movement) {
|
||||||
void _applyMovementAndCollision(double dx, double dy) {
|
|
||||||
const double margin = 0.3;
|
const double margin = 0.3;
|
||||||
|
|
||||||
double newX = player.x + dx;
|
// Calculate the intended new position
|
||||||
int checkX = (dx > 0) ? (newX + margin).toInt() : (newX - margin).toInt();
|
Coordinate2D newPos = player.position + movement;
|
||||||
|
|
||||||
if (_isWalkable(checkX, player.y.toInt())) {
|
// Check X axis independently (allows for sliding along walls)
|
||||||
player.x = newX;
|
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;
|
// Check Y axis independently
|
||||||
int checkY = (dy > 0) ? (newY + margin).toInt() : (newY - margin).toInt();
|
if (movement.y != 0) {
|
||||||
|
int checkY = (movement.y > 0)
|
||||||
|
? (newPos.y + margin).toInt()
|
||||||
|
: (newPos.y - margin).toInt();
|
||||||
|
|
||||||
if (_isWalkable(player.x.toInt(), checkY)) {
|
if (_isWalkable(player.position.x.toInt(), checkY)) {
|
||||||
player.y = newY;
|
player.y = newPos.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user