Path Finder and moving NPC

This commit is contained in:
iryupnik 2020-03-19 16:33:54 +01:00
parent 1e1c685530
commit 7ea7fff949
5 changed files with 69 additions and 34 deletions

View File

@ -1,6 +1,5 @@
package ruscz.game.gameOfProd.Cells; package ruscz.game.gameOfProd.Cells;
import ruscz.game.gameOfProd.Cells.Cell;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
@ -35,9 +34,9 @@ public class EventMoveToCell extends Cell {
public void moveToMap() { public void moveToMap() {
//window.getMap().getPerson().stopPerson(); //window.getMap().getPerson().stopPerson();
window.getMap().getActualMap().setIdExit(id); window.getMapPainter().getActualMap().setIdExit(id);
window.setMap(this.map); window.setMapPainter(this.map);
window.getMap().getActualMap().setIdExit(id); window.getMapPainter().getActualMap().setIdExit(id);
window.paintMapOnWindow(); window.paintMapOnWindow();
window.repaintCanvas(); window.repaintCanvas();
window.setCanPersonMove(true); window.setCanPersonMove(true);
@ -46,7 +45,7 @@ public class EventMoveToCell extends Cell {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
g.drawImage(window.getMap().getActualMap().getImageCell().getImagesOfTiles().get(277), getX() * WindowOfFreeMove.CELL_SIZE, g.drawImage(window.getMapPainter().getActualMap().getImageCell().getImagesOfTiles().get(277), getX() * WindowOfFreeMove.CELL_SIZE,
getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE); g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE);
} }

View File

@ -1,18 +1,21 @@
package ruscz.game.gameOfProd.Cells; package ruscz.game.gameOfProd.Cells;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapPainter;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.awt.*; import java.awt.*;
public class Npc extends Person { public class Npc extends Person {
private int[] path; private int[] path;
private MapPainter mapPainter;
int x; int x;
int y; int y;
Color color; Color color;
public Npc(int x, int y, Map map) { public Npc(int x, int y, Map map, MapPainter mapPainter) {
super(x, y, map); super(x, y, map);
this.mapPainter = mapPainter;
this.color = Color.cyan; this.color = Color.cyan;
} }
@ -23,10 +26,14 @@ public class Npc extends Person {
path[0] = endX-x; path[0] = endX-x;
path[1] = endY-y; path[1] = endY-y;
for (int i = 0; i < Math.abs(path[0]); i++) { for (int i = 0; i < Math.abs(path[0]); i++) {
WindowOfFreeMove.sleep(500);
moveX(); moveX();
mapPainter.repaintCanvas();
} }
for (int i = 0; i < Math.abs(path[1]); i++) { for (int i = 0; i < Math.abs(path[1]); i++) {
WindowOfFreeMove.sleep(500);
moveY(); moveY();
mapPainter.repaintCanvas();
} }
} }
@ -43,7 +50,7 @@ public class Npc extends Person {
public void moveY() { public void moveY() {
if (path[1] > 0) { if (path[1] > 0) {
y--; y++;
set(x, y); set(x, y);
} }
else { else {

View File

@ -5,6 +5,7 @@ import ruscz.game.gameOfProd.Cells.EventMoveToCell;
import ruscz.game.gameOfProd.Cells.TypeOfCell; import ruscz.game.gameOfProd.Cells.TypeOfCell;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.awt.*;
import java.util.LinkedList; import java.util.LinkedList;
import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.*; import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.*;
@ -131,4 +132,8 @@ public class MapPainter {
} }
canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize); canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize);
} }
public void repaintCanvas() {
canvas.repaint();
}
} }

View File

@ -1,7 +1,6 @@
package ruscz.game.gameOfProd.Windows; package ruscz.game.gameOfProd.Windows;
import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Cells.*;
import ruscz.game.gameOfProd.Dialog.Dialog;
import ruscz.game.gameOfProd.Dialog.DialogBar; import ruscz.game.gameOfProd.Dialog.DialogBar;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapPainter; import ruscz.game.gameOfProd.Map.MapPainter;
@ -33,13 +32,13 @@ public class WindowOfFreeMove extends JFrame {
private Canvas canvas; private Canvas canvas;
private DialogBar dialogBar; private DialogBar dialogBar;
private MapPainter map; private MapPainter mapPainter;
private Timer inputTimer; private Timer inputTimer;
private LinkedList<Map> mapLinkedList; private LinkedList<Map> mapLinkedList;
public MapPainter getMap() { public MapPainter getMapPainter() {
return map; return mapPainter;
} }
public void repaintCanvas() { public void repaintCanvas() {
@ -50,12 +49,12 @@ public class WindowOfFreeMove extends JFrame {
WindowOfFreeMove.canPersonMove = canPersonMove; WindowOfFreeMove.canPersonMove = canPersonMove;
} }
public void setMap(Map map) { public void setMapPainter(Map mapPainter) {
this.map.setMap(map); this.mapPainter.setMap(mapPainter);
} }
public void paintMapOnWindow() { public void paintMapOnWindow() {
this.map.createMap(); this.mapPainter.createMap();
} }
public static void setNextMove(int nextMove) { public static void setNextMove(int nextMove) {
@ -91,8 +90,8 @@ public class WindowOfFreeMove extends JFrame {
inputTimer = new Timer(10, new ActionListener() { inputTimer = new Timer(10, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) { if (!mapPainter.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) {
map.getActualMap().getPerson().setDirectionMove(nextMove); mapPainter.getActualMap().getPerson().setDirectionMove(nextMove);
} }
} }
}); });
@ -100,8 +99,8 @@ public class WindowOfFreeMove extends JFrame {
addKeyListener(new KeyAdapter() { addKeyListener(new KeyAdapter() {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) { if (!mapPainter.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) {
map.getActualMap().getPerson().setDirectionMove(nextMove); mapPainter.getActualMap().getPerson().setDirectionMove(nextMove);
} }
} }
}); });
@ -183,28 +182,34 @@ public class WindowOfFreeMove extends JFrame {
m4.linkMaps(m4, this); m4.linkMaps(m4, this);
m5.linkMaps(m5, this); m5.linkMaps(m5, this);
Npc npc = new Npc(1,1,m5); mapPainter = new MapPainter(mapLinkedList, canvas);
Npc npc = new Npc(1,1, m5, mapPainter);
m5.setNpcs(npc); m5.setNpcs(npc);
map = new MapPainter(mapLinkedList, canvas); setMapPainter(m5);
setMap(m5); mapPainter.moveCamera();
mapPainter.createMap();
map.moveCamera();
map.createMap();
isReadyToPaint = true; isReadyToPaint = true;
canvas.repaint(); canvas.repaint();
Npc npc2 = new Npc(1,1, m1, mapPainter);
m1.setNpcs(npc2);
Timer timer = new Timer(10, new ActionListener() { Timer timer = new Timer(10, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
map.getActualMap().getPerson().move(); mapPainter.getActualMap().getPerson().move();
map.moveCamera(); mapPainter.moveCamera();
canvas.repaint(); canvas.repaint();
} }
}); });
timer.start(); timer.start();
while (true){
npc2.move(5,1);
npc2.move(1, 5);
}
} }
public class Canvas extends JPanel { public class Canvas extends JPanel {
@ -217,27 +222,27 @@ public class WindowOfFreeMove extends JFrame {
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < CANVAS_WIDTH; i++) { for (int i = 0; i < CANVAS_WIDTH; i++) {
for (int j = 0; j < CANVAS_HEIGHT; j++) { for (int j = 0; j < CANVAS_HEIGHT; j++) {
map.getActualMap().getImageCell().paintBackground(g2D, i, j); mapPainter.getActualMap().getImageCell().paintBackground(g2D, i, j);
} }
} }
for (Cell cell : map.getActualMap().getMoveToCells()) { for (Cell cell : mapPainter.getActualMap().getMoveToCells()) {
cell.paint(g2D); cell.paint(g2D);
} }
map.getActualMap().getPerson().paint(g2D); mapPainter.getActualMap().getPerson().paint(g2D);
for (Cell cell : map.getActualMap().getBlocks()) { for (Cell cell : mapPainter.getActualMap().getBlocks()) {
cell.paint(g2D); cell.paint(g2D);
} }
for (Cell cell : map.getActualMap().getInteractionEventCells()) { for (Cell cell : mapPainter.getActualMap().getInteractionEventCells()) {
cell.paint(g2D); cell.paint(g2D);
} }
for (Person person: map.getActualMap().getNpcs()){ for (Person person: mapPainter.getActualMap().getNpcs()){
person.paint(g2D); person.paint(g2D);
} }
} }
} }
} }
protected static void sleep(long tm) { public static void sleep(long tm) {
try { try {
Thread.sleep(tm); Thread.sleep(tm);
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -0,0 +1,19 @@
package ruscz.game.gameOfProd.scripts;
import ruscz.game.gameOfProd.Cells.*;
import ruscz.game.gameOfProd.Map.Map;
public class PathFinder {
Map map;
private int startX;
private int startY;
private int endX;
private int endY;
PathFinder(Map map, Npc npc, int endX, int endY){
this.map = map;
startX = npc.getX();
startY = npc.getY();
this.endX = endX;
this.endY = endY;
}
}