package ruscz.game.gameOfProd.Cells; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Image.ImageCell; import ruscz.game.gameOfProd.Image.ImageDialog; import ruscz.game.gameOfProd.Inventory.InventoryManager; import ruscz.game.gameOfProd.Inventory.Item; import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import javax.swing.*; import java.awt.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.concurrent.TimeUnit; public class InteractionCell extends Cell { protected String message; protected ruscz.game.gameOfProd.Dialog.Dialog dialog; protected Scanner scanner; private ImageCell imageCell; private WindowOfFreeMove window; protected boolean paint; private Timer timer; public InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window, boolean paint) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); this.message = message; this.dialog = dialog; this.imageCell = imageCell; this.window = window; this.paint = paint; } public void startEvent() { if (message != null) { window.setCanPersonMove(false); readDialogFile(message); } } public void readDialogFile(String name) { if (message.equals("error")) { final int[] i = {0}; timer = new Timer(1000, actionEvent -> { errorEvent(i[0] +1); i[0]++; }); timer.start(); return; } try { File dialogFile = new File("src/main/resources/dialogs/" + name + ".txt"); scanner = new Scanner(dialogFile); readNextLine(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public void readNextLine() { if (scanner.hasNextLine() && !dialog.getTimerIsRunning()) { String data = scanner.nextLine(); window.getCamera().setCameraFirstMove(false); if (data.equals("")) { endEvent(); } else if (data.contains("")) { String[] imageName = data.split(" "); if (!imageName[1].equals("null")) dialog.setImageDialog(new ImageDialog(imageName[1])); else { dialog.unsetImageDialog(); } readNextLine(); } else if (data.contains("")) { String[] cameraInfo = data.split(" "); window.getCamera().setCameraStart(); window.getCamera().setCameraBound(false); window.getCamera().setCameraEnd(Integer.parseInt(cameraInfo[1]), Integer.parseInt(cameraInfo[2])); window.getCamera().setCameraTime(Double.parseDouble(cameraInfo[3])); window.getCamera().setCameraStopped(false); if (!window.getCamera().getIsFirstMove()) window.getCamera().setCameraFirstMove(true); readNextLine(); } else if (data.contains("")) { window.getCamera().setCameraFirstMove(true); return; } else if (data.contains("")) { String[] dialogInfo = data.split(" "); switch (dialogInfo[1]) { case "show": readNextLine(); dialog.showText(); break; case "hide": dialog.hideText(); break; case "up": readNextLine(); window.getDialogBar().setBounds(0, 0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT / 4); break; case "down": readNextLine(); window.getDialogBar().setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT / 4); break; } } else if (data.contains("")) { String[] dialogInfo = data.split(" "); Item item; if (dialogInfo.length == 5) { item = new Item(dialogInfo[1], Integer.parseInt(dialogInfo[2]), Integer.parseInt(dialogInfo[3]), dialogInfo[4]); } else { item = new Item(dialogInfo[1], Integer.parseInt(dialogInfo[2]), dialogInfo[3]); } InventoryManager.addItem(item); String name = dialogInfo[1].replace("-", " "); name = name.substring(0,1).toUpperCase() + name.substring(1); data = name + " was added to your inventory."; dialog.hideText(); dialog.setText(data); dialog.showText(); } else if (data.contains("")) { String[] dialogInfo = data.split(" "); String name = dialogInfo[1].replace("-", " "); name = name.substring(0,1).toUpperCase() + name.substring(1); if(InventoryManager.isInInventory(InventoryManager.findItemByName(name))) { useItem(InventoryManager.findItemByName(name)); data = "You used " + name + "."; dialog.hideText(); dialog.setText(data); dialog.showText(); } else { data = "You need " + name + " to proceed."; dialog.hideText(); dialog.setText(data); dialog.showText(); } } else if (data.contains("")) { String[] imageName = data.split(" "); if (!imageName[1].equals("null")) { dialog.hideText(); window.getForegroundPanel().setImageForeground(imageName[1]); window.getForegroundPanel().setVisible(true); } else { window.getForegroundPanel().setVisible(false); } readNextLine(); } else { if (data.contains("
")) data = "" + data + ""; dialog.hideText(); dialog.setText(data); dialog.showText(); } } else { dialog.skipShowText(); } } public void endEvent() { if (dialog != null) { dialog.hideText(); dialog.setImageDialog(null); } if (!window.getCamera().getCameraBound()) { window.getCamera().setCameraStopped(false); window.getCamera().returnFreeCamera(); } else { Person.setInEvent(false); WindowOfFreeMove.setCanPersonMove(true); } } private void errorEvent(int i) { if (i == 1) { WindowOfFreeMove.setCanPersonMove(false); window.getForegroundPanel().setImageForeground("error"); window.getForegroundPanel().setVisible(true); window.repaintCanvas(); } else if (i == 3) { Map openspace = Map.findMapByName("openspace", window); window.getMapManager().setMap(openspace); openspace.getPerson().set(openspace.getSpawnPoint()[0], openspace.getSpawnPoint()[1]); openspace.getPerson().setDirectionSight(4); openspace.setIdExit(0); dialog.hideText(); dialog.setText("Shit.."); dialog.showText(); } else if (i == 5) { window.getForegroundPanel().setVisible(false); dialog.hideText(); } else if (i == 6) { dialog.setText("I have to take a break. It's lunch time anyway."); dialog.showText(); } else if (i == 10) { dialog.hideText(); WindowOfFreeMove.setCanPersonMove(true); } else if (i == 11) { window.getMusicManager().playMusic(); timer.stop(); } } public void useItem(Item item) { InventoryManager.removeItem(item); } @Override public void paint(Graphics2D g) { super.paint(g); if (paint) g.drawImage(imageCell.getImagesOfTiles().get(imageCell.getImageFromCellMap("EVENT")), getX() * getSize(), getY() * getSize(), getSize(), getSize(), null); } public ImageCell getImageCell() { return imageCell; } }