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 ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Npc extends Person { private 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; 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()); for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){ WindowOfFreeMove.sleep(500); switch (direction){ case UP: moveUp(); break; case DOWN: moveDown(); break; case LEFT: moveLeft(); break; case RIGHT: moveRight(); break; } mapManager.repaintCanvas(); } } private void moveUp() { int y = getY() - 1; set(getX(), y); } private void moveDown() { int y = getY() + 1; set(getX(), y); } private void moveLeft() { int x = getX() - 1; set(x, getY()); } private void moveRight() { int x = getX() + 1; set(x, getY()); } private void loadNpc() { File dialogFile = new File("src/main/resources/characters/" + 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(); } @Override public void paint(Graphics2D g) { if (name != null) { BufferedImage image = null; try { image = ImageIO.read(new File("src/main/resources/characters/" + 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; } }