154 lines
4.7 KiB
Java
154 lines
4.7 KiB
Java
package ruscz.game.gameOfProd.Cells;
|
|
|
|
import ruscz.game.gameOfProd.Map.Map;
|
|
import ruscz.game.gameOfProd.Map.MapManager;
|
|
import ruscz.game.gameOfProd.Scripts.Direction;
|
|
import ruscz.game.gameOfProd.Scripts.PathFinder;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.LinkedList;
|
|
import java.util.Scanner;
|
|
|
|
public class Npc extends Person {
|
|
private final MapManager mapManager;
|
|
private String name;
|
|
private int direction;
|
|
private int width;
|
|
private int height;
|
|
private int offsetX;
|
|
private int offsetY;
|
|
private int widthCoef;
|
|
private int heightCoef;
|
|
private String imageNpc;
|
|
private String battle;
|
|
private Timer timer;
|
|
|
|
public Npc(int x, int y, String name, Map map, MapManager mapManager) {
|
|
super(x, y, map);
|
|
this.name = name;
|
|
this.mapManager = mapManager;
|
|
loadNpc();
|
|
}
|
|
|
|
public Npc(int x, int y, Map map, MapManager mapManager) {
|
|
super(x, y, map);
|
|
this.mapManager = mapManager;
|
|
}
|
|
|
|
public void move(int endX, int endY) {
|
|
PathFinder pathFinder = new PathFinder(getMap());
|
|
LinkedList<Direction> directions = pathFinder.getWayWithCollision(getX(), getY(), endX, endY);
|
|
if (endX == mapManager.getActualMap().getPerson().getX() && endY == mapManager.getActualMap().getPerson().getY()) {
|
|
directions.removeLast();
|
|
}
|
|
final int[] i = {0};
|
|
timer = new Timer(500, actionEvent -> {
|
|
if (i[0] == directions.size()) stopNPCMove(endX, endY);
|
|
else {
|
|
switch (directions.get(i[0])) {
|
|
case UP:
|
|
moveUp();
|
|
break;
|
|
case DOWN:
|
|
moveDown();
|
|
break;
|
|
case LEFT:
|
|
moveLeft();
|
|
break;
|
|
case RIGHT:
|
|
moveRight();
|
|
break;
|
|
}
|
|
if (name != null && name.equals("monster")) {
|
|
getMap().getSoundManager().playSound("monstersound.wav", 0.0f);
|
|
}
|
|
}
|
|
mapManager.repaintCanvas();
|
|
i[0]++;
|
|
});
|
|
timer.start();
|
|
}
|
|
|
|
private void stopNPCMove(int endX, int endY) {
|
|
timer.stop();
|
|
if (battle != null && endX == mapManager.getActualMap().getPerson().getX() && endY == mapManager.getActualMap().getPerson().getY()) {
|
|
mapManager.getWindow().startBattle(battle);
|
|
}
|
|
}
|
|
|
|
private void moveUp() {
|
|
int y = getY() - 1;
|
|
set(getX(), y);
|
|
direction = 2;
|
|
}
|
|
|
|
private void moveDown() {
|
|
int y = getY() + 1;
|
|
set(getX(), y);
|
|
direction = 1;
|
|
}
|
|
|
|
private void moveLeft() {
|
|
int x = getX() - 1;
|
|
set(x, getY());
|
|
direction = 0;
|
|
}
|
|
|
|
private void moveRight() {
|
|
int x = getX() + 1;
|
|
set(x, getY());
|
|
direction = 3;
|
|
}
|
|
|
|
private void loadNpc() {
|
|
File dialogFile = new File("src/main/resources/characters/" + name + "/" + name + ".txt");
|
|
Scanner scanner = null;
|
|
try {
|
|
scanner = new Scanner(dialogFile);
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
scanner.nextLine();
|
|
String[] imageInfo = scanner.nextLine().split(" ");
|
|
width = Integer.parseInt(imageInfo[0]);
|
|
height = Integer.parseInt(imageInfo[1]);
|
|
direction = Integer.parseInt(imageInfo[2]);
|
|
offsetX = Integer.parseInt(imageInfo[3]);
|
|
offsetY = Integer.parseInt(imageInfo[4]);
|
|
widthCoef = Integer.parseInt(imageInfo[5]);
|
|
heightCoef = Integer.parseInt(imageInfo[6]);
|
|
imageNpc = scanner.nextLine();
|
|
if (scanner.hasNextLine()) battle = scanner.nextLine();
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics2D g) {
|
|
if (name != null) {
|
|
BufferedImage image = null;
|
|
try {
|
|
image = ImageIO.read(new File("src/main/resources/characters/" + name + "/" + imageNpc)).getSubimage(width * direction, 0, width, height);
|
|
g.drawImage(image, getX() * getSize() + (getSize() / 5) + offsetX,
|
|
getY() * getSize() - (getSize() * 3 / 5) + offsetY, getSize() * widthCoef / 5, getSize() * heightCoef / 2, null);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setDirection(int direction) {
|
|
this.direction = direction;
|
|
}
|
|
|
|
public String getName() { return name; }
|
|
|
|
public Timer getTimer() {
|
|
return timer;
|
|
}
|
|
}
|