Compare commits

...

2 Commits

7 changed files with 78 additions and 38 deletions

View File

@ -2,16 +2,15 @@ package com.me.brickbuster;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 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.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.entity.*; import com.me.brickbuster.entity.*;
import com.me.brickbuster.entity.powerup.GluePowerUp; import com.me.brickbuster.entity.powerup.*;
import com.me.brickbuster.entity.powerup.LongerPaddlePowerUp;
import com.me.brickbuster.entity.powerup.MultiBallPowerUp;
import com.me.brickbuster.entity.powerup.PowerUp;
import java.util.*; import java.util.*;
@ -21,6 +20,8 @@ public class BrickBuster extends ApplicationAdapter {
public static final int HEIGHT = 600; public static final int HEIGHT = 600;
public static final String TITLE = "Brick Buster"; 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 HORIZONTAL_EDGE = new Vector2(1, 0);
public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1); public static final Vector2 VERTICAL_EDGE = new Vector2(0, 1);
@ -29,16 +30,20 @@ public class BrickBuster extends ApplicationAdapter {
private BitmapFont font; private BitmapFont font;
private SpriteBatch batch; private SpriteBatch batch;
private ShapeRenderer sr;
private Paddle paddle; private Paddle paddle;
private ArrayList<Ball> balls; private ArrayList<Ball> balls;
private ArrayList<Brick> bricks; private ArrayList<Brick> bricks;
private ArrayList<PowerUp> powerUps; private ArrayList<PowerUp> powerUps;
private boolean shieldActive = false;
@Override @Override
public void create () { public void create () {
font = new BitmapFont(); font = new BitmapFont();
batch = new SpriteBatch(); batch = new SpriteBatch();
sr = new ShapeRenderer();
paddle = new Paddle(this); paddle = new Paddle(this);
@ -66,21 +71,30 @@ public class BrickBuster extends ApplicationAdapter {
Gdx.gl.glClearColor(0.5f,1,1,1); Gdx.gl.glClearColor(0.5f,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
long start_update = System.nanoTime(); long start_update = System.nanoTime();
update(Gdx.graphics.getDeltaTime()); update(Gdx.graphics.getDeltaTime());
long finish_update = System.nanoTime() - start_update; long finish_update = System.nanoTime() - start_update;
long start_render = System.nanoTime(); long start_render = System.nanoTime();
for (Brick block : bricks) { for (Brick block : bricks) {
block.render(); block.render(sr);
} }
for (PowerUp powerUp : powerUps) { for (PowerUp powerUp : powerUps) {
powerUp.render(); powerUp.render(sr);
} }
for (Ball ball : balls) { for (Ball ball : balls) {
ball.render(); ball.render(sr);
} }
paddle.render(); paddle.render(sr);
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; long finish_render = System.nanoTime() - start_render;
batch.begin(); batch.begin();
@ -143,6 +157,14 @@ public class BrickBuster extends ApplicationAdapter {
return null; return null;
} }
public void setShieldActive(boolean shieldActive) {
this.shieldActive = shieldActive;
}
public boolean isShieldActive() {
return shieldActive;
}
public ArrayList<Ball> getBalls() { public ArrayList<Ball> getBalls() {
return balls; return balls;
} }
@ -166,6 +188,7 @@ public class BrickBuster extends ApplicationAdapter {
tmp.put(GluePowerUp.class, 30); tmp.put(GluePowerUp.class, 30);
tmp.put(LongerPaddlePowerUp.class, 40); tmp.put(LongerPaddlePowerUp.class, 40);
tmp.put(MultiBallPowerUp.class, 20); tmp.put(MultiBallPowerUp.class, 20);
tmp.put(ShieldPowerUp.class, 40);
powerUpWeights = Collections.unmodifiableMap(tmp); powerUpWeights = Collections.unmodifiableMap(tmp);
int sum = 0; int sum = 0;

View File

@ -2,6 +2,7 @@ package com.me.brickbuster.entity;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; 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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@ -30,11 +31,11 @@ public class Ball extends Entity {
} }
@Override @Override
public void render() { public void render(ShapeRenderer sr) {
getShapeRenderer().begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
getShapeRenderer().setColor(BALL_COLOR); sr.setColor(BALL_COLOR);
getShapeRenderer().circle(getPos().x, getPos().y, RADIUS); sr.circle(getPos().x, getPos().y, RADIUS);
getShapeRenderer().end(); sr.end();
} }
@Override @Override
@ -75,12 +76,13 @@ public class Ball extends Entity {
Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); Utils.reflect(direction, BrickBuster.VERTICAL_EDGE);
} else if (new_pos.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 (new_pos.y - RADIUS < BrickBuster.SHIELD_HEIGHT && getBrickBuster().isShieldActive()) {
Utils.reflect(direction, BrickBuster.HORIZONTAL_EDGE);
getBrickBuster().setShieldActive(false);
} else if (new_pos.y + RADIUS < 0) { } else if (new_pos.y + RADIUS < 0) {
isDead = true; isDead = true;
return; return;
} } else if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) {
if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) {
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, new_pos.cpy()); Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy());

View File

@ -1,6 +1,7 @@
package com.me.brickbuster.entity; package com.me.brickbuster.entity;
import com.badlogic.gdx.graphics.Color; 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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
@ -28,11 +29,11 @@ public class Brick extends Entity {
} }
@Override @Override
public void render() { public void render(ShapeRenderer sr) {
getShapeRenderer().begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
getShapeRenderer().setColor(BLOCK_COLOR); sr.setColor(BLOCK_COLOR);
getShapeRenderer().rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT); sr.rect(getX(), getY(), BLOCK_WIDTH, BLOCK_HEIGHT);
getShapeRenderer().end(); sr.end();
} }
@Override @Override

