Eliminate movement side-effect
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -190,7 +190,14 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
if (player.angle < 0) player.angle += 2 * math.pi;
|
if (player.angle < 0) player.angle += 2 * math.pi;
|
||||||
if (player.angle >= 2 * math.pi) player.angle -= 2 * math.pi;
|
if (player.angle >= 2 * math.pi) player.angle -= 2 * math.pi;
|
||||||
|
|
||||||
_applyMovementAndCollision(inputResult.movement);
|
Coordinate2D validatedPos = _calculateValidatedPosition(
|
||||||
|
player.position,
|
||||||
|
inputResult.movement,
|
||||||
|
);
|
||||||
|
|
||||||
|
player.x = validatedPos.x;
|
||||||
|
player.y = validatedPos.y;
|
||||||
|
|
||||||
_updateEntities(elapsed);
|
_updateEntities(elapsed);
|
||||||
|
|
||||||
// Explicit reassignment from a pure(r) function
|
// Explicit reassignment from a pure(r) function
|
||||||
@@ -258,33 +265,40 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
return (movement: movement, dAngle: dAngle);
|
return (movement: movement, dAngle: dAngle);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _applyMovementAndCollision(Coordinate2D movement) {
|
Coordinate2D _calculateValidatedPosition(
|
||||||
|
Coordinate2D currentPos,
|
||||||
|
Coordinate2D movement,
|
||||||
|
) {
|
||||||
const double margin = 0.3;
|
const double margin = 0.3;
|
||||||
|
double newX = currentPos.x;
|
||||||
|
double newY = currentPos.y;
|
||||||
|
|
||||||
// Calculate the intended new position
|
// Calculate potential new coordinates
|
||||||
Coordinate2D newPos = player.position + movement;
|
Coordinate2D target = currentPos + movement;
|
||||||
|
|
||||||
// Check X axis independently (allows for sliding along walls)
|
// Validate X (allows sliding along walls)
|
||||||
if (movement.x != 0) {
|
if (movement.x != 0) {
|
||||||
int checkX = (movement.x > 0)
|
int checkX = (movement.x > 0)
|
||||||
? (newPos.x + margin).toInt()
|
? (target.x + margin).toInt()
|
||||||
: (newPos.x - margin).toInt();
|
: (target.x - margin).toInt();
|
||||||
|
|
||||||
if (_isWalkable(checkX, player.position.y.toInt())) {
|
if (_isWalkable(checkX, currentPos.y.toInt())) {
|
||||||
player.x = newPos.x;
|
newX = target.x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Y axis independently
|
// Validate Y
|
||||||
if (movement.y != 0) {
|
if (movement.y != 0) {
|
||||||
int checkY = (movement.y > 0)
|
int checkY = (movement.y > 0)
|
||||||
? (newPos.y + margin).toInt()
|
? (target.y + margin).toInt()
|
||||||
: (newPos.y - margin).toInt();
|
: (target.y - margin).toInt();
|
||||||
|
|
||||||
if (_isWalkable(player.position.x.toInt(), checkY)) {
|
if (_isWalkable(newX.toInt(), checkY)) {
|
||||||
player.y = newPos.y;
|
newY = target.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Coordinate2D(newX, newY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateEntities(Duration elapsed) {
|
void _updateEntities(Duration elapsed) {
|
||||||
|
|||||||
Reference in New Issue
Block a user