2020-04-15 18:07:17 +02:00

172 lines
6.5 KiB
Java

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.Windows.WindowOfFreeMove;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
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;
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) {
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();
if (data.equals("<end>")) {
endEvent();
}
else if (data.contains("<image>")) {
String[] imageName = data.split(" ");
dialog.setImageDialog(new ImageDialog(imageName[1]));
readNextLine();
}
else if (data.contains("<cameraFree>")) {
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);
readNextLine();
}
else if (data.contains("<wait>")) {
return;
}
else if (data.contains("<dialog>")) {
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("<additem>")) {
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("<removeitem>")) {
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("<br/>"))
data = "<html>" + data + "</html>";
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);
}
}
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;
}
}