200 lines
4.7 KiB
Java
200 lines
4.7 KiB
Java
package com.me.brickbuster;
|
|
|
|
import com.badlogic.gdx.ApplicationAdapter;
|
|
import com.badlogic.gdx.Gdx;
|
|
import com.badlogic.gdx.graphics.Color;
|
|
import com.badlogic.gdx.graphics.GL20;
|
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
|
import com.badlogic.gdx.math.MathUtils;
|
|
import com.badlogic.gdx.math.Vector2;
|
|
import com.me.brickbuster.entity.*;
|
|
import com.me.brickbuster.entity.powerup.*;
|
|
|
|
import java.util.*;
|
|
|
|
public class BrickBuster extends ApplicationAdapter {
|
|
|
|
public static final int WIDTH = 800;
|
|
public static final int HEIGHT = 600;
|
|
public static final String TITLE = "Brick Buster";
|
|
|
|
public static final int SHIELD_HEIGHT = 5;
|
|
|
|
public static final Vector2 HORIZONTAL_EDGE = new Vector2(1, 0);
|
|
public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1);
|
|
|
|
private static final Map<Class<? extends PowerUp>, Integer> powerUpWeights;
|
|
private static final int weightSum;
|
|
|
|
private BitmapFont font;
|
|
private SpriteBatch batch;
|
|
private ShapeRenderer sr;
|
|
|
|
private Paddle paddle;
|
|
private ArrayList<Ball> balls;
|
|
private ArrayList<Brick> bricks;
|
|
private ArrayList<PowerUp> powerUps;
|
|
|
|
private boolean shieldActive = false;
|
|
|
|
@Override
|
|
public void create () {
|
|
font = new BitmapFont();
|
|
batch = new SpriteBatch();
|
|
sr = new ShapeRenderer();
|
|
|
|
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.15f)) {
|
|
powerUpType = getWeightedPowerUp();
|
|
}
|
|
bricks.add(new Brick(this, powerUpType, x, HEIGHT - y));
|
|
}
|
|
}
|
|
|
|
balls = new ArrayList<Ball>();
|
|
balls.add(new Ball(this));
|
|
}
|
|
|
|
@Override
|
|
public void render () {
|
|
Gdx.gl.glClearColor(0.5f,1,1,1);
|
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
long start_update = System.nanoTime();
|
|
update(Gdx.graphics.getDeltaTime());
|
|
long finish_update = System.nanoTime() - start_update;
|
|
|
|
long start_render = System.nanoTime();
|
|
for (Brick block : bricks) {
|
|
block.render();
|
|
}
|
|
for (PowerUp powerUp : powerUps) {
|
|
powerUp.render();
|
|
}
|
|
for (Ball ball : balls) {
|
|
ball.render();
|
|
}
|
|
paddle.render();
|
|
|
|
if (shieldActive) {
|
|
sr.begin(ShapeRenderer.ShapeType.Filled);
|
|
sr.setColor(Color.SALMON);
|
|
sr.rect(0, 0, WIDTH, SHIELD_HEIGHT);
|
|
sr.end();
|
|
}
|
|
|
|
long finish_render = System.nanoTime() - start_render;
|
|
|
|
batch.begin();
|
|
//batch.setColor(Color.BLACK);
|
|
font.draw(batch, String.format("FPS: %d Update: %.2f ms Render: %.2f ms",
|
|
Gdx.graphics.getFramesPerSecond(), finish_update/1000000f, finish_render/1000000f), 0, 13);
|
|
batch.end();
|
|
}
|
|
|
|
public void update(float dt) {
|
|
for (Iterator<Ball> it = balls.iterator(); it.hasNext();) {
|
|
Ball ball = it.next();
|
|
ball.update(dt);
|
|
if (ball.isDead()) {
|
|
it.remove();
|
|
}
|
|
}
|
|
|
|
if (balls.isEmpty()) {
|
|
reset();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
public void reset() {
|
|
Paddle paddle = getPaddle();
|
|
Ball ball = new Ball(this);
|
|
ball.setX(paddle.getX());
|
|
ball.setY(Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + Ball.RADIUS);
|
|
balls.add(ball);
|
|
paddle.setSticky(false);
|
|
paddle.setWidth(Paddle.DEFAULT_WIDTH);
|
|
}
|
|
|
|
@Override
|
|
public void dispose () {
|
|
}
|
|
|
|
private static final Class<? extends PowerUp> getWeightedPowerUp() {
|
|
int remaining = MathUtils.random(weightSum);
|
|
for (Map.Entry<Class<? extends PowerUp>, Integer> entry : powerUpWeights.entrySet()) {
|
|
remaining -= entry.getValue();
|
|
if (remaining < 0) {
|
|
return entry.getKey();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void setShieldActive(boolean shieldActive) {
|
|
this.shieldActive = shieldActive;
|
|
}
|
|
|
|
public boolean isShieldActive() {
|
|
return shieldActive;
|
|
}
|
|
|
|
public ArrayList<Ball> getBalls() {
|
|
return balls;
|
|
}
|
|
|
|
public Paddle getPaddle() {
|
|
return paddle;
|
|
}
|
|
|
|
public ArrayList<Brick> getBricks() {
|
|
return bricks;
|
|
}
|
|
|
|
public void addPowerUp(PowerUp powerUp) {
|
|
this.powerUps.add(powerUp);
|
|
}
|
|
|
|
static {
|
|
Map<Class<? extends PowerUp>, Integer> tmp = new HashMap<Class<? extends PowerUp>, Integer>();
|
|
|
|
// Assign PowerUp weights here
|
|
tmp.put(GluePowerUp.class, 30);
|
|
tmp.put(LongerPaddlePowerUp.class, 40);
|
|
tmp.put(MultiBallPowerUp.class, 20);
|
|
tmp.put(ShieldPowerUp.class, 40);
|
|
powerUpWeights = Collections.unmodifiableMap(tmp);
|
|
|
|
int sum = 0;
|
|
for (int x : powerUpWeights.values()) {
|
|
sum += x;
|
|
}
|
|
weightSum = sum;
|
|
}
|
|
} |