From 8f1a12e6e31a2e74dc524557700a7f69245c9460 Mon Sep 17 00:00:00 2001 From: musilto9 Date: Thu, 12 Mar 2020 20:25:56 +0100 Subject: [PATCH] added dialogues from external file --- src/main/java/Dialog.java | 15 ++++++++++- src/main/java/InteractionCell.java | 41 ++++++++++++++++++++++++++--- src/main/java/Person.java | 10 ++++--- src/main/java/WindowOfFreeMove.java | 4 +-- src/main/resources/dialog-test.txt | 4 +++ src/main/resources/first-dialog.txt | 7 +++++ 6 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/dialog-test.txt create mode 100644 src/main/resources/first-dialog.txt diff --git a/src/main/java/Dialog.java b/src/main/java/Dialog.java index 92ca33f..58faff4 100644 --- a/src/main/java/Dialog.java +++ b/src/main/java/Dialog.java @@ -26,8 +26,13 @@ public class Dialog { charNumber = 0; } + Dialog(DialogBar dialogBar) { + this.dialogBar = dialogBar; + charNumber = 0; + } - private JLabel prepareDialogBar(String string) { + + public JLabel prepareDialogBar(String string) { Font f1 = new Font("Times New Roman", Font.PLAIN, 20); JLabel text = new JLabel(); @@ -79,5 +84,13 @@ public class Dialog { dialogBar.repaint(); } + public void setText(String text) { + this.text = text; + this.textLength = text.length() + 1; + } + + public void setImageDialog(ImageDialog imageDialog) { + this.imageDialog = imageDialog; + } } diff --git a/src/main/java/InteractionCell.java b/src/main/java/InteractionCell.java index 5415186..b30d30b 100644 --- a/src/main/java/InteractionCell.java +++ b/src/main/java/InteractionCell.java @@ -1,16 +1,21 @@ import java.awt.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; public class InteractionCell extends Cell { private String message; private Dialog dialog; + private Scanner scanner; InteractionCell(){ super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); } - InteractionCell(int x, int y, String message) { + InteractionCell(int x, int y, String message, Dialog dialog) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); this.message = message; + this.dialog = dialog; } InteractionCell(int x, int y, Dialog dialog) { @@ -21,9 +26,36 @@ public class InteractionCell extends Cell { public void startEvent() { WindowOfFreeMove.setCanPersonMove(false); if (message != null) - System.out.println(message); - if (dialog != null) { - dialog.showText(); + readDialogFile(message); + } + + public void readDialogFile(String name) { + try { + File dialogFile = new File("src/main/resources/" + name + ".txt"); + scanner = new Scanner(dialogFile); + readNextLine(); + } catch (FileNotFoundException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + + public void readNextLine() { + if (scanner.hasNextLine()) { + String data = scanner.nextLine(); + if (data.equals("")) + endEvent(); + else if (data.contains("")) { + String[] imageName = data.split(" "); + dialog.setImageDialog(new ImageDialog(imageName[1])); + readNextLine(); + } + else { + dialog.hideText(); + dialog.setText(data); + dialog.showText(); + dialog.prepareDialogBar(data); + } } } @@ -31,6 +63,7 @@ public class InteractionCell extends Cell { if (dialog != null) dialog.hideText(); WindowOfFreeMove.setCanPersonMove(true); + Person.setInEvent(false); } @Override diff --git a/src/main/java/Person.java b/src/main/java/Person.java index a30a917..e6ca2ea 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -8,7 +8,7 @@ public class Person extends Cell { private int directionSight; private int directionMoveLast; private boolean isMoving; - private boolean isInEvent; + private static boolean isInEvent; private int size; public void setDirectionMoveLast(int directionMoveLast) { @@ -25,7 +25,6 @@ public class Person extends Cell { this.map = map; this.directionSight = 3; this.isMoving = false; - this.isInEvent = false; this.size = WindowOfFreeMove.CELL_SIZE; } @@ -82,8 +81,7 @@ public class Person extends Cell { map.findInteractionByXY(x, y, directionSight).startEvent(); } else if (isEventInFront(x,y,directionSight) && isInEvent) { - map.findInteractionByXY(x, y, directionSight).endEvent(); - isInEvent = false; + map.findInteractionByXY(x, y, directionSight).readNextLine(); } break; } @@ -219,4 +217,8 @@ public class Person extends Cell { public void setSize(int size) { this.size = size; } + + public static void setInEvent(boolean isInEvent) { + Person.isInEvent = isInEvent; + } } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index 7fd7d8f..ea9054e 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -143,8 +143,8 @@ public class WindowOfFreeMove extends JFrame { m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this)); m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this)); - m1.addInteractionEvent(new InteractionCell(3, 5, new Dialog("First line", dialogBar))); - m1.addInteractionEvent(new InteractionCell(5, 5, new Dialog("Second line", dialogBar, new ImageDialog("BaetoriceFace.png")))); + m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar))); + m1.addInteractionEvent(new InteractionCell(5,5,"dialog-test", new Dialog(dialogBar))); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this)); diff --git a/src/main/resources/dialog-test.txt b/src/main/resources/dialog-test.txt new file mode 100644 index 0000000..e00338c --- /dev/null +++ b/src/main/resources/dialog-test.txt @@ -0,0 +1,4 @@ + BaetoriceFace.png +Wow +to je úžasné + \ No newline at end of file diff --git a/src/main/resources/first-dialog.txt b/src/main/resources/first-dialog.txt new file mode 100644 index 0000000..4e23f91 --- /dev/null +++ b/src/main/resources/first-dialog.txt @@ -0,0 +1,7 @@ +Tento +dialog +je +na +několik +řádků + \ No newline at end of file