Rename ball -> new_pos

Fix paddle reset
This commit is contained in:
Matt Low 2018-11-11 21:25:09 +04:00
parent 659fde4622
commit 78d0a72217

View File

@ -26,7 +26,7 @@ public class Ball extends Entity {
public void render() { public void render() {
getShapeRenderer().begin(ShapeType.Filled); getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(BALL_COLOR); getShapeRenderer().setColor(BALL_COLOR);
getShapeRenderer().circle(getX(), getY(), RADIUS); getShapeRenderer().circle(getPos().x, getPos().y, RADIUS);
getShapeRenderer().end(); getShapeRenderer().end();
} }
@ -36,7 +36,7 @@ public class Ball extends Entity {
return; return;
} }
Vector2 ball = getPos().cpy().add(direction.cpy().scl(speed)); Vector2 new_pos = getPos().cpy().add(direction.cpy().scl(speed));
boolean brick_collision = false; boolean brick_collision = false;
Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator(); Iterator<Brick> brickIterator = getBrickBuster().getBricks().iterator();
@ -47,9 +47,9 @@ public class Ball extends Entity {
Vector2 v1 = vertices[i]; Vector2 v1 = vertices[i];
Vector2 v2 = vertices[i+1 < vertices.length? i+1 : 0]; Vector2 v2 = vertices[i+1 < vertices.length? i+1 : 0];
Vector2 segment = v2.cpy().sub(v1); Vector2 segment = v2.cpy().sub(v1);
Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, ball.cpy()); Vector2 nearest = Utils.nearestPoint(v1.cpy(), segment, new_pos.cpy());
if (nearest.dst(ball.x, ball.y) <= RADIUS) { if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
brickIterator.remove(); brickIterator.remove();
Utils.reflect(direction, segment.nor()); Utils.reflect(direction, segment.nor());
brick_collision = true; brick_collision = true;
@ -58,25 +58,26 @@ public class Ball extends Entity {
} }
} }
if (ball.x + RADIUS > BrickBuster.WIDTH || ball.x - RADIUS < 0) { if (new_pos.x + RADIUS > BrickBuster.WIDTH || new_pos.x - RADIUS < 0) {
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
} else if (ball.y + RADIUS > BrickBuster.HEIGHT) { } else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) {
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE); Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
} else if (ball.y - RADIUS < 0) { } else if (new_pos.y - RADIUS < 0) {
reset(); reset();
return;
} }
if (direction.y < 0) { if (direction.y < 0) {
Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge(); Pair<Vector2, Vector2> paddle = getBrickBuster().getPaddle().getTopEdge();
Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); Vector2 lineDir = paddle.getValue().sub(paddle.getKey());
Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, ball.cpy()); Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy());
if (nearest.dst(ball.x, ball.y) <= RADIUS) { if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
paddleCollision(); paddleCollision();
} }
} }
setPos(ball); getPos().add(direction.cpy().scl(speed));
} }
public void launch() { public void launch() {