Compare commits

...

2 Commits

3 changed files with 105 additions and 10 deletions

View File

@@ -30,10 +30,12 @@ public class GamePanel extends JPanel implements Runnable {
// SYSTEM
TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler();
Sound sound = new Sound();
Thread gameThread;
Sound se = new Sound();
Sound music = new Sound();
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);
@@ -98,22 +100,23 @@ 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();
}
public void playMusic(int i) {
sound.setFile(i);
sound.play();
sound.loop();
music.setFile(i);
music.play();
music.loop();
}
public void stopMusic() {
sound.stop();
music.stop();
}
public void playSE(int i) {
sound.setFile(i);
sound.play();
se.setFile(i);
se.play();
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}