package jbyoshi.blockdodge.gui;

import java.awt.event.*;

import com.flowpowered.math.vector.*;

import jbyoshi.blockdodge.*;

public final class InputInGame implements Input, PlayerController, KeyListener {
	private final BlockDodge panel;
	private final BlockDodgeGame game;
	private volatile boolean up, down, left, right;

	public InputInGame(BlockDodge panel) {
		this.panel = panel;
		this.game = panel.getGame();
	}

	@Override
	public Vector2d getMovement() {
		Vector2d movement = new Vector2d(left == right ? 0 : left ? -1 : 1, up == down ? 0 : up ? -1 : 1);
		if (up || down || left || right) { // "Cannot normalize the zero vector"
			movement = movement.normalize();
		}
		return movement;
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			up = true;
			break;
		case KeyEvent.VK_DOWN:
			down = true;
			break;
		case KeyEvent.VK_LEFT:
			left = true;
			break;
		case KeyEvent.VK_RIGHT:
			right = true;
			break;
		case KeyEvent.VK_ESCAPE:
			game.addTask(() -> game.setPaused(!game.isPaused()));
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			up = false;
			break;
		case KeyEvent.VK_DOWN:
			down = false;
			break;
		case KeyEvent.VK_LEFT:
			left = false;
			break;
		case KeyEvent.VK_RIGHT:
			right = false;
			break;
		}
	}

	@Override
	public void activate() {
		panel.addKeyListener(this);
	}

	@Override
	public void deactivate() {
		panel.removeKeyListener(this);
	}

}

@@ -456,16 +456,29 @@
 %09public 
+synchronized 
 Vector2d
@@ -495,16 +495,16 @@
 nt() %7B%0D%0A
-
 %09%09Vector
@@ -603,43 +603,8 @@
 %0D%0A%09%09
-if (up %7C%7C down %7C%7C left %7C%7C right) %7B 
 // %22
@@ -638,16 +638,57 @@
 ector%22%0D%0A
+%09%09if (movement.equals(Vector2d.ZERO)) %7B%0D%0A
 %09%09%09movem
@@ -810,32 +810,32 @@
 %09@Override%0D%0A
-
 %09public 
 void keyPres
@@ -814,32 +814,45 @@
 erride%0D%0A%09public 
+synchronized 
 void keyPressed(

Fix synchronization issue.