From 219622ff53b2a65421c1d501090004536f0390e2 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Mon, 12 Nov 2018 21:11:40 +0400 Subject: [PATCH] Add shield powerup --- core/src/com/me/brickbuster/BrickBuster.java | 31 ++++++++++++++++--- core/src/com/me/brickbuster/entity/Ball.java | 7 +++-- .../entity/powerup/ShieldPowerUp.java | 18 +++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index 78e31b2..e08d99d 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -2,16 +2,15 @@ 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.GluePowerUp; -import com.me.brickbuster.entity.powerup.LongerPaddlePowerUp; -import com.me.brickbuster.entity.powerup.MultiBallPowerUp; -import com.me.brickbuster.entity.powerup.PowerUp; +import com.me.brickbuster.entity.powerup.*; import java.util.*; @@ -21,6 +20,8 @@ public class BrickBuster extends ApplicationAdapter { 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); @@ -29,16 +30,20 @@ public class BrickBuster extends ApplicationAdapter { private BitmapFont font; private SpriteBatch batch; + private ShapeRenderer sr; private Paddle paddle; private ArrayList balls; private ArrayList bricks; private ArrayList powerUps; + private boolean shieldActive = false; + @Override public void create () { font = new BitmapFont(); batch = new SpriteBatch(); + sr = new ShapeRenderer(); paddle = new Paddle(this); @@ -66,6 +71,7 @@ public class BrickBuster extends ApplicationAdapter { 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; @@ -81,6 +87,14 @@ public class BrickBuster extends ApplicationAdapter { 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(); @@ -143,6 +157,14 @@ public class BrickBuster extends ApplicationAdapter { return null; } + public void setShieldActive(boolean shieldActive) { + this.shieldActive = shieldActive; + } + + public boolean isShieldActive() { + return shieldActive; + } + public ArrayList getBalls() { return balls; } @@ -166,6 +188,7 @@ public class BrickBuster extends ApplicationAdapter { 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; diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 5bb4b40..59c9b28 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -75,12 +75,13 @@ public class Ball extends Entity { Utils.reflect(direction, BrickBuster.VERTICAL_EDGE); } else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) { 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) { isDead = true; return; - } - - if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) { + } else if (direction.y < 0 && new_pos.y <= Paddle.PADDLE_Y + Paddle.PADDLE_HEIGHT + RADIUS) { Pair paddle = getBrickBuster().getPaddle().getTopEdge(); Vector2 lineDir = paddle.getValue().sub(paddle.getKey()); Vector2 nearest = Utils.nearestPoint(paddle.getKey().cpy(), lineDir, new_pos.cpy()); diff --git a/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java new file mode 100644 index 0000000..eaf7697 --- /dev/null +++ b/core/src/com/me/brickbuster/entity/powerup/ShieldPowerUp.java @@ -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); + } + +}