diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index c1622ee..f6ff552 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -11,7 +11,7 @@ public class WindowOfFreeMove extends JFrame { static final int CANVAS_HEIGHT = 10; private static boolean canPersonMove; - private static boolean preventDoubleMove; + private static int nextMove; private static boolean isReadyToPaint; static final int KEY_LEFT = 37; @@ -24,6 +24,7 @@ public class WindowOfFreeMove extends JFrame { private JLayeredPane lp; private static Map map; private Timer timer; + private Timer inputTimer; public static Map getMap() { return map; @@ -52,7 +53,6 @@ public class WindowOfFreeMove extends JFrame { public WindowOfFreeMove() { isReadyToPaint = false; canPersonMove = true; - preventDoubleMove = true; lp = getLayeredPane(); canvas = new Canvas(); @@ -67,13 +67,40 @@ public class WindowOfFreeMove extends JFrame { dialogBar.setBackground(Color.BLACK); dialogBar.setVisible(false); + inputTimer = new Timer(17, new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (!map.getPerson().getIsMoving()) { + map.getPerson().setDirectionMove(nextMove); + } + } + }); + + addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + if (!map.getPerson().getIsMoving()) { + map.getPerson().setDirectionMove(nextMove); + } + } + }); + addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { - if (canPersonMove && !map.getPerson().getIsMoving() && preventDoubleMove) { - map.getPerson().setDirectionMove(e.getKeyCode()); - } - preventDoubleMove = !map.getPerson().getIsMoving(); + nextMove = e.getKeyCode(); + inputTimer.start(); +// if (canPersonMove && !map.getPerson().getIsMoving() && preventDoubleMove) { +// map.getPerson().setDirectionMove(e.getKeyCode()); +// } +// preventDoubleMove = !map.getPerson().getIsMoving(); + } + }); + + addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + inputTimer.stop(); } }); @@ -159,13 +186,11 @@ public class WindowOfFreeMove extends JFrame { //setCanPersonMove(false); // lp.add(jf, JLayeredPane.POPUP_LAYER); - timer = new Timer( 16, new ActionListener() { + timer = new Timer(13, new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - if (!map.getPerson().getIsMoving()) { - map.getPerson().move(); - } - canvas.repaint(); + map.getPerson().move(); + canvas.repaint(new Rectangle(CELL_SIZE, CELL_SIZE, CELL_SIZE * 8, CELL_SIZE * 8)); } }); timer.start();