From 75307fe50d5a3bd2b55f59d9a55fd9156480ab08 Mon Sep 17 00:00:00 2001 From: iryupnik Date: Mon, 9 Mar 2020 15:00:06 +0200 Subject: [PATCH] Remove static properties ffor WindowOfFreeMove --- src/main/java/Block.java | 6 +++-- src/main/java/EventMoveToCell.java | 18 ++++++++------- src/main/java/Game.java | 12 +++++++++- src/main/java/Map.java | 8 ++++--- src/main/java/MapManager.java | 14 ++++++++++++ src/main/java/Person.java | 17 +++++++++------ src/main/java/WindowOfFreeMove.java | 34 ++++++++++++++--------------- 7 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 src/main/java/MapManager.java diff --git a/src/main/java/Block.java b/src/main/java/Block.java index c593b69..ba7e83b 100644 --- a/src/main/java/Block.java +++ b/src/main/java/Block.java @@ -1,13 +1,15 @@ import java.awt.*; public class Block extends Cell { + private ImageCell imageCell; public Block() { super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.BLUE); } - public Block(int x, int y) { + public Block(int x, int y, ImageCell imageCell) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.BLUE); + this.imageCell = imageCell; } public boolean isInBlock(int x, int y) { @@ -19,7 +21,7 @@ public class Block extends Cell { public void paint(Graphics2D g) { super.paint(g); g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE); - g.drawImage(WindowOfFreeMove.getMap().getImageCell().getImagesOfTiles()[102], getX() * WindowOfFreeMove.CELL_SIZE, + g.drawImage(imageCell.getImagesOfTiles()[102], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } } \ No newline at end of file diff --git a/src/main/java/EventMoveToCell.java b/src/main/java/EventMoveToCell.java index 316332b..5e2528f 100644 --- a/src/main/java/EventMoveToCell.java +++ b/src/main/java/EventMoveToCell.java @@ -2,6 +2,7 @@ import java.awt.*; public class EventMoveToCell extends Cell { private Map map; + private WindowOfFreeMove window; private int id; private int directionOut; @@ -23,23 +24,24 @@ public class EventMoveToCell extends Cell { this.map = Map.findMapByName(nameOfMap); } - public EventMoveToCell(int id, int directionOut, int x, int y, Map map){ + public EventMoveToCell(int id, int directionOut, int x, int y, Map map, WindowOfFreeMove window){ super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green); this.id = id; this.directionOut = directionOut; this.map = map; + this.window = window; } public int getDirectionOut() { return directionOut; } public void moveToMap() { - WindowOfFreeMove.getMap().getPerson().stopPerson(); - WindowOfFreeMove.getMap().setIdExit(id); - WindowOfFreeMove.setMap(this.map); - WindowOfFreeMove.getMap().setIdExit(id); - WindowOfFreeMove.paintMapOnWindow(); - WindowOfFreeMove.repaintCanvas(); - WindowOfFreeMove.setCanPersonMove(true); + window.getMap().getPerson().stopPerson(); + window.getMap().setIdExit(id); + window.setMap(this.map); + window.getMap().setIdExit(id); + window.paintMapOnWindow(); + window.repaintCanvas(); + window.setCanPersonMove(true); } @Override diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 6b8c633..7f796a7 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,5 +1,15 @@ public class Game { + private MapManager mm; + public static void main(String[] args) { - new WindowOfFreeMove().start();} + WindowOfFreeMove window = new WindowOfFreeMove(); + Game game = new Game(); + game.setMapManager(window); + window.start(); + } + + private void setMapManager(WindowOfFreeMove window) { + mm = new MapManager(window); + } } diff --git a/src/main/java/Map.java b/src/main/java/Map.java index de970c9..942994d 100644 --- a/src/main/java/Map.java +++ b/src/main/java/Map.java @@ -21,7 +21,6 @@ public class Map { name = "Spawn"; idExit = 0; mapArray = new TypeOfCell[height][width]; - person = new Person(); moveToCells = new LinkedList(); block = new LinkedList(); isBlocked = false; @@ -41,12 +40,13 @@ public class Map { mapArray[i][0] = type; mapArray[i][mapArray.length - 1] = type; } + + person = new Person(this); } Map(int height, int width, Boolean isBlocked) { name = "Spawn"; idExit = 0; - person = new Person(); moveToCells = new LinkedList(); block = new LinkedList(); mapArray = new TypeOfCell[height][width]; @@ -67,6 +67,8 @@ public class Map { mapArray[i][0] = type; mapArray[i][mapArray[0].length - 1] = type; } + + person = new Person(this); } public static Map findMapByName(String nameOfMap) { @@ -114,7 +116,7 @@ public class Map { for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray.length; j++) { if (mapArray[i][j] == TypeOfCell.BLOCK) { - block.add(new Block(i, j)); + block.add(new Block(i, j, imageCell)); } if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) { diff --git a/src/main/java/MapManager.java b/src/main/java/MapManager.java new file mode 100644 index 0000000..a477aba --- /dev/null +++ b/src/main/java/MapManager.java @@ -0,0 +1,14 @@ +import java.util.LinkedList; + +public class MapManager { + WindowOfFreeMove window; + LinkedList maps; + Map actualMap; + + MapManager(WindowOfFreeMove window){ + this.window = window; + } + + + +} diff --git a/src/main/java/Person.java b/src/main/java/Person.java index 4d5ab5a..811e00d 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -2,6 +2,7 @@ import javax.swing.*; import java.awt.*; public class Person extends Cell { + private Map map; private ImagePerson imagePerson; private int directionMove; private int directionSight; @@ -17,19 +18,21 @@ public class Person extends Cell { this.directionSight = directionSight; } - public Person() { + public Person(Map map) { super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; + this.map = map; this.directionSight = 3; this.isMoving = false; this.size = WindowOfFreeMove.CELL_SIZE; } - public Person(int x, int y) { + public Person(int x, int y, Map map) { super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED); this.directionMove = 0; this.directionSight = 3; this.isMoving = false; + this.map = map; this.size = WindowOfFreeMove.CELL_SIZE; } @@ -72,20 +75,20 @@ public class Person extends Cell { y = 0; break; } - if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) { + if (directionMove != 0 && !map.isBusyCell(x, y)) { directionMoveLast = directionMove; isMoving = true; - } else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) { + } else if (directionMove != 0 && map.isBusyCell(x, y)) { directionMoveLast = 0; } - if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { + if (map.isCellMoveTo(x, y)) { set(x, y); WindowOfFreeMove.setCanPersonMove(false); if (!isMoving) { - WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); + map.findEventByXY(x, y).moveToMap(); } } else { - if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) { + if (!map.isBusyCell(x, y)) { set(x, y); } } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index 0a660c8..7d1aec6 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -19,18 +19,18 @@ public class WindowOfFreeMove extends JFrame { static final int KEY_RIGHT = 39; static final int KEY_DOWN = 40; - private static Canvas canvas; + private Canvas canvas; private static JPanel dialogBar; private JLayeredPane lp; - private static Map map; + private Map map; private Timer timer; private Timer inputTimer; - public static Map getMap() { + public Map getMap() { return map; } - public static void repaintCanvas(){ + public void repaintCanvas(){ canvas.repaint(); } @@ -46,12 +46,12 @@ public class WindowOfFreeMove extends JFrame { WindowOfFreeMove.dialogBar = dialogBar; } - public static void setMap(Map map) { - WindowOfFreeMove.map = map; + public void setMap(Map map) { + this.map = map; } - public static void paintMapOnWindow() { - WindowOfFreeMove.map.createMap(); + public void paintMapOnWindow() { + this.map.createMap(); } public WindowOfFreeMove() { @@ -147,17 +147,17 @@ public class WindowOfFreeMove extends JFrame { m4.getPerson().setImagePerson("mainpng.png", rec); m4.setName("4"); - m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2)); - m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4)); + m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this)); + m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this)); - m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3)); - m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1)); + m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); + m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this)); - m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4)); - m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2)); + m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4, this)); + m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2, this)); - m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1)); - m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3)); + m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1, this)); + m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this)); m1.getImageCell().readMapFile("map-test"); @@ -200,7 +200,7 @@ public class WindowOfFreeMove extends JFrame { timer.start(); } - static class Canvas extends JPanel { + class Canvas extends JPanel { @Override public void paint(Graphics g) { if (isReadyToPaint) {