Add MenuState, start with it and switch back when a game is finished
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								core/assets/playBtn.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								core/assets/playBtn.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 2.7 KiB  | 
@ -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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										49
									
								
								core/src/com/me/brickbuster/state/MenuState.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								core/src/com/me/brickbuster/state/MenuState.java
									
									
									
									
									
										Normal 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();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -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,8 +16,15 @@ public abstract class State extends ScreenAdapter {
 | 
			
		||||
	@Override
 | 
			
		||||
	public final void render(float delta) {
 | 
			
		||||
		update(delta);
 | 
			
		||||
		if (!disposed) {
 | 
			
		||||
			render();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Override
 | 
			
		||||
	public void dispose() {
 | 
			
		||||
		this.disposed = true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public abstract void setup();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user