From a56d72105053dd2b83ad76495217237a1098f230 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 13 Nov 2018 15:57:44 +0400 Subject: [PATCH] Move away from using pixel coordinates to "board coordinates" Changed aspect ratio from 4:3 to 9:16 --- core/src/com/me/brickbuster/BrickBuster.java | 25 +++++++++++++++++-- core/src/com/me/brickbuster/entity/Ball.java | 6 ++--- .../src/com/me/brickbuster/entity/Paddle.java | 6 ++--- .../com/me/brickbuster/state/PlayState.java | 4 +-- .../brickbuster/desktop/DesktopLauncher.java | 5 ++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/core/src/com/me/brickbuster/BrickBuster.java b/core/src/com/me/brickbuster/BrickBuster.java index c271a62..ea0c39f 100644 --- a/core/src/com/me/brickbuster/BrickBuster.java +++ b/core/src/com/me/brickbuster/BrickBuster.java @@ -3,16 +3,23 @@ package com.me.brickbuster; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.OrthographicCamera; 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.utils.viewport.StretchViewport; +import com.badlogic.gdx.utils.viewport.Viewport; import com.me.brickbuster.state.PlayState; public class BrickBuster extends Game { - public static final int WIDTH = 800; - public static final int HEIGHT = 600; public static final String TITLE = "Brick Buster"; + // 9*16 board area + public static final int BOARD_WIDTH = 2250; + public static final int BOARD_HEIGHT = 4000; + + public OrthographicCamera cam; + public Viewport viewport; public BitmapFont font; public SpriteBatch sb; @@ -20,6 +27,10 @@ public class BrickBuster extends Game { @Override public void create () { + cam = new OrthographicCamera(); + viewport = new StretchViewport(BOARD_WIDTH, BOARD_HEIGHT, cam); + viewport.apply(true); + font = new BitmapFont(); sb = new SpriteBatch(); sr = new ShapeRenderer(); @@ -35,4 +46,14 @@ public class BrickBuster extends Game { super.render(); } + @Override + public void resize(int width, int height) { + viewport.update(width, height); + + sb.setProjectionMatrix(cam.combined); + sr.setProjectionMatrix(cam.combined); + + super.resize(width, height); + } + } diff --git a/core/src/com/me/brickbuster/entity/Ball.java b/core/src/com/me/brickbuster/entity/Ball.java index 74ce2a6..c48a6dd 100644 --- a/core/src/com/me/brickbuster/entity/Ball.java +++ b/core/src/com/me/brickbuster/entity/Ball.java @@ -27,7 +27,7 @@ public class Ball extends Entity { private boolean isDead = false; public Ball(PlayState state) { - super(state,BrickBuster.WIDTH/2, state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); + super(state,BrickBuster.BOARD_WIDTH /2, state.paddle.getY() + Paddle.PADDLE_HEIGHT + RADIUS); this.speed = state.bricks.size() > BLOCKS_FOR_BOOST? DEFAULT_SPEED : BOOST_SPEED; } @@ -73,9 +73,9 @@ public class Ball extends Entity { } } - if (new_pos.x + RADIUS > BrickBuster.WIDTH || new_pos.x - RADIUS < 0) { + if (new_pos.x + RADIUS > BrickBuster.BOARD_WIDTH || new_pos.x - RADIUS < 0) { Utils.reflect(direction, Utils.VERTICAL_EDGE); - } else if (new_pos.y + RADIUS > BrickBuster.HEIGHT) { + } else if (new_pos.y + RADIUS > BrickBuster.BOARD_HEIGHT) { Utils.reflect(direction, Utils.HORIZONTAL_EDGE); } else if (state.getShieldCount() > 0 && new_pos.y - RADIUS < PlayState.SHIELD_HEIGHT * state.getShieldCount()) { diff --git a/core/src/com/me/brickbuster/entity/Paddle.java b/core/src/com/me/brickbuster/entity/Paddle.java index 6a6d3c1..510e86b 100644 --- a/core/src/com/me/brickbuster/entity/Paddle.java +++ b/core/src/com/me/brickbuster/entity/Paddle.java @@ -22,7 +22,7 @@ public class Paddle extends Entity { private boolean sticky = false; public Paddle(PlayState brickBuster) { - super(brickBuster, BrickBuster.WIDTH / 2, PADDLE_Y); + super(brickBuster, BrickBuster.BOARD_WIDTH / 2, PADDLE_Y); } @Override @@ -49,8 +49,8 @@ public class Paddle extends Entity { } } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { - if (pos.x + width/2 + PADDLE_SPEED * dt > BrickBuster.WIDTH) { - setX(BrickBuster.WIDTH - width/2); + if (pos.x + width/2 + PADDLE_SPEED * dt > BrickBuster.BOARD_WIDTH) { + setX(BrickBuster.BOARD_WIDTH - width/2); return; } pos.x = pos.x + PADDLE_SPEED * dt; diff --git a/core/src/com/me/brickbuster/state/PlayState.java b/core/src/com/me/brickbuster/state/PlayState.java index 202fbd8..0dcf030 100644 --- a/core/src/com/me/brickbuster/state/PlayState.java +++ b/core/src/com/me/brickbuster/state/PlayState.java @@ -47,7 +47,7 @@ public class PlayState extends State { if (MathUtils.randomBoolean(POWERUP_CHANCE)) { powerUpType = getWeightedPowerUp(); } - bricks.add(new Brick(this, powerUpType, x, BrickBuster.HEIGHT - y)); + bricks.add(new Brick(this, powerUpType, x, BrickBuster.BOARD_HEIGHT - y)); } } @@ -72,7 +72,7 @@ public class PlayState extends State { if (getShieldCount() > 0) { game.sr.begin(ShapeType.Filled); game.sr.setColor(Color.SALMON); - game.sr.rect(0, 0, BrickBuster.WIDTH, getShieldCount() * SHIELD_HEIGHT); + game.sr.rect(0, 0, BrickBuster.BOARD_WIDTH, getShieldCount() * SHIELD_HEIGHT); game.sr.end(); } long renderTime = System.nanoTime() - start; diff --git a/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java b/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java index 0afce69..767e6ae 100644 --- a/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java +++ b/desktop/src/com/me/brickbuster/desktop/DesktopLauncher.java @@ -7,9 +7,10 @@ import com.me.brickbuster.BrickBuster; public class DesktopLauncher { public static void main (String[] arg) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); - config.width = BrickBuster.WIDTH; - config.height = BrickBuster.HEIGHT; + config.width = 450; + config.height = 800; config.title = BrickBuster.TITLE; new LwjglApplication(new BrickBuster(), config); + } }