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;
import ruscz.game.gameOfProd.Cells.Cell;
import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
@ -35,9 +34,9 @@ public class EventMoveToCell extends Cell {
public void moveToMap() {
//window.getMap().getPerson().stopPerson();
window.getMap().getActualMap().setIdExit(id);
window.setMap(this.map);
window.getMap().getActualMap().setIdExit(id);
window.getMapPainter().getActualMap().setIdExit(id);
window.setMapPainter(this.map);
window.getMapPainter().getActualMap().setIdExit(id);
window.paintMapOnWindow();
window.repaintCanvas();
window.setCanPersonMove(true);
@ -46,7 +45,7 @@ public class EventMoveToCell extends Cell {
@Override
public void paint(Graphics2D 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);
g.drawString(Integer.toString(getId()), getX()* WindowOfFreeMove.CELL_SIZE, getY()* WindowOfFreeMove.CELL_SIZE);
}

View File

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

View File

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

View File

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