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() {
if (!isMoving) {
int x = this.getX();
int y = this.getY();
if (!isMoving) {
switch (directionMove) {
case WindowOfFreeMove.KEY_LEFT:
directionSight = 1;
@ -78,8 +78,6 @@ public class Person extends Cell {
} 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 {
@ -87,6 +85,7 @@ public class Person extends Cell {
set(x, y);
}
}
directionMove = 0;
}
}
@ -113,9 +112,9 @@ public class Person extends Cell {
break;
}
if (size > 0 && isMoving) {
size--;
size -= 2;
}
if (size == 0 && isMoving) {
else {
isMoving = false;
}
} else {
@ -145,16 +144,19 @@ public class Person extends Cell {
if (size > WindowOfFreeMove.CELL_SIZE * 3 / 4) {
g.drawImage(imagePerson.getImagesOfDirection()[direction], getX() * WindowOfFreeMove.CELL_SIZE + Xsize,
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,
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,
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,
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 java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
@ -9,6 +11,7 @@ public class WindowOfFreeMove extends JFrame {
static final int CANVAS_HEIGHT = 10;
private static boolean canPersonMove;
private static boolean preventDoubleMove;
static final int KEY_LEFT = 37;
static final int KEY_UP = 38;
@ -19,6 +22,7 @@ public class WindowOfFreeMove extends JFrame {
private static JPanel dialogBar;
private JLayeredPane lp;
private static Map map;
private Timer timer;
public static Map getMap() {
return map;
@ -46,6 +50,7 @@ public class WindowOfFreeMove extends JFrame {
public WindowOfFreeMove() {
canPersonMove = true;
preventDoubleMove = true;
lp = getLayeredPane();
canvas = new Canvas();
@ -63,9 +68,11 @@ public class WindowOfFreeMove extends JFrame {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (canPersonMove)
if (canPersonMove && !map.getPerson().getIsMoving() && preventDoubleMove) {
map.getPerson().setDirectionMove(e.getKeyCode());
}
preventDoubleMove = !map.getPerson().getIsMoving();
}
});
lp.add(canvas, JLayeredPane.PALETTE_LAYER);
@ -149,11 +156,16 @@ public class WindowOfFreeMove extends JFrame {
//setCanPersonMove(false);
// lp.add(jf, JLayeredPane.POPUP_LAYER);
while (true) {
sleep(1);
timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (!map.getPerson().getIsMoving()) {
map.getPerson().move();
}
canvas.repaint();
}
});
timer.start();
}
static class Canvas extends JPanel {