added gamestates and started pause screen
This commit is contained in:
@@ -31,7 +31,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
|
||||
// SYSTEM
|
||||
TileManager tileM = new TileManager(this);
|
||||
KeyHandler keyH = new KeyHandler();
|
||||
KeyHandler keyH = new KeyHandler(this);
|
||||
Sound se = new Sound();
|
||||
Sound music = new Sound();
|
||||
public CollisionHandler collisionH = new CollisionHandler(this);
|
||||
@@ -43,6 +43,9 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public Player player = new Player(this, keyH);
|
||||
public SuperObject[] obj = new SuperObject[10];
|
||||
|
||||
// GAME STATE
|
||||
public GameState gameState;
|
||||
|
||||
public GamePanel() {
|
||||
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
||||
this.setBackground(Color.black);
|
||||
@@ -54,6 +57,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public void setupGame() {
|
||||
assetSetter.setObject();
|
||||
playMusic(0); //Play main theme
|
||||
gameState = GameState.PLAY;
|
||||
}
|
||||
|
||||
public void startGameThread() {
|
||||
@@ -91,7 +95,13 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
}
|
||||
|
||||
public void update() {
|
||||
switch(gameState) {
|
||||
case PLAY:
|
||||
player.update();
|
||||
break;
|
||||
case PAUSE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics graphics) {
|
||||
|
||||
8
src/de/miaurizius/jgame2d/core/GameState.java
Normal file
8
src/de/miaurizius/jgame2d/core/GameState.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
|
||||
public enum GameState {
|
||||
|
||||
PLAY,
|
||||
PAUSE;
|
||||
|
||||
}
|
||||
@@ -6,9 +6,14 @@ import java.awt.event.KeyListener;
|
||||
public class KeyHandler implements KeyListener {
|
||||
|
||||
public boolean upPressed, downPressed, leftPressed, rightPressed;
|
||||
public GamePanel panel;
|
||||
// DEBUG
|
||||
public boolean checkDrawTime = false;
|
||||
|
||||
public KeyHandler(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
|
||||
@@ -16,11 +21,17 @@ public class KeyHandler implements KeyListener {
|
||||
public void keyPressed(KeyEvent e) {
|
||||
int code = e.getKeyCode();
|
||||
switch (code) {
|
||||
// CONTROLS
|
||||
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
|
||||
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true;
|
||||
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
|
||||
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true;
|
||||
|
||||
// DEBUG OPTIONS
|
||||
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
|
||||
|
||||
// GAME STATES
|
||||
case KeyEvent.VK_ESCAPE -> panel.gameState = (panel.gameState == GameState.PAUSE) ? GameState.PLAY : GameState.PAUSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,26 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
|
||||
import de.miaurizius.jgame2d.object.KeyObj;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class UI {
|
||||
|
||||
GamePanel panel;
|
||||
Graphics graphics2d;
|
||||
Font arial_40, arial_80B;
|
||||
BufferedImage keyImage;
|
||||
DecimalFormat df = new DecimalFormat("#0.00");
|
||||
|
||||
public boolean messageOn = false;
|
||||
public String message;
|
||||
int msgC = 0;
|
||||
public boolean gameFinished = false;
|
||||
|
||||
int msgC = 0;
|
||||
double playTime;
|
||||
DecimalFormat df = new DecimalFormat("#0.00");
|
||||
|
||||
public UI(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
arial_40 = new Font("Arial", Font.PLAIN, 40);
|
||||
arial_80B = new Font("Arial", Font.BOLD, 80);
|
||||
keyImage = new KeyObj(panel).image;
|
||||
}
|
||||
|
||||
public void showMessage(String text) {
|
||||
@@ -32,52 +29,28 @@ public class UI {
|
||||
}
|
||||
|
||||
public void draw(Graphics graphics2d) {
|
||||
if(gameFinished) {
|
||||
this.graphics2d = graphics2d;
|
||||
graphics2d.setFont(arial_40);
|
||||
graphics2d.setColor(Color.white);
|
||||
String text = "You found the treasure!";
|
||||
int textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
|
||||
int x,y;
|
||||
x = panel.screenWidth/2 - textLength/2;
|
||||
y = panel.screenHeight/2 - (panel.tileSize*3);
|
||||
graphics2d.drawString(text, x, y);
|
||||
|
||||
text = "Your time is: " + df.format(playTime) + "!";
|
||||
textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
|
||||
x = panel.screenWidth/2 - textLength/2;
|
||||
y = panel.screenHeight/2 + (panel.tileSize*4);
|
||||
graphics2d.drawString(text, x, y);
|
||||
switch (panel.gameState) {
|
||||
case PLAY:
|
||||
|
||||
graphics2d.setFont(arial_80B);
|
||||
graphics2d.setColor(Color.yellow);
|
||||
text = "Congratulations!";
|
||||
textLength = (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
|
||||
x = panel.screenWidth/2 - textLength/2;
|
||||
y = panel.screenHeight/2 + (panel.tileSize*2);
|
||||
graphics2d.drawString(text, x, y);
|
||||
|
||||
panel.gameThread = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// MESSAGE
|
||||
graphics2d.setFont(arial_40);
|
||||
graphics2d.setColor(Color.white);
|
||||
graphics2d.drawImage(keyImage, panel.tileSize/2, panel.tileSize/2, panel.tileSize, panel.tileSize, null);
|
||||
graphics2d.drawString("x " + panel.player.hasKey, 74, 65);
|
||||
|
||||
// TIME
|
||||
playTime += (double) 1/panel.FPS;
|
||||
graphics2d.drawString("Time: " + df.format(playTime), panel.tileSize*11, 65);
|
||||
|
||||
if(messageOn) {
|
||||
graphics2d.setFont(panel.getFont().deriveFont(30F));
|
||||
graphics2d.drawString(message, panel.tileSize/2, panel.tileSize*5);
|
||||
msgC++;
|
||||
if(msgC <= 120) return; //text stays 2 seconds
|
||||
msgC = 0;
|
||||
messageOn = false;
|
||||
break;
|
||||
case PAUSE:
|
||||
drawPauseScreen();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawPauseScreen() {
|
||||
String text = "PAUSED";
|
||||
int y = panel.screenHeight / 2;
|
||||
graphics2d.drawString(text, getCenteredX(text), y);
|
||||
}
|
||||
|
||||
public int getCenteredX(String text) {
|
||||
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user