Small fixes

This commit is contained in:
iryupnik 2020-03-03 12:22:37 +02:00
parent 92d11b869f
commit 5269320951
4 changed files with 31 additions and 22 deletions

View File

@ -1,5 +1,5 @@
public class Game { public class Game {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
new WindowOfFreeMove().start(); } new WindowOfFreeMove().start(); }
} }

View File

@ -121,23 +121,23 @@ public class Map {
switch (cell.getDirectionOut()) { switch (cell.getDirectionOut()) {
case 1: case 1:
person.set(cell.getX(), cell.getY() - 1); person.set(cell.getX(), cell.getY() - 1);
person.directionMoveLast = 0; person.setDirectionMoveLast(0);
person.directionSight = 2; person.setDirectionSight(2);
break; break;
case 2: case 2:
person.set(cell.getX() + 1, cell.getY()); person.set(cell.getX() + 1, cell.getY());
person.directionMoveLast = 0; person.setDirectionMoveLast(0);
person.directionSight = 3; person.setDirectionSight(3);
break; break;
case 3: case 3:
person.set(cell.getX(), cell.getY() + 1); person.set(cell.getX(), cell.getY() + 1);
person.directionMoveLast = 0; person.setDirectionMoveLast(0);
person.directionSight = 0; person.setDirectionSight(0);
break; break;
case 4: case 4:
person.set(cell.getX() - 1, cell.getY()); person.set(cell.getX() - 1, cell.getY());
person.directionMoveLast = 0; person.setDirectionMoveLast(0);
person.directionSight = 1; person.setDirectionSight(1);
break; break;
default: default:
person.set(-1, -1); person.set(-1, -1);

View File

@ -4,20 +4,30 @@ import java.awt.*;
public class Person extends Cell { public class Person extends Cell {
private ImagePerson imagePerson; private ImagePerson imagePerson;
private int directionMove; private int directionMove;
public int directionSight; private int directionSight;
public int directionMoveLast; private int directionMoveLast;
boolean ready = true; private boolean ready;
public void setDirectionMoveLast(int directionMoveLast) {
this.directionMoveLast = directionMoveLast;
}
public void setDirectionSight(int directionSight) {
this.directionSight = directionSight;
}
public Person() { public Person() {
super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED); super(-1, -1, WindowOfFreeMove.CELL_SIZE, Color.RED);
this.directionMove = 0; this.directionMove = 0;
this.directionSight = 3; this.directionSight = 3;
this.ready = true;
} }
public Person(int x, int y) { public Person(int x, int y) {
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.ready = true;
} }
public void setDirectionMove(int directionMove) { public void setDirectionMove(int directionMove) {
@ -29,7 +39,7 @@ public class Person extends Cell {
this.imagePerson = new ImagePerson(name, rectangles); this.imagePerson = new ImagePerson(name, rectangles);
} }
public void setAnimation(String[] names){ public void setAnimation(String[] names) {
this.imagePerson.setAnimationOfMoving(names); this.imagePerson.setAnimationOfMoving(names);
} }
@ -71,8 +81,7 @@ public class Person extends Cell {
imagePerson.getAnimationOfMoving()[2].flush(); imagePerson.getAnimationOfMoving()[2].flush();
imagePerson.getAnimationOfMoving()[3].flush(); imagePerson.getAnimationOfMoving()[3].flush();
ready = true; ready = true;
} } else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
directionMoveLast = 0; directionMoveLast = 0;
} }
directionMove = 0; directionMove = 0;
@ -94,22 +103,22 @@ public class Person extends Cell {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
super.paint(g); super.paint(g);
switch (directionMoveLast){ switch (directionMoveLast) {
case 0: case 0:
g.drawImage(imagePerson.getImagesOfDirection()[directionSight], getX() * WindowOfFreeMove.CELL_SIZE, g.drawImage(imagePerson.getImagesOfDirection()[directionSight], 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);
break; break;
case WindowOfFreeMove.KEY_LEFT: case WindowOfFreeMove.KEY_LEFT:
g.drawImage(imagePerson.getAnimationOfMoving()[3], getX()*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null); g.drawImage(imagePerson.getAnimationOfMoving()[3], getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null);
break; break;
case WindowOfFreeMove.KEY_RIGHT: case WindowOfFreeMove.KEY_RIGHT:
g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX()-1)*WindowOfFreeMove.CELL_SIZE, getY()*WindowOfFreeMove.CELL_SIZE, null); g.drawImage(imagePerson.getAnimationOfMoving()[1], (getX() - 1) * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, null);
break; break;
case WindowOfFreeMove.KEY_UP: case WindowOfFreeMove.KEY_UP:
g.drawImage(imagePerson.getAnimationOfMoving()[0], getX()*WindowOfFreeMove.CELL_SIZE, (getY())*WindowOfFreeMove.CELL_SIZE, null); g.drawImage(imagePerson.getAnimationOfMoving()[0], getX() * WindowOfFreeMove.CELL_SIZE, (getY()) * WindowOfFreeMove.CELL_SIZE, null);
break; break;
case WindowOfFreeMove.KEY_DOWN: case WindowOfFreeMove.KEY_DOWN:
g.drawImage(imagePerson.getAnimationOfMoving()[2], getX()*WindowOfFreeMove.CELL_SIZE, (getY()-1)*WindowOfFreeMove.CELL_SIZE, null); g.drawImage(imagePerson.getAnimationOfMoving()[2], getX() * WindowOfFreeMove.CELL_SIZE, (getY() - 1) * WindowOfFreeMove.CELL_SIZE, null);
break; break;
} }
} }

View File

@ -86,7 +86,7 @@ public class WindowOfFreeMove extends JFrame {
} }
public void start() throws InterruptedException { public void start() {
String[] strings = {"main-walk-up-long.gif", "main-walk-right-long.gif", "main-walk-down-long.gif", "main-walk-left-long.gif"}; 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]; Rectangle[] rec = new Rectangle[4];
for (int i = 0; i < rec.length; i++){ for (int i = 0; i < rec.length; i++){
@ -151,7 +151,7 @@ public class WindowOfFreeMove extends JFrame {
// lp.add(jf, JLayeredPane.POPUP_LAYER); // lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) { while (true) {
Thread.sleep(1); //Thread.sleep();
map.getPerson().move(); map.getPerson().move();
canvas.repaint(); canvas.repaint();
} }