Add MenuState, start with it and switch back when a game is finished

This commit is contained in:
Matt Low 2018-11-13 17:58:42 +04:00
parent fa7e93953b
commit 0abae880d3
5 changed files with 75 additions and 5 deletions

BIN
core/assets/playBtn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -10,7 +10,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.me.brickbuster.state.PlayState;
import com.me.brickbuster.state.MenuState;
public class BrickBuster extends Game {
@ -39,7 +39,7 @@ public class BrickBuster extends Game {
sb = new SpriteBatch();
sr = new ShapeRenderer();
setScreen(new PlayState(this));
setScreen(new MenuState(this));
}
@Override

View File

@ -0,0 +1,49 @@
package com.me.brickbuster.state;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector3;
import com.me.brickbuster.BrickBuster;
public class MenuState extends State {
private Texture playButton;
public MenuState(BrickBuster game) {
super(game);
}
@Override
public void setup() {
this.playButton = new Texture(Gdx.files.internal("playBtn.png"));
}
@Override
public void render() {
game.sb.begin();
game.sb.draw(playButton,
BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2,
BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2);
game.sb.end();
}
@Override
public void update(float dt) {
if (Gdx.input.justTouched()) {
Vector3 clickLoc = game.cam.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
if (clickLoc.x >= BrickBuster.BOARD_WIDTH/2-playButton.getWidth()/2
&& clickLoc.x <= BrickBuster.BOARD_WIDTH/2+playButton.getWidth()/2
&& clickLoc.y >= BrickBuster.BOARD_HEIGHT/2-playButton.getHeight()/2
&& clickLoc.y <= BrickBuster.BOARD_HEIGHT/2+playButton.getHeight()/2) {
game.setScreen(new PlayState(game));
dispose();
}
}
}
@Override
public void dispose() {
super.dispose();
playButton.dispose();
}
}

View File

@ -115,12 +115,25 @@ public class PlayState extends State {
}
if (bricks.isEmpty()) {
// TODO: Fix this - go to an after-game menu
//create();
game.setScreen(new MenuState(game));
dispose();
}
updateTime = System.nanoTime() - start;
}
@Override
public void dispose() {
super.dispose();
powerUps.clear();
powerUps = null;
balls.clear();
balls = null;
bricks.clear();
bricks = null;
paddle = null;
}
public int getShieldCount() {
return shieldCount;
}

View File

@ -6,6 +6,7 @@ import com.me.brickbuster.BrickBuster;
public abstract class State extends ScreenAdapter {
protected final BrickBuster game;
private boolean disposed;
public State(BrickBuster game) {
this.game = game;
@ -15,7 +16,14 @@ public abstract class State extends ScreenAdapter {
@Override
public final void render(float delta) {
update(delta);
render();
if (!disposed) {
render();
}
}
@Override
public void dispose() {
this.disposed = true;
}
public abstract void setup();