Remove static properties ffor WindowOfFreeMove

This commit is contained in:
iryupnik 2020-03-09 15:00:06 +02:00
parent d32861393c
commit 75307fe50d
7 changed files with 71 additions and 38 deletions

View File

@ -1,13 +1,15 @@
import java.awt.*; import java.awt.*;
public class Block extends Cell { public class Block extends Cell {
private ImageCell imageCell;
public Block() { public Block() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.BLUE); 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); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.imageCell = imageCell;
} }
public boolean isInBlock(int x, int y) { public boolean isInBlock(int x, int y) {
@ -19,7 +21,7 @@ public class Block extends Cell {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE); 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); getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} }
} }

View File

@ -2,6 +2,7 @@ import java.awt.*;
public class EventMoveToCell extends Cell { public class EventMoveToCell extends Cell {
private Map map; private Map map;
private WindowOfFreeMove window;
private int id; private int id;
private int directionOut; private int directionOut;
@ -23,23 +24,24 @@ public class EventMoveToCell extends Cell {
this.map = Map.findMapByName(nameOfMap); 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); super(x,y,WindowOfFreeMove.CELL_SIZE, Color.green);
this.id = id; this.id = id;
this.directionOut = directionOut; this.directionOut = directionOut;
this.map = map; this.map = map;
this.window = window;
} }
public int getDirectionOut() { return directionOut; } public int getDirectionOut() { return directionOut; }
public void moveToMap() { public void moveToMap() {
WindowOfFreeMove.getMap().getPerson().stopPerson(); window.getMap().getPerson().stopPerson();
WindowOfFreeMove.getMap().setIdExit(id); window.getMap().setIdExit(id);
WindowOfFreeMove.setMap(this.map); window.setMap(this.map);
WindowOfFreeMove.getMap().setIdExit(id); window.getMap().setIdExit(id);
WindowOfFreeMove.paintMapOnWindow(); window.paintMapOnWindow();
WindowOfFreeMove.repaintCanvas(); window.repaintCanvas();
WindowOfFreeMove.setCanPersonMove(true); window.setCanPersonMove(true);
} }
@Override @Override

View File

@ -1,5 +1,15 @@
public class Game { public class Game {
private MapManager mm;
public static void main(String[] args) { 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);
}
} }

View File