View File

@ -6,17 +6,15 @@ import com.me.brickbuster.BrickBuster;
public abstract class Entity { public abstract class Entity {
private ShapeRenderer shapeRenderer;
private BrickBuster brickBuster; private BrickBuster brickBuster;
private Vector2 pos; private Vector2 pos;
public Entity(BrickBuster brickBuster, float x, float y) { public Entity(BrickBuster brickBuster, float x, float y) {
this.shapeRenderer = new ShapeRenderer();
this.brickBuster = brickBuster; this.brickBuster = brickBuster;
this.pos = new Vector2(x, y); this.pos = new Vector2(x, y);
} }
public abstract void render(); public abstract void render(ShapeRenderer sr);
public abstract void update(float dt); public abstract void update(float dt);
@ -24,10 +22,6 @@ public abstract class Entity {
return brickBuster; return brickBuster;
} }
public ShapeRenderer getShapeRenderer() {
return shapeRenderer;
}
public Vector2 getPos() { public Vector2 getPos() {
return pos; return pos;
} }

View File

@ -3,6 +3,7 @@ package com.me.brickbuster.entity;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color; 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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
@ -24,11 +25,11 @@ public class Paddle extends Entity {
} }
@Override @Override
public void render() { public void render(ShapeRenderer sr) {
getShapeRenderer().begin(ShapeType.Filled); sr.begin(ShapeType.Filled);
getShapeRenderer().setColor(sticky? Color.GRAY : PADDLE_COLOR); sr.setColor(sticky? Color.GRAY : PADDLE_COLOR);
getShapeRenderer().rect(getX() - width/2, getY(), width, PADDLE_HEIGHT); sr.rect(getX() - width/2, getY(), width, PADDLE_HEIGHT);
getShapeRenderer().end(); sr.end();
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.brickbuster.BrickBuster; import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.Utils; import com.me.brickbuster.Utils;
@ -23,11 +24,11 @@ public abstract class PowerUp extends Entity {
} }
@Override @Override
public void render() { public void render(ShapeRenderer sr) {
getShapeRenderer().begin(ShapeRenderer.ShapeType.Filled); sr.begin(ShapeType.Filled);
getShapeRenderer().setColor(color); sr.setColor(color);
getShapeRenderer().circle(getX(), getY(), RADIUS); sr.circle(getX(), getY(), RADIUS);
getShapeRenderer().end(); sr.end();
} }
@Override @Override

View File

@ -0,0 +1,18 @@
package com.me.brickbuster.entity.powerup;
import com.badlogic.gdx.graphics.Color;
import com.me.brickbuster.BrickBuster;
import com.me.brickbuster.entity.Brick;
public class ShieldPowerUp extends PowerUp {
public ShieldPowerUp(BrickBuster brickBuster, Brick brick) {
super(brickBuster, brick, Color.SALMON);
}
@Override
public void activate() {
getBrickBuster().setShieldActive(true);
}
}