diff --git a/src/main/java/EventMoveToCell.java b/src/main/java/EventMoveToCell.java index 1f3bfe5..2e6532f 100644 --- a/src/main/java/EventMoveToCell.java +++ b/src/main/java/EventMoveToCell.java @@ -35,25 +35,10 @@ public class EventMoveToCell extends Cell { public void moveToMap() { WindowOfFreeMove.getMap().setIdExit(id); WindowOfFreeMove.setMap(this.map); - WindowOfFreeMove.getMap().getPerson().setDirectionSight(idToDirectionSight()); WindowOfFreeMove.getMap().setIdExit(id); WindowOfFreeMove.paintMapOnWindow(); } - private int idToDirectionSight(){ - switch (directionOut){ - case 1: - return 0; - case 2: - return 1; - case 3: - return 2; - case 4: - return 3; - } - return 0; - } - @Override public void paint(Graphics2D g) { super.paint(g); diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 9db5f16..89710ea 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,5 +1,5 @@ public class Game { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { new WindowOfFreeMove().start(); } } diff --git a/src/main/java/Map.java b/src/main/java/Map.java index a159ae1..4b7a15d 100644 --- a/src/main/java/Map.java +++ b/src/main/java/Map.java @@ -121,15 +121,23 @@ public class Map { switch (cell.getDirectionOut()) { case 1: person.set(cell.getX(), cell.getY() - 1); + person.directionMoveLast = 0; + person.directionSight = 2; break; case 2: person.set(cell.getX() + 1, cell.getY()); + person.directionMoveLast = 0; + person.directionSight = 3; break; case 3: person.set(cell.getX(), cell.getY() + 1); + person.directionMoveLast = 0; + person.directionSight = 0; break; case 4: person.set(cell.getX() - 1, cell.getY()); + person.directionMoveLast = 0; + person.directionSight = 1; break; default: person.set(-1, -1); diff --git a/src/main/java/Person.java b/src/main/java/Person.java index 3c97001..a7a62fa 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -1,9 +1,12 @@ +import javax.swing.*; import java.awt.*; public class Person extends Cell { private ImagePerson imagePerson; private int directionMove; - private int directionSight; + public int directionSight; + public int directionMoveLast; + boolean ready = true; public Person() { super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); @@ -22,54 +25,66 @@ public class Person extends Cell { this.directionMove = directionMove; } - public void setDirectionSight(int directionSight) { - if ((directionSight >= 0) && (directionSight <= 3)) - this.directionSight = directionSight; + public void setImagePerson(String name, Rectangle[] rectangles) { + this.imagePerson = new ImagePerson(name, rectangles); } - public void setImagePerson(String name, Rectangle[] rectangles) { this.imagePerson = new ImagePerson(name, rectangles); } - - public void setAnimation(String[] names) { this.imagePerson.setAnimationOfMoving(names); } + public void setAnimation(String[] names){ + this.imagePerson.setAnimationOfMoving(names); + } public void move() { int x = this.getX(); int y = this.getY(); - switch (directionMove) { - case WindowOfFreeMove.KEY_LEFT: - directionSight = 1; - x--; - if (x < 0) - x = WindowOfFreeMove.CANVAS_WIDTH - 1; - break; - case WindowOfFreeMove.KEY_RIGHT: - directionSight = 3; - x++; - if (x == WindowOfFreeMove.CANVAS_WIDTH) - x = 0; - break; - case WindowOfFreeMove.KEY_UP: - directionSight = 2; - y--; - if (y < 0) - y = WindowOfFreeMove.CANVAS_HEIGHT - 1; - break; - case WindowOfFreeMove.KEY_DOWN: - directionSight = 0; - y++; - if (y == WindowOfFreeMove.CANVAS_HEIGHT) - y = 0; - break; - } - this.directionMove = 0; + if (ready) { + switch (directionMove) { + case WindowOfFreeMove.KEY_LEFT: + directionSight = 1; + x--; + if (x < 0) + x = WindowOfFreeMove.CANVAS_WIDTH - 1; + break; + case WindowOfFreeMove.KEY_RIGHT: + directionSight = 3; + x++; + if (x == WindowOfFreeMove.CANVAS_WIDTH) + x = 0; + break; + case WindowOfFreeMove.KEY_UP: + directionSight = 2; + y--; + if (y < 0) + y = WindowOfFreeMove.CANVAS_HEIGHT - 1; + break; + case WindowOfFreeMove.KEY_DOWN: + directionSight = 0; + y++; + if (y == WindowOfFreeMove.CANVAS_HEIGHT) + y = 0; + break; + } + if (directionMove != 0 && !WindowOfFreeMove.getMap().isBusyCell(x, y)) { + ready = false; + directionMoveLast = directionMove; + imagePerson.getAnimationOfMoving()[0].flush(); + imagePerson.getAnimationOfMoving()[1].flush(); + imagePerson.getAnimationOfMoving()[2].flush(); + imagePerson.getAnimationOfMoving()[3].flush(); + ready = true; + } + else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) { + directionMoveLast = 0; + } + directionMove = 0; - if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { - WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); - } else { - if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) { - set(x, y); + if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { + WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); + } else { + if (!WindowOfFreeMove.getMap().isBusyCell(x, y)) { + set(x, y); + } } } - } public boolean isInPerson(int x, int y) { @@ -79,25 +94,23 @@ public class Person extends Cell { @Override public void paint(Graphics2D g) { super.paint(g); - if (directionMove == 0) - g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE, - getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); - else { - switch (directionMove) { - case WindowOfFreeMove.KEY_LEFT: - g.drawImage(imagePerson.getAnimationOfMoving()[3], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, - WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); - break; - case WindowOfFreeMove.KEY_RIGHT: - g.drawImage(imagePerson.getAnimationOfMoving()[1], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); - break; - case WindowOfFreeMove.KEY_UP: - g.drawImage(imagePerson.getAnimationOfMoving()[0], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); - break; - case WindowOfFreeMove.KEY_DOWN: - g.drawImage(imagePerson.getAnimationOfMoving()[2], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null); - break; - } + switch (directionMoveLast){ + case 0: + g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE, + getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); + break; + case WindowOfFreeMove.KEY_LEFT: + g.drawImage(imagePerson.getAnimationOfMoving()[3], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null); + break; + case WindowOfFreeMove.KEY_RIGHT: + g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX()-1)*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null); + break; + case WindowOfFreeMove.KEY_UP: + g.drawImage(imagePerson.getAnimationOfMoving()[0], getX()*WindowOfFreeMove.CELL_SIZE, (getY())*WindowOfFreeMove.CELL_SIZE, null); + break; + case WindowOfFreeMove.KEY_DOWN: + g.drawImage(imagePerson.getAnimationOfMoving()[2], getX()*WindowOfFreeMove.CELL_SIZE, (getY()-1)*WindowOfFreeMove.CELL_SIZE, null); + break; } } } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index 1b7f56a..b726312 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -4,7 +4,7 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class WindowOfFreeMove extends JFrame { - static final int CELL_SIZE = 50; + static final int CELL_SIZE = 40; static final int CANVAS_WIDTH = 10; static final int CANVAS_HEIGHT = 10; @@ -86,8 +86,8 @@ public class WindowOfFreeMove extends JFrame { } - public void start() { - String[] strings = {"main-walk-up.gif", "main-walk-right.gif", "main-walk-down.gif", "main-walk-left.gif"}; + public void start() throws InterruptedException { + String[] strings = {"main-walk-up-long.gif", "main-walk-right-long.gif", "main-walk-down-long.gif", "main-walk-left-long.gif"}; Rectangle[] rec = new Rectangle[4]; for (int i = 0; i < rec.length; i++){ rec[i] = new Rectangle(40, 40*i, 40, 40); @@ -100,12 +100,15 @@ public class WindowOfFreeMove extends JFrame { m1.setSpawnPoint(1, 1); Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false); m2.getPerson().setImagePerson("mainpng.png", rec); + m2.getPerson().setAnimation(strings); m2.setName("2"); Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true); m3.getPerson().setImagePerson("mainpng.png", rec); + m3.getPerson().setAnimation(strings); m3.setName("3"); Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false); m4.getPerson().setImagePerson("mainpng.png", rec); + m4.getPerson().setAnimation(strings); m4.setName("4"); m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2)); @@ -122,36 +125,33 @@ public class WindowOfFreeMove extends JFrame { setMap(m1); map.createMap(); - -// String s1 = "> Test"; -// for (int i = 0; i < s1.length() + 1; i++) { -// lp.remove(dialogBar); -// final String text = s1.substring(0, i); -// JPanel jf = new JPanel() { -// Font f = new Font("Times New Roman", Font.ITALIC, 50); // -// @Override -// public void paint(Graphics g) { -// super.paint(g); -// g.setColor(Color.green); -// g.setFont(f); -// g.drawString(text, 10, 50); -// } -// }; +// lp.remove(dialogBar); // -// jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4); -// jf.setBackground(Color.BLACK); -// jf.setVisible(false); // -// setDialogBar(jf); -// dialogBar.setVisible(true); - //setCanPersonMove(false); -// lp.add(jf, JLayeredPane.POPUP_LAYER); +// JPanel jf = new JPanel() { +// Font f = new Font("Times New Roman", Font.ITALIC, 50); // -// sleep(300); -// } +// @Override +// public void paint(Graphics g) { +// super.paint(g); +// g.setColor(Color.green); +// g.setFont(f); +// g.drawString("> Test", 10, 50); +// } +// }; +// +// jf.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4); +// jf.setBackground(Color.BLACK); +// jf.setVisible(false); +// +// setDialogBar(jf); +// dialogBar.setVisible(true); + //setCanPersonMove(false); +// lp.add(jf, JLayeredPane.POPUP_LAYER); while (true) { + Thread.sleep(1); map.getPerson().move(); canvas.repaint(); } @@ -174,9 +174,9 @@ public class WindowOfFreeMove extends JFrame { } } - protected static void sleep(int i) { + protected static void sleep() { try { - Thread.sleep(i); + Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/resources/main-walk-down-long.gif b/src/main/resources/main-walk-down-long.gif new file mode 100644 index 0000000..3cf4744 Binary files /dev/null and b/src/main/resources/main-walk-down-long.gif differ diff --git a/src/main/resources/main-walk-left-long.gif b/src/main/resources/main-walk-left-long.gif new file mode 100644 index 0000000..19ccaf8 Binary files /dev/null and b/src/main/resources/main-walk-left-long.gif differ diff --git a/src/main/resources/main-walk-right-long.gif b/src/main/resources/main-walk-right-long.gif new file mode 100644 index 0000000..08490df Binary files /dev/null and b/src/main/resources/main-walk-right-long.gif differ diff --git a/src/main/resources/main-walk-up-long.gif b/src/main/resources/main-walk-up-long.gif new file mode 100644 index 0000000..3d92c37 Binary files /dev/null and b/src/main/resources/main-walk-up-long.gif differ