fixed walking animation

This commit is contained in:
musilto9 2020-03-03 17:53:52 +01:00
parent 02d382a696
commit b986eba42a
2 changed files with 30 additions and 16 deletions

View File

@ -43,9 +43,9 @@ public class Person extends Cell {
} }
public void move() { public void move() {
if (!isMoving) {
int x = this.getX(); int x = this.getX();
int y = this.getY(); int y = this.getY();
if (!isMoving) {
switch (directionMove) { switch (directionMove) {
case WindowOfFreeMove.KEY_LEFT: case WindowOfFreeMove.KEY_LEFT:
directionSight = 1; directionSight = 1;
@ -78,8 +78,6 @@ public class Person extends Cell {
} else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) { } else if (directionMove != 0 && WindowOfFreeMove.getMap().isBusyCell(x, y)) {
directionMoveLast = 0; directionMoveLast = 0;
} }
directionMove = 0;
if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) { if (WindowOfFreeMove.getMap().isCellMoveTo(x, y)) {
WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap(); WindowOfFreeMove.getMap().findEventByXY(x, y).moveToMap();
} else { } else {
@ -87,6 +85,7 @@ public class Person extends Cell {
set(x, y); set(x, y);
} }
} }
directionMove = 0;
} }
} }
@ -113,9 +112,9 @@ public class Person extends Cell {
break; break;
} }
if (size > 0 && isMoving) { if (size > 0 && isMoving) {
size--; size -= 2;
} }
if (size == 0 && isMoving) { else {
isMoving = false; isMoving = false;
} }
} else { } else {
@ -145,16 +144,19 @@ public class Person extends Cell {
if (size > WindowOfFreeMove.CELL_SIZE * 3 / 4) { if (size > WindowOfFreeMove.CELL_SIZE * 3 / 4) {
g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * WindowOfFreeMove.CELL_SIZE + Xsize,
getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} else if (size > WindowOfFreeMove.CELL_SIZE * 1 / 2) { } else if (size > WindowOfFreeMove.CELL_SIZE / 2) {
g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize,
getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} else if (size > WindowOfFreeMove.CELL_SIZE * 1 / 4) { } else if (size > WindowOfFreeMove.CELL_SIZE / 4) {
g.drawImage(imagePerson.getImagesOfDirection()[direction + 8], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, g.drawImage(imagePerson.getImagesOfDirection()[direction + 8], getX() * WindowOfFreeMove.CELL_SIZE + Xsize,
getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} } else{
else{
g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize, g.drawImage(imagePerson.getImagesOfDirection()[direction + 4], getX() * WindowOfFreeMove.CELL_SIZE + Xsize,
getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); getY() * WindowOfFreeMove.CELL_SIZE + Ysize, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
} }
} }
public boolean getIsMoving() {
return isMoving;
}
} }

View File

@ -1,5 +1,7 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -9,6 +11,7 @@ public class WindowOfFreeMove extends JFrame {
static final int CANVAS_HEIGHT = 10; static final int CANVAS_HEIGHT = 10;
private static boolean canPersonMove; private static boolean canPersonMove;
private static boolean preventDoubleMove;
static final int KEY_LEFT = 37; static final int KEY_LEFT = 37;
static final int KEY_UP = 38; static final int KEY_UP = 38;
@ -19,6 +22,7 @@ public class WindowOfFreeMove extends JFrame {
private static JPanel dialogBar; private static JPanel dialogBar;
private JLayeredPane lp; private JLayeredPane lp;
private static Map map; private static Map map;
private Timer timer;
public static Map getMap() { public static Map getMap() {
return map; return map;
@ -46,6 +50,7 @@ public class WindowOfFreeMove extends JFrame {
public WindowOfFreeMove() { public WindowOfFreeMove() {
canPersonMove = true; canPersonMove = true;
preventDoubleMove = true;
lp = getLayeredPane(); lp = getLayeredPane();
canvas = new Canvas(); canvas = new Canvas();
@ -63,9 +68,11 @@ public class WindowOfFreeMove extends JFrame {
addKeyListener(new KeyAdapter() { addKeyListener(new KeyAdapter() {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
if (canPersonMove) if (canPersonMove && !map.getPerson().getIsMoving() && preventDoubleMove) {
map.getPerson().setDirectionMove(e.getKeyCode()); map.getPerson().setDirectionMove(e.getKeyCode());
} }
preventDoubleMove = !map.getPerson().getIsMoving();
}
}); });
lp.add(canvas, JLayeredPane.PALETTE_LAYER); lp.add(canvas, JLayeredPane.PALETTE_LAYER);
@ -149,11 +156,16 @@ public class WindowOfFreeMove extends JFrame {
//setCanPersonMove(false); //setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER); // lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) { timer = new Timer(5, new ActionListener() {
sleep(1); @Override
public void actionPerformed(ActionEvent actionEvent) {
if (!map.getPerson().getIsMoving()) {
map.getPerson().move(); map.getPerson().move();
}
canvas.repaint(); canvas.repaint();
} }
});
timer.start();
} }
static class Canvas extends JPanel { static class Canvas extends JPanel {