@ -21,7 +21,6 @@ public class Map {
name = "Spawn"; name = "Spawn";
idExit = 0; idExit = 0;
mapArray = new TypeOfCell[height][width]; mapArray = new TypeOfCell[height][width];
person = new Person();
moveToCells = new LinkedList<EventMoveToCell>(); moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>(); block = new LinkedList<Block>();
isBlocked = false; isBlocked = false;
@ -41,12 +40,13 @@ public class Map {
mapArray[i][0] = type; mapArray[i][0] = type;
mapArray[i][mapArray.length - 1] = type; mapArray[i][mapArray.length - 1] = type;
} }
person = new Person(this);
} }
Map(int height, int width, Boolean isBlocked) { Map(int height, int width, Boolean isBlocked) {
name = "Spawn"; name = "Spawn";
idExit = 0; idExit = 0;
person = new Person();
moveToCells = new LinkedList<EventMoveToCell>(); moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>(); block = new LinkedList<Block>();
mapArray = new TypeOfCell[height][width]; mapArray = new TypeOfCell[height][width];
@ -67,6 +67,8 @@ public class Map {
mapArray[i][0] = type; mapArray[i][0] = type;
mapArray[i][mapArray[0].length - 1] = type; mapArray[i][mapArray[0].length - 1] = type;
} }
person = new Person(this);
} }
public static Map findMapByName(String nameOfMap) { public static Map findMapByName(String nameOfMap) {
@ -114,7 +116,7 @@ public class Map {
for (int i = 0; i < mapArray.length; i++) { for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) { for (int j = 0; j < mapArray.length; j++) {
if (mapArray[i][j] == TypeOfCell.BLOCK) { 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) { if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) {

View File

@ -0,0 +1,14 @@
import java.util.LinkedList;
public class MapManager {
WindowOfFreeMove window;
LinkedList<Map> maps;
Map actualMap;
MapManager(WindowOfFreeMove window){
this.window = window;
}
}

View File

@ -2,6 +2,7 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class Person extends Cell { public class Person extends Cell {
private Map map;
private ImagePerson imagePerson; private ImagePerson imagePerson;
private int directionMove; private int directionMove;
private int directionSight; private int directionSight;
@ -17,19 +18,21 @@ public class Person extends Cell {
this.directionSight = directionSight; this.directionSight = directionSight;
} }
public Person() { public Person(Map map) {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.directionMove = 0; this.directionMove = 0;
this.map = map;
this.directionSight = 3; this.directionSight = 3;
this.isMoving = false; this.isMoving = false;
this.size = WindowOfFreeMove.CELL_SIZE; 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); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.directionMove = 0; this.directionMove = 0;
this.directionSight = 3; this.directionSight = 3;
this.isMoving = false; this.isMoving = false;
this.map = map;
this.size = WindowOfFreeMove.CELL_SIZE; this.size = WindowOfFreeMove.CELL_SIZE;
} }
@ -72,20 +75,20 @@ public class Person extends Cell {
y = 0; y = 0;
break; break;
} }
if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) { if (directionMove != 0 && !map.isBusyCell(x, y)) {
directionMoveLast = directionMove; directionMoveLast = directionMove;
isMoving = true; isMoving = true;
} else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) { } else if (directionMove != 0 && map.isBusyCell(x, y)) {
directionMoveLast = 0; directionMoveLast = 0;
} }
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { if (map.isCellMoveTo(x, y)) {
set(x, y); set(x, y);
WindowOfFreeMove.setCanPersonMove(false); WindowOfFreeMove.setCanPersonMove(false);
if (!isMoving) { if (!isMoving) {
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); map.findEventByXY(x, y).moveToMap();
} }
} else { } else {
if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) { if (!map.isBusyCell(x, y)) {
set(x, y); set(x, y);
} }
} }

View File

@ -19,18 +19,18 @@ public class WindowOfFreeMove extends JFrame {
static final int KEY_RIGHT = 39; static final int KEY_RIGHT = 39;
static final int KEY_DOWN = 40; static final int KEY_DOWN = 40;
private static Canvas canvas; private Canvas canvas;
private static JPanel dialogBar; private static JPanel dialogBar;
private JLayeredPane lp; private JLayeredPane lp;
private static Map map; private Map map;
private Timer timer; private Timer timer;
private Timer inputTimer; private Timer inputTimer;
public static Map getMap() { public Map getMap() {
return map; return map;
} }
public static void repaintCanvas(){ public void repaintCanvas(){
canvas.repaint(); canvas.repaint();
} }
@ -46,12 +46,12 @@ public class WindowOfFreeMove extends JFrame {
WindowOfFreeMove.dialogBar = dialogBar; WindowOfFreeMove.dialogBar = dialogBar;
} }
public static void setMap(Map map) { public void setMap(Map map) {
WindowOfFreeMove.map = map; this.map = map;
} }
public static void paintMapOnWindow() { public void paintMapOnWindow() {
WindowOfFreeMove.map.createMap(); this.map.createMap();
} }
public WindowOfFreeMove() { public WindowOfFreeMove() {
@ -147,17 +147,17 @@ public class WindowOfFreeMove extends JFrame {
m4.getPerson().setImagePerson("mainpng.png", rec); m4.getPerson().setImagePerson("mainpng.png", rec);
m4.setName("4"); m4.setName("4");
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2)); m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4)); m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this));
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3)); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this));
m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4)); m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4, this));
m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2)); m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2, this));
m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1)); m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1, this));
m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3)); m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this));
m1.getImageCell().readMapFile("map-test"); m1.getImageCell().readMapFile("map-test");
@ -200,7 +200,7 @@ public class WindowOfFreeMove extends JFrame {
timer.start(); timer.start();
} }
static class Canvas extends JPanel { class Canvas extends JPanel {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (isReadyToPaint) { if (isReadyToPaint) {