Compare commits

..

No commits in common. "1f9c2e6db0005e4feb73056ce73e610bf1ea692d" and "353afe1cee8d0d5dd72d12c786b55d856125a6d4" have entirely different histories.

7 changed files with 20 additions and 160 deletions

View File

@ -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.*;
import com.me.brickbuster.entity.Ball;
import com.me.brickbuster.entity.Brick;
import com.me.brickbuster.entity.Paddle;
import java.util.ArrayList;
import java.util.Iterator;
public class BrickBuster extends ApplicationAdapter {
@ -22,24 +22,18 @@ public class BrickBuster extends ApplicationAdapter {
private Ball ball;
private Paddle paddle;
private ArrayList<Brick> bricks;
private ArrayList<PowerUp> powerUps;
private boolean playing = false;
@Override
public void create () {
ball = new Ball(this);
paddle = new Paddle(this);
powerUps = new ArrayList<PowerUp>();
bricks = new ArrayList<Brick>();
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));
Class<? extends PowerUp> powerUpType = null;
if (MathUtils.randomBoolean(0.08f)) {
powerUpType = GluePowerUp.class;
}
bricks.add(new Brick(this, powerUpType, x, HEIGHT - y));
bricks.add(new Brick(this, x, HEIGHT - y));
}
}
}
@ -55,31 +49,17 @@ 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() || ball.isStuck())) {
if (Gdx.input.justTouched() && !isPlaying()) {
playing = true;
ball.launch();
}
ball.update(dt);
paddle.update(dt);
for (Iterator<PowerUp> it = powerUps.iterator(); it.hasNext();) {
PowerUp powerUp = it.next();
powerUp.update(dt);
if(powerUp.isCaught()) {
it.remove();
}
}
if (getBricks().isEmpty()) {
create();
playing = false;
@ -109,9 +89,4 @@ public class BrickBuster extends ApplicationAdapter {
public ArrayList<Brick> getBricks() {
return bricks;
}
public void addPowerUp(PowerUp powerUp) {
this.powerUps.add(powerUp);
}
}

View File

@ -14,13 +14,9 @@ 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 = 350;
public static final float BOOST_SPEED = 450;
public static final int BLOCKS_FOR_BOOST = 39;
public Vector2 direction;
private float speed;
private boolean isStuck = false;
public float speed = 300;
public Ball(BrickBuster brickBuster) {
super(brickBuster,BrickBuster.WIDTH/2, Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS);
@ -55,9 +51,8 @@ public class Ball extends Entity {
if (nearest.dst(new_pos.x, new_pos.y) <= RADIUS) {
brickIterator.remove();
brick.hit();
if (getBrickBuster().getBricks().size() <= BLOCKS_FOR_BOOST) {
speed = BOOST_SPEED;
if (getBrickBuster().getBricks().size() <= 39) {
speed = 450;
}
Utils.reflect(direction, segment.nor());
brick_collision = true;
@ -91,18 +86,9 @@ 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));
@ -112,12 +98,6 @@ 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;
}
}

View File

@ -10,13 +10,10 @@ public class Brick extends Entity {
public static final int BLOCK_WIDTH = 50;
public static final int BLOCK_HEIGHT = 20;
private Class<? extends PowerUp> powerUpType;
private Vector2[] vertices;
public Brick(BrickBuster brickBuster, Class<? extends PowerUp> powerUpType, int x, int y) {
public Brick(BrickBuster brickBuster, int x, int y) {
super(brickBuster, x, y);
this.powerUpType = powerUpType;
this.vertices = new Vector2[] {
new Vector2(x, y),
@ -38,18 +35,6 @@ 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;
}

View File

@ -10,7 +10,7 @@ public abstract class Entity {
private BrickBuster brickBuster;
private Vector2 pos;
public Entity(BrickBuster brickBuster, float x, float y) {
public Entity(BrickBuster brickBuster, int x, int y) {
this.shapeRenderer = new ShapeRenderer();
this.brickBuster = brickBuster;
this.pos = new Vector2(x, y);

View File

@ -1,17 +0,0 @@
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);
}
}

View File

@ -14,9 +14,7 @@ public class Paddle extends Entity {
public static final int PADDLE_WIDTH = 100;
public static final int PADDLE_HEIGHT = 10;
public static final int PADDLE_Y = 15;
public static final int PADDLE_SPEED = 375;
private boolean sticky = false;
public static final int PADDLE_SPEED = 6;
public Paddle(BrickBuster brickBuster) {
super(brickBuster, BrickBuster.WIDTH / 2 - PADDLE_WIDTH / 2, PADDLE_Y);
@ -25,7 +23,7 @@ public class Paddle extends Entity {
@Override
public void render() {
getShapeRenderer().begin(ShapeType.Filled);
getShapeRenderer().setColor(isSticky()? Color.GRAY : PADDLE_COLOR);
getShapeRenderer().setColor(PADDLE_COLOR);
getShapeRenderer().rect(getX(), getY(), PADDLE_WIDTH, PADDLE_HEIGHT);
getShapeRenderer().end();
}
@ -34,21 +32,21 @@ public class Paddle extends Entity {
public void update(float dt) {
Ball ball = getBrickBuster().getBall();
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
if (getX() - PADDLE_SPEED * dt < 0) {
if (getX() - PADDLE_SPEED < 0) {
return;
}
setX(getX() - PADDLE_SPEED * dt);
if (!getBrickBuster().isPlaying() || ball.isStuck()) {
ball.setX(ball.getX() - PADDLE_SPEED * dt);
setX(getX() - PADDLE_SPEED);
if (!getBrickBuster().isPlaying()) {
ball.setX(ball.getX() - PADDLE_SPEED);
}
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
if (getX() + PADDLE_SPEED * dt + PADDLE_WIDTH > BrickBuster.WIDTH) {
if (getX() + PADDLE_SPEED + PADDLE_WIDTH > BrickBuster.WIDTH) {
return;
}
setX(getX() + PADDLE_SPEED * dt);
if (!getBrickBuster().isPlaying() || ball.isStuck()) {
ball.setX(ball.getX() + PADDLE_SPEED * dt);
setX(getX() + PADDLE_SPEED);
if (!getBrickBuster().isPlaying()) {
ball.setX(ball.getX() + PADDLE_SPEED);
}
}
}
@ -60,11 +58,4 @@ public class Paddle extends Entity {
);
}
public boolean isSticky() {
return sticky;
}
public void setSticky(boolean sticky) {
this.sticky = sticky;
}
}

View File

@ -1,54 +0,0 @@
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<Vector2, Vector2> 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;
}
}