From 555b70e2f0607ca6f5ee9c330fdae3c9f1d05012 Mon Sep 17 00:00:00 2001 From: iryupnik Date: Fri, 13 Mar 2020 23:15:33 +0100 Subject: [PATCH] Add MapPainter --- .../gameOfProd/Cells/EventMoveToCell.java | 6 +- .../java/ruscz/game/gameOfProd/Map/Map.java | 54 ----------- .../ruscz/game/gameOfProd/Map/MapPainter.java | 90 +++++++++++++++++++ .../gameOfProd/Windows/WindowOfFreeMove.java | 38 +++++--- src/main/resources/second-dialog.txt | 6 +- 5 files changed, 120 insertions(+), 74 deletions(-) create mode 100644 src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java index 3896fc9..23b99d6 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/EventMoveToCell.java @@ -42,9 +42,9 @@ public class EventMoveToCell extends Cell { public void moveToMap() { //window.getMap().getPerson().stopPerson(); - window.getMap().setIdExit(id); + window.getMap().getActualMap().setIdExit(id); window.setMap(this.map); - window.getMap().setIdExit(id); + window.getMap().getActualMap().setIdExit(id); window.paintMapOnWindow(); window.repaintCanvas(); window.setCanPersonMove(true); @@ -53,7 +53,7 @@ public class EventMoveToCell extends Cell { @Override public void paint(Graphics2D g) { super.paint(g); - g.drawImage(window.getMap().getImageCell().getImagesOfTiles().get(277), getX() * WindowOfFreeMove.CELL_SIZE, + g.drawImage(window.getMap().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); } diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index 9ce3e55..8850ac7 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -135,60 +135,6 @@ public class Map { public TypeOfCell[][] getMapArray() { return mapArray; } - public void createMap() { - for (int i = 0; i < mapArray.length; i++) { - for (int j = 0; j < mapArray.length; j++) { - if (mapArray[i][j] == TypeOfCell.BLOCK && !isBusyCell(i,j)) { - block.add(new Block(i, j, imageCell)); - } - - if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) { - person.set(i, j); - } - } - } - setPersonByMoveToEvent(); - } - - private void setPersonByMoveToEvent() { - for (EventMoveToCell cell : moveToCells) { - if (this.idExit == cell.getId()) { - switch (cell.getDirectionOut()) { - case 1: - person.set(cell.getX(), cell.getY() - 1); - person.setIsMoving(true); - person.setDirectionMoveLast(WindowOfFreeMove.KEY_UP); - person.setSize(WindowOfFreeMove.CELL_SIZE); - person.setDirectionSight(2); - break; - case 2: - person.set(cell.getX() + 1, cell.getY()); - person.setIsMoving(true); - person.setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT); - person.setSize(WindowOfFreeMove.CELL_SIZE); - person.setDirectionSight(3); - break; - case 3: - person.set(cell.getX(), cell.getY() + 1); - person.setIsMoving(true); - person.setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN); - person.setSize(WindowOfFreeMove.CELL_SIZE); - person.setDirectionSight(0); - break; - case 4: - person.set(cell.getX() - 1, cell.getY()); - person.setIsMoving(true); - person.setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT); - person.setSize(WindowOfFreeMove.CELL_SIZE); - person.setDirectionSight(1); - break; - default: - person.set(-1, -1); - break; - } - } - } - } public boolean isBusyCell(int x, int y) { for (Block cell : block) { diff --git a/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java b/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java new file mode 100644 index 0000000..31a75c5 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java @@ -0,0 +1,90 @@ +package ruscz.game.gameOfProd.Map; + +import ruscz.game.gameOfProd.Cells.Block; +import ruscz.game.gameOfProd.Cells.EventMoveToCell; +import ruscz.game.gameOfProd.Cells.TypeOfCell; +import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; + +import java.util.LinkedList; + +public class MapPainter { + private LinkedList maps; + private Map actualMap; + + public MapPainter (LinkedList maps){ + this.maps = maps; + } + + public void setMap(Map map){ + if (maps.contains(map)){ + actualMap = map; + } + else { + maps.add(map); + actualMap = map; + } + } + + public Map getActualMap(){ + return actualMap; + } + + + public void createMap() { + for (int i = 0; i < actualMap.getMapArray().length; i++) { + for (int j = 0; j < actualMap.getMapArray().length; j++) { + if (actualMap.getMapArray()[i][j] == TypeOfCell.BLOCK && !actualMap.isBusyCell(i,j)) { + actualMap.getBlocks().add(new Block(i, j, actualMap.getImageCell())); + } + + if (actualMap.getMapArray()[i][j] == TypeOfCell.SPAWNPOINT && actualMap.getIdExit() == 0) { + actualMap.getPerson().set(i, j); + } + } + } + setPersonByMoveToEvent(); + } + + private void setPersonByMoveToEvent() { + for (EventMoveToCell cell : actualMap.getMoveToCells()) { + if (this.actualMap.getIdExit() == cell.getId()) { + switch (cell.getDirectionOut()) { + case 1: + actualMap.getPerson().set(cell.getX(), cell.getY() - 1); + actualMap.getPerson().setIsMoving(true); + actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_UP); + actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE); + actualMap.getPerson().setDirectionSight(2); + break; + case 2: + actualMap.getPerson().set(cell.getX() + 1, cell.getY()); + actualMap.getPerson().setIsMoving(true); + actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT); + actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE); + actualMap.getPerson().setDirectionSight(3); + break; + case 3: + actualMap.getPerson().set(cell.getX(), cell.getY() + 1); + actualMap.getPerson().setIsMoving(true); + actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN); + actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE); + actualMap.getPerson().setDirectionSight(0); + break; + case 4: + actualMap.getPerson().set(cell.getX() - 1, cell.getY()); + actualMap.getPerson().setIsMoving(true); + actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT); + actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE); + actualMap.getPerson().setDirectionSight(1); + break; + default: + actualMap.getPerson().set(-1, -1); + break; + } + } + } + } + + + +} diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index b214541..f2daeff 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -6,6 +6,7 @@ import ruscz.game.gameOfProd.Cells.TriggerCell; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Dialog.DialogBar; import ruscz.game.gameOfProd.Map.Map; +import ruscz.game.gameOfProd.Map.MapPainter; import javax.swing.*; import java.awt.*; @@ -13,6 +14,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.util.LinkedList; public class WindowOfFreeMove extends JFrame { public static final int CELL_SIZE = 40; @@ -32,11 +34,11 @@ public class WindowOfFreeMove extends JFrame { private Canvas canvas; private DialogBar dialogBar; private JLayeredPane lp; - private Map map; + private MapPainter map; private Timer timer; private Timer inputTimer; - public Map getMap() { + public MapPainter getMap() { return map; } @@ -49,7 +51,7 @@ public class WindowOfFreeMove extends JFrame { } public void setMap(Map map) { - this.map = map; + this.map.setMap(map); } public void paintMapOnWindow() { @@ -79,8 +81,8 @@ public class WindowOfFreeMove extends JFrame { inputTimer = new Timer(10, new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - if (!map.getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) { - map.getPerson().setDirectionMove(nextMove); + if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) { + map.getActualMap().getPerson().setDirectionMove(nextMove); } } }); @@ -88,8 +90,8 @@ public class WindowOfFreeMove extends JFrame { addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { - if (!map.getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) { - map.getPerson().setDirectionMove(nextMove); + if (!map.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) { + map.getActualMap().getPerson().setDirectionMove(nextMove); } } }); @@ -156,7 +158,7 @@ public class WindowOfFreeMove extends JFrame { m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this)); m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar), m1.getImageCell())); m1.addInteractionEvent(new InteractionCell(5, 5, "dialog-test", new Dialog(dialogBar), m1.getImageCell())); - m1.addInteractionEvent(new TriggerCell(7,5,"second-dialog", new Dialog(dialogBar), m1.getImageCell())); + m1.addInteractionEvent(new TriggerCell(2,2,"second-dialog", new Dialog(dialogBar), m1.getImageCell())); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this)); @@ -172,6 +174,14 @@ public class WindowOfFreeMove extends JFrame { m3.getImageCell().readMapFile("m1-tileset"); m4.getImageCell().readMapFile("m1-tileset"); + LinkedList mapLinkedList = new LinkedList<>(); + mapLinkedList.add(m1); + mapLinkedList.add(m2); + mapLinkedList.add(m3); + mapLinkedList.add(m4); + + map = new MapPainter(mapLinkedList); + setMap(m1); map.createMap(); isReadyToPaint = true; @@ -180,7 +190,7 @@ public class WindowOfFreeMove extends JFrame { timer = new Timer(10, new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - map.getPerson().move(); + map.getActualMap().getPerson().move(); canvas.repaint(); } }); @@ -198,18 +208,18 @@ public class WindowOfFreeMove extends JFrame { int count = 0; for (int i = 0; i < CANVAS_WIDTH; i++) { for (int j = 0; j < CANVAS_HEIGHT; j++) { - map.getImageCell().paintBackground(g2D, i, j, count); + map.getActualMap().getImageCell().paintBackground(g2D, i, j, count); count++; } } - for (Cell cell : map.getMoveToCells()) { + for (Cell cell : map.getActualMap().getMoveToCells()) { cell.paint(g2D); } - map.getPerson().paint(g2D); - for (Cell cell : map.getBlocks()) { + map.getActualMap().getPerson().paint(g2D); + for (Cell cell : map.getActualMap().getBlocks()) { cell.paint(g2D); } - for (Cell cell: map.getInteractionEventCells()){ + for (Cell cell: map.getActualMap().getInteractionEventCells()){ cell.paint(g2D); } } diff --git a/src/main/resources/second-dialog.txt b/src/main/resources/second-dialog.txt index eb7c310..d39a65c 100644 --- a/src/main/resources/second-dialog.txt +++ b/src/main/resources/second-dialog.txt @@ -1,7 +1,7 @@ Cop: Turn around! -Me: every now and then I get a little bit lonely, -and you're never comming 'round +Me: Living in material world +And I am a material boy Cop: Turn around! -Me: every now and then... +Me: You know that... *gets tazed* \ No newline at end of file