From 3e0d4657f2d8595a6bd61bfcfe4a29baadac4986 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Mon, 12 Nov 2018 09:28:12 +0400 Subject: [PATCH] Add PowerUp logic + GluePowerUp --- core/src/com/me/brickbuster/BrickBuster.java | 35 ++++++++++-- core/src/com/me/brickbuster/entity/Ball.java | 26 +++++++-- core/src/com/me/brickbuster/entity/Brick.java | 17 +++++- .../me/brickbuster/entity/GluePowerUp.java | 17 ++++++ .../src/com/me/brickbuster/entity/Paddle.java | 15 ++++-- .../com/me/brickbuster/entity/PowerUp.java | 54 +++++++++++++++++++ 6 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 core/src/com/me/brickbuster/entity/GluePowerUp.java create mode 100644 core/src/com/me/brickbuster/entity/PowerUp.java diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index 7e2c1c8..782f0e3 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -3,12 +3,12 @@ package com.me.brickbuster; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; -import com.me.brickbuster.entity.Ball; -import com.me.brickbuster.entity.Brick; -import com.me.brickbuster.entity.Paddle; +import com.me.brickbuster.entity.*; import java.util.ArrayList; +import java.util.Iterator; public class BrickBuster extends ApplicationAdapter { @@ -22,18 +22,24 @@ public class BrickBuster extends ApplicationAdapter { private Ball ball; private Paddle paddle; private ArrayList bricks; + private ArrayList powerUps; private boolean playing = false; @Override public void create () { ball = new Ball(this); paddle = new Paddle(this); + powerUps = new ArrayList(); bricks = new ArrayList(); for (int col = 0; col < 13; col++) { for (int row = 0; row < 7; row++) { int x = 15 + (col * (Brick.BLOCK_WIDTH + 10)); int y = 15 + Brick.BLOCK_HEIGHT + (row * (Brick.BLOCK_HEIGHT + 10)); - bricks.add(new Brick(this, x, HEIGHT - y)); + Class powerUpType = null; + if (MathUtils.randomBoolean(0.5f)) { + powerUpType = GluePowerUp.class; + } + bricks.add(new Brick(this, powerUpType, x, HEIGHT - y)); } } } @@ -49,17 +55,31 @@ public class BrickBuster extends ApplicationAdapter { block.render(); } + for (PowerUp powerUp : powerUps) { + powerUp.render(); + } + ball.render(); paddle.render(); } public void update(float dt) { - if (Gdx.input.justTouched() && !isPlaying()) { + if (Gdx.input.justTouched() && (!isPlaying() || ball.isStuck())) { playing = true; ball.launch(); } ball.update(dt); paddle.update(dt); + + + for (Iterator it = powerUps.iterator(); it.hasNext();) { + PowerUp powerUp = it.next(); + powerUp.update(dt); + if(powerUp.isCaught()) { + it.remove(); + } + } + if (getBricks().isEmpty()) { create(); playing = false; @@ -89,4 +109,9 @@ public class BrickBuster extends ApplicationAdapter { public ArrayList getBricks() { return bricks; } + + public void addPowerUp(PowerUp powerUp) { + this.powerUps.add(powerUp); + } + } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index a074fa8..31e57dd 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -14,9 +14,13 @@ public class Ball extends Entity { public static final int RADIUS = 12; public static final Color BALL_COLOR = Color.CHARTREUSE; + public static final float DEFAULT_SPEED = 300; + public static final float BOOST_SPEED = 450; + public static final int BLOCKS_FOR_BOOST = 39; public Vector2 direction; - public float speed = 300; + private float speed; + private boolean isStuck = false; public Ball(BrickBuster brickBuster) { super(brickBuster,BrickBuster.WIDTH/2, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); @@ -51,8 +55,9 @@ public class Ball extends Entity { if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) { brickIterator.remove(); - if (getBrickBuster().getBricks().size() <= 39) { - speed = 450; + brick.hit(); + if (getBrickBuster().getBricks().size() <= BLOCKS_FOR_BOOST) { + speed = BOOST_SPEED; } Utils.reflect(direction, segment.nor()); brick_collision = true; @@ -86,9 +91,18 @@ public class Ball extends Entity { public void launch() { float angle = MathUtils.random(MathUtils.PI/2) + MathUtils.PI/4; direction = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); + speed = DEFAULT_SPEED; + isStuck = false; } public void paddleCollision() { + Paddle paddle = getBrickBuster().getPaddle(); + if (paddle.isSticky()) { + speed = 0; + isStuck = true; + setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); + return; + } float paddleCenter = getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2; float rel = MathUtils.clamp((getX() - paddleCenter) + 50, 5, 95); float newAngle = MathUtils.PI - (MathUtils.PI * (rel/100)); @@ -98,6 +112,12 @@ public class Ball extends Entity { public void reset() { setX(getBrickBuster().getPaddle().getX() + Paddle.PADDLE_WIDTH/2); setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS); + getBrickBuster().getPaddle().setSticky(false); getBrickBuster().setPlaying(false); } + + public boolean isStuck() { + return isStuck; + } + } diff --git a/core/src/com/me/brickbuster/entity/Brick.java b/core/src/com/me/brickbuster/entity/Brick.java index b64a888..f866239 100644 --- a/core/src/com/me/brickbuster/entity/Brick.java +++ b/core/src/com/me/brickbuster/entity/Brick.java @@ -10,10 +10,13 @@ public class Brick extends Entity { public static final int BLOCK_WIDTH = 50; public static final int BLOCK_HEIGHT = 20; + private Class powerUpType; + private Vector2[] vertices; - public Brick(BrickBuster brickBuster, int x, int y) { + public Brick(BrickBuster brickBuster, Class powerUpType, int x, int y) { super(brickBuster, x, y); + this.powerUpType = powerUpType; this.vertices = new Vector2[] { new Vector2(x, y), @@ -35,6 +38,18 @@ public class Brick extends Entity { public void update(float dt) { } + public boolean hit() { + if (powerUpType != null) { + try { + PowerUp powerUp = powerUpType.getConstructor(BrickBuster.class, Brick.class).newInstance(getBrickBuster(), this); + getBrickBuster().addPowerUp(powerUp); + } catch(Exception e) { + + } + } + return true; + } + public Vector2[] getVertices() { return vertices; } diff --git a/core/src/com/me/brickbuster/entity/GluePowerUp.java b/core/src/com/me/brickbuster/entity/GluePowerUp.java new file mode 100644 index 0000000..c043c07 --- /dev/null +++ b/core/src/com/me/brickbuster/entity/GluePowerUp.java @@ -0,0 +1,17 @@ +package com.me.brickbuster.entity; + +import com.badlogic.gdx.graphics.Color; +import com.me.brickbuster.BrickBuster; + +public class GluePowerUp extends PowerUp { + + public GluePowerUp(BrickBuster brickBuster, Brick brick) { + super(brickBuster, brick, Color.WHITE); + } + + @Override + public void activate() { + getBrickBuster().getPaddle().setSticky(true); + } + +} diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 5b1a923..f974965 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -16,6 +16,8 @@ public class Paddle extends Entity { public static final int PADDLE_Y = 15; public static final int PADDLE_SPEED = 6; + private boolean sticky = false; + public Paddle(BrickBuster brickBuster) { super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y); } @@ -23,7 +25,7 @@ public class Paddle extends Entity { @Override public void render() { getShapeRenderer().begin(ShapeType.Filled); - getShapeRenderer().setColor(PADDLE_COLOR); + getShapeRenderer().setColor(isSticky()? Color.GRAY : PADDLE_COLOR); getShapeRenderer().rect(getX(), getY(), PADDLE_WIDTH, PADDLE_HEIGHT); getShapeRenderer().end(); } @@ -36,7 +38,7 @@ public class Paddle extends Entity { return; } setX(getX() - PADDLE_SPEED); - if (!getBrickBuster().isPlaying()) { + if (!getBrickBuster().isPlaying() || ball.isStuck()) { ball.setX(ball.getX() - PADDLE_SPEED); } } @@ -45,7 +47,7 @@ public class Paddle extends Entity { return; } setX(getX() + PADDLE_SPEED); - if (!getBrickBuster().isPlaying()) { + if (!getBrickBuster().isPlaying() || ball.isStuck()) { ball.setX(ball.getX() + PADDLE_SPEED); } } @@ -58,4 +60,11 @@ public class Paddle extends Entity { ); } + public boolean isSticky() { + return sticky; + } + + public void setSticky(boolean sticky) { + this.sticky = sticky; + } } diff --git a/core/src/com/me/brickbuster/entity/PowerUp.java b/core/src/com/me/brickbuster/entity/PowerUp.java new file mode 100644 index 0000000..c6258af --- /dev/null +++ b/core/src/com/me/brickbuster/entity/PowerUp.java @@ -0,0 +1,54 @@ +package com.me.brickbuster.entity; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2; +import com.me.brickbuster.BrickBuster; +import com.me.brickbuster.Utils; +import net.dermetfan.utils.Pair; + +public abstract class PowerUp extends Entity { + + public static final int RADIUS = 10; + public static final int FALL_SPEED = 100; + + private Color color; + private boolean isCaught; + + public PowerUp(BrickBuster brickBuster, Brick brick, Color color) { + super(brickBuster, brick.getX() + Brick.BLOCK_WIDTH/2, brick.getY() + Brick.BLOCK_HEIGHT/2); + this.color = color; + } + + @Override + public void render() { + getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); + getShapeRenderer().setColor(color); + getShapeRenderer().circle(getX(), getY(), RADIUS); + getShapeRenderer().end(); + } + + @Override + public void update(float dt) { + setY(getY() - FALL_SPEED * dt); + + Pair paddle = getBrickBuster().getPaddle().getTopEdge(); + Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); + Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, getPos().cpy()); + + if (nearest.dst(getX(), getY()) <= RADIUS) { + activate(); + isCaught = true; + } + + if (getY() + RADIUS < 0) { + isCaught = true; + } + } + + public abstract void activate(); + + public boolean isCaught() { + return isCaught; + } +}