diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 0e405a0..7373491 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.Input; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef; @@ -48,29 +49,27 @@ public class Paddle extends Entity implements PhysicsBody { @Override public void update(float dt) { + float displacement = PADDLE_SPEED * dt; + float adjust = 0; if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { - if (pos.x - width/2 - PADDLE_SPEED * dt < 0) { - setX(width/2); - return; - } - setX(pos.x - PADDLE_SPEED * dt); - - for (Ball ball : state.balls) { - if (ball.isStuck()) { - ball.setX(ball.getX() - PADDLE_SPEED * dt); - } + if (pos.x - width / 2 - displacement < PlayState.EDGE_PADDING) { + adjust = -(pos.x - width/2 - PlayState.EDGE_PADDING); + } else { + adjust = -displacement; } } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - if (pos.x + width/2 + PADDLE_SPEED * dt > PlayState.BOARD_WIDTH) { - setX(PlayState.BOARD_WIDTH - width/2); - return; + if (pos.x + width / 2 + displacement > PlayState.BOARD_WIDTH-PlayState.EDGE_PADDING) { + adjust = PlayState.BOARD_WIDTH - PlayState.EDGE_PADDING - width/2 - pos.x; + } else { + adjust = displacement; } - setX(pos.x + PADDLE_SPEED * dt); - + } + if (!MathUtils.isZero(adjust)) { + setX(pos.x + adjust); for (Ball ball : state.balls) { if (ball.isStuck()) { - ball.setX(ball.getX() + PADDLE_SPEED * dt); + ball.setX(ball.getX() + adjust); } } }