diff --git a/src/de/miaurizius/jgame2d/core/GamePanel.java b/src/de/miaurizius/jgame2d/core/GamePanel.java index 639c9c6..4211182 100644 --- a/src/de/miaurizius/jgame2d/core/GamePanel.java +++ b/src/de/miaurizius/jgame2d/core/GamePanel.java @@ -32,9 +32,10 @@ public class GamePanel extends JPanel implements Runnable { KeyHandler keyH = new KeyHandler(); Sound se = new Sound(); Sound music = new Sound(); - Thread gameThread; public CollisionHandler collisionH = new CollisionHandler(this); - public AssetSetter assetSetter = new AssetSetter(this);; + public AssetSetter assetSetter = new AssetSetter(this); + public UI ui = new UI(this); + Thread gameThread; // ENTITY AND OBJECT public Player player = new Player(this, keyH); @@ -99,6 +100,7 @@ public class GamePanel extends JPanel implements Runnable { tileM.draw(graphics2d); for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this); player.draw(graphics2d); + ui.draw(graphics2d); graphics.dispose(); } diff --git a/src/de/miaurizius/jgame2d/core/UI.java b/src/de/miaurizius/jgame2d/core/UI.java new file mode 100644 index 0000000..9b5313e --- /dev/null +++ b/src/de/miaurizius/jgame2d/core/UI.java @@ -0,0 +1,83 @@ +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; + Font arial_40, arial_80B; + BufferedImage keyImage; + public boolean messageOn = false; + public String message; + int msgC = 0; + public boolean gameFinished = false; + + 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().image; + } + + public void showMessage(String text) { + message = text; + messageOn = true; + } + + public void draw(Graphics graphics2d) { + if(gameFinished) { + 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); + + 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; + } + } + +} diff --git a/src/de/miaurizius/jgame2d/entity/Player.java b/src/de/miaurizius/jgame2d/entity/Player.java index b71a518..9ac7ecf 100644 --- a/src/de/miaurizius/jgame2d/entity/Player.java +++ b/src/de/miaurizius/jgame2d/entity/Player.java @@ -16,7 +16,7 @@ public class Player extends Entity { public final int screenX; public final int screenY; - int hasKey = 0; + public int hasKey = 0; public Player(GamePanel panel, KeyHandler keyH) { this.panel = panel; @@ -98,18 +98,27 @@ public class Player extends Entity { panel.playSE(1); hasKey++; panel.obj[index] = null; + panel.ui.showMessage("You got a key!"); break; case "door": if(hasKey > 0) { panel.playSE(3); panel.obj[index] = null; hasKey--; + panel.ui.showMessage("You opened the door!"); } + else panel.ui.showMessage("You need a key!"); break; case "boots": panel.playSE(2); speed += 1; panel.obj[index] = null; + panel.ui.showMessage("Speed up!"); + break; + case "chest": + panel.ui.gameFinished = true; + panel.stopMusic(); + panel.playSE(4); break; } }