Compare commits
5 Commits
1f614916b5
...
88c6f4be72
| Author | SHA1 | Date | |
|---|---|---|---|
|
88c6f4be72
|
|||
|
9950053bcd
|
|||
|
4a9eefa416
|
|||
|
dada9e4f2f
|
|||
|
61a51f184c
|
@@ -8,3 +8,6 @@ _To be continued_
|
|||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
_To be continued_
|
_To be continued_
|
||||||
|
|
||||||
|
## Note (for me)
|
||||||
|
- [Changing font](https://www.youtube.com/watch?v=g-wrebFVP3E)
|
||||||
@@ -37,7 +37,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
|
|
||||||
// SYSTEM
|
// SYSTEM
|
||||||
public TileManager tileM = new TileManager(this);
|
public TileManager tileM = new TileManager(this);
|
||||||
KeyHandler keyH = new KeyHandler(this);
|
public KeyHandler keyH = new KeyHandler(this);
|
||||||
Sound se = new Sound();
|
Sound se = new Sound();
|
||||||
Sound music = new Sound();
|
Sound music = new Sound();
|
||||||
public CollisionHandler collisionH = new CollisionHandler(this);
|
public CollisionHandler collisionH = new CollisionHandler(this);
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import java.awt.*;
|
|||||||
public class UI {
|
public class UI {
|
||||||
|
|
||||||
GamePanel panel;
|
GamePanel panel;
|
||||||
Graphics graphics2d;
|
Graphics2D graphics2d;
|
||||||
Font arial_40, arial_80B;
|
Font arial_40, arial_80B;
|
||||||
|
public String currentDialogue;
|
||||||
|
|
||||||
public UI(GamePanel panel) {
|
public UI(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
@@ -16,7 +17,7 @@ public class UI {
|
|||||||
arial_80B = new Font("Arial", Font.BOLD, 80);
|
arial_80B = new Font("Arial", Font.BOLD, 80);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics graphics2d) {
|
public void draw(Graphics2D graphics2d) {
|
||||||
this.graphics2d = graphics2d;
|
this.graphics2d = graphics2d;
|
||||||
graphics2d.setFont(arial_40);
|
graphics2d.setFont(arial_40);
|
||||||
graphics2d.setColor(Color.white);
|
graphics2d.setColor(Color.white);
|
||||||
@@ -29,6 +30,9 @@ public class UI {
|
|||||||
case GameState.PAUSE:
|
case GameState.PAUSE:
|
||||||
drawPauseScreen();
|
drawPauseScreen();
|
||||||
break;
|
break;
|
||||||
|
case GameState.DIALOGUE:
|
||||||
|
drawDialogueScreen();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +43,33 @@ public class UI {
|
|||||||
graphics2d.drawString(text, getCenteredX(text), y);
|
graphics2d.drawString(text, getCenteredX(text), y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void drawDialogueScreen() {
|
||||||
|
// WINDOW
|
||||||
|
int x = panel.tileSize*2;
|
||||||
|
int y = panel.tileSize/2;
|
||||||
|
int width = panel.screenWidth - (panel.tileSize*4);
|
||||||
|
int height = panel.tileSize*4;
|
||||||
|
drawSubWindow(x, y, width, height);
|
||||||
|
|
||||||
|
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 28F));
|
||||||
|
x += panel.tileSize;
|
||||||
|
y += panel.tileSize;
|
||||||
|
|
||||||
|
for(String line : currentDialogue.split("\n")) {
|
||||||
|
graphics2d.drawString(line, x, y);
|
||||||
|
y += 40;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawSubWindow(int x, int y, int width, int height) {
|
||||||
|
graphics2d.setColor(new Color(0,0,0,210));
|
||||||
|
graphics2d.fillRoundRect(x, y, width, height, 35, 35);
|
||||||
|
graphics2d.setColor(new Color(255,255,255));
|
||||||
|
graphics2d.setStroke(new BasicStroke(5));
|
||||||
|
graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getCenteredX(String text) {
|
public int getCenteredX(String text) {
|
||||||
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
|
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package de.miaurizius.jgame2d.core.enums;
|
|||||||
public enum GameState {
|
public enum GameState {
|
||||||
|
|
||||||
PLAY,
|
PLAY,
|
||||||
PAUSE;
|
PAUSE,
|
||||||
|
DIALOGUE,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import java.awt.event.KeyListener;
|
|||||||
|
|
||||||
public class KeyHandler implements KeyListener {
|
public class KeyHandler implements KeyListener {
|
||||||
|
|
||||||
public boolean upPressed, downPressed, leftPressed, rightPressed;
|
public boolean upPressed, downPressed, leftPressed, rightPressed, spacePressed;
|
||||||
public GamePanel panel;
|
public GamePanel panel;
|
||||||
// DEBUG
|
// DEBUG
|
||||||
public boolean checkDrawTime = false;
|
public boolean checkDrawTime = false;
|
||||||
@@ -23,21 +23,40 @@ public class KeyHandler implements KeyListener {
|
|||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
int code = e.getKeyCode();
|
int code = e.getKeyCode();
|
||||||
switch (code) {
|
switch(panel.gameState) {
|
||||||
// CONTROLS
|
case PLAY:
|
||||||
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
|
switch (code) {
|
||||||
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = true;
|
// CONTROLS
|
||||||
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
|
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
|
||||||
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = 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;
|
||||||
|
case KeyEvent.VK_SPACE -> spacePressed = true;
|
||||||
|
|
||||||
// DEBUG OPTIONS
|
// DEBUG OPTIONS
|
||||||
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
|
case KeyEvent.VK_T -> checkDrawTime = !checkDrawTime;
|
||||||
|
|
||||||
// GAME STATES
|
// GAME STATES
|
||||||
case KeyEvent.VK_ESCAPE -> panel.gameState = (panel.gameState == GameState.PAUSE) ? GameState.PLAY : GameState.PAUSE;
|
case KeyEvent.VK_ESCAPE -> panel.gameState = GameState.PAUSE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DIALOGUE:
|
||||||
|
switch (code) {
|
||||||
|
case KeyEvent.VK_SPACE -> panel.gameState = GameState.PLAY;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// TODO: setup keybinds that will always work (like pausing the game)
|
||||||
|
// public void alwaysOnKeys(int code) {
|
||||||
|
// // GAME STATES
|
||||||
|
// switch (code) {
|
||||||
|
// case KeyEvent.VK_ESCAPE -> panel.gameState = GameState.PAUSE;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
int code = e.getKeyCode();
|
int code = e.getKeyCode();
|
||||||
|
|||||||
@@ -25,12 +25,28 @@ public class Entity {
|
|||||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||||
public boolean collisionOn = false;
|
public boolean collisionOn = false;
|
||||||
public int actionLock = 0;
|
public int actionLock = 0;
|
||||||
|
String[] dialogue = new String[20];
|
||||||
|
int dialogueIndex = 0;
|
||||||
|
|
||||||
public Entity(GamePanel panel) {
|
public Entity(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAction() {}
|
public void setAction() {}
|
||||||
|
|
||||||
|
public void speak() {
|
||||||
|
if(dialogue[dialogueIndex] == null) dialogueIndex = 0;
|
||||||
|
panel.ui.currentDialogue = dialogue[dialogueIndex];
|
||||||
|
dialogueIndex++;
|
||||||
|
|
||||||
|
switch(panel.player.direction) {
|
||||||
|
case UP -> direction = Direction.DOWN;
|
||||||
|
case DOWN -> direction = Direction.UP;
|
||||||
|
case LEFT -> direction = Direction.RIGHT;
|
||||||
|
case RIGHT -> direction = Direction.LEFT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
setAction();
|
setAction();
|
||||||
collisionOn = false;
|
collisionOn = false;
|
||||||
@@ -52,6 +68,7 @@ public class Entity {
|
|||||||
if(spriteNum == 1) spriteNum = 2;
|
if(spriteNum == 1) spriteNum = 2;
|
||||||
else if(spriteNum == 2) spriteNum = 1;
|
else if(spriteNum == 2) spriteNum = 1;
|
||||||
else spriteNum = 0;
|
else spriteNum = 0;
|
||||||
|
spriteCounter = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,17 +81,20 @@ public class Entity {
|
|||||||
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
|
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
|
||||||
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
|
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
|
||||||
) {
|
) {
|
||||||
BufferedImage image = switch (direction) {
|
graphics2d.drawImage(parseSprite(), screenX, screenY, panel.tileSize, panel.tileSize, null);
|
||||||
case UP -> (spriteNum == 1) ? up1 : up2;
|
|
||||||
case DOWN -> (spriteNum == 1) ? down1 : down2;
|
|
||||||
case LEFT -> (spriteNum == 1) ? left1 : left2;
|
|
||||||
case RIGHT -> (spriteNum == 1) ? right1 : right2;
|
|
||||||
};
|
|
||||||
graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage initEntitySprites(String name) {
|
BufferedImage parseSprite() {
|
||||||
|
return switch (direction) {
|
||||||
|
case UP -> (spriteNum == 1) ? up1 : up2;
|
||||||
|
case DOWN -> (spriteNum == 1) ? down1 : down2;
|
||||||
|
case LEFT -> (spriteNum == 1) ? left1 : left2;
|
||||||
|
case RIGHT -> (spriteNum == 1) ? right1 : right2;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedImage initEntitySprites(String name) {
|
||||||
try {
|
try {
|
||||||
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);
|
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public class OldManNPC extends Entity {
|
|||||||
direction = Direction.DOWN;
|
direction = Direction.DOWN;
|
||||||
speed = 1;
|
speed = 1;
|
||||||
getImage();
|
getImage();
|
||||||
|
setDialogue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getImage() {
|
public void getImage() {
|
||||||
@@ -26,6 +27,13 @@ public class OldManNPC extends Entity {
|
|||||||
right2 = initEntitySprites("npc/oldman_right_2");
|
right2 = initEntitySprites("npc/oldman_right_2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDialogue() {
|
||||||
|
dialogue[0] = "Hello, lad.";
|
||||||
|
dialogue[1] = "So you've come to this island to \nfind the treasure?";
|
||||||
|
dialogue[2] = "I used to be a great wizard but now... \nI'm a bit too old for taking an \nadventure";
|
||||||
|
dialogue[3] = "Well, good luck on you.";
|
||||||
|
}
|
||||||
|
|
||||||
public void setAction() {
|
public void setAction() {
|
||||||
actionLock++;
|
actionLock++;
|
||||||
if(actionLock != 120) return; //lock action for x frames
|
if(actionLock != 120) return; //lock action for x frames
|
||||||
@@ -36,7 +44,5 @@ public class OldManNPC extends Entity {
|
|||||||
if(i > 50 && i <= 75) direction = Direction.LEFT;
|
if(i > 50 && i <= 75) direction = Direction.LEFT;
|
||||||
if(i > 75) direction = Direction.RIGHT;
|
if(i > 75) direction = Direction.RIGHT;
|
||||||
actionLock = 0;
|
actionLock = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package de.miaurizius.jgame2d.entity;
|
|||||||
|
|
||||||
import de.miaurizius.jgame2d.core.*;
|
import de.miaurizius.jgame2d.core.*;
|
||||||
import de.miaurizius.jgame2d.core.enums.Direction;
|
import de.miaurizius.jgame2d.core.enums.Direction;
|
||||||
|
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||||
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
|
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
public class Player extends Entity {
|
public class Player extends Entity {
|
||||||
|
|
||||||
@@ -85,28 +85,30 @@ public class Player extends Entity {
|
|||||||
if(spriteNum == 1) spriteNum = 2;
|
if(spriteNum == 1) spriteNum = 2;
|
||||||
else if(spriteNum == 2) spriteNum = 1;
|
else if(spriteNum == 2) spriteNum = 1;
|
||||||
else spriteNum = 0;
|
else spriteNum = 0;
|
||||||
|
spriteCounter = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pickObject(int index) {
|
public void pickObject(int index) {
|
||||||
if(index == 999) return;
|
if(index == 999) return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interactNPC(int index) {
|
public void interactNPC(int index) {
|
||||||
if(index == 999) return;
|
if(index == 999) return;
|
||||||
System.out.println("npc collision detected");
|
//if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space
|
||||||
|
panel.gameState = GameState.DIALOGUE;
|
||||||
|
panel.npc[index].speak();
|
||||||
|
panel.keyH.spacePressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics2D graphics2d) {
|
public void draw(Graphics2D graphics2d) {
|
||||||
BufferedImage image = switch (direction) {
|
graphics2d.drawImage(parseSprite(), screenX, screenY, null);
|
||||||
case UP -> (spriteNum == 1) ? up1 : up2;
|
}
|
||||||
case DOWN -> (spriteNum == 1) ? down1 : down2;
|
|
||||||
case LEFT -> (spriteNum == 1) ? left1 : left2;
|
public void speak() {
|
||||||
case RIGHT -> (spriteNum == 1) ? right1 : right2;
|
//This method only exists for character specific things later...
|
||||||
};
|
super.speak();
|
||||||
graphics2d.drawImage(image, screenX, screenY, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user