From e832cee10a67121df3be47d3df18b5532a9ee9f6 Mon Sep 17 00:00:00 2001 From: andreas Date: Thu, 6 Nov 2014 11:58:10 +0100 Subject: [PATCH] Anpassung an Keybinding --- HeatUp/src/de/heatup/resources/maps/map1.txt | 4 +- HeatUp/src/de/heatup/testing/GameLoop.java | 43 +++++- HeatUp/src/de/heatup/ui/KeyBindings.java | 145 ++++++++++++------- 3 files changed, 135 insertions(+), 57 deletions(-) diff --git a/HeatUp/src/de/heatup/resources/maps/map1.txt b/HeatUp/src/de/heatup/resources/maps/map1.txt index 3c648a4..5d78ea3 100644 --- a/HeatUp/src/de/heatup/resources/maps/map1.txt +++ b/HeatUp/src/de/heatup/resources/maps/map1.txt @@ -1,6 +1,6 @@ ################################ -#--1111----#----------#222222--# -#--1111----#----------#222222--# +#1111111111#----------#222222--# +#1111111111#----------#222222--# #######################222222### #---------#----------#########-# ###############-######---------# diff --git a/HeatUp/src/de/heatup/testing/GameLoop.java b/HeatUp/src/de/heatup/testing/GameLoop.java index 8c0f95e..c99444b 100644 --- a/HeatUp/src/de/heatup/testing/GameLoop.java +++ b/HeatUp/src/de/heatup/testing/GameLoop.java @@ -1,6 +1,11 @@ package de.heatup.testing; +import java.awt.event.KeyEvent; + +import javax.swing.InputMap; import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.KeyStroke; import de.heatup.cfg.Config; import de.heatup.mapengine.MapEngine; @@ -27,14 +32,14 @@ public class GameLoop { Player player01 = new Player(); Query.setPlayer(player01); GameInterface.getGamePanel().add(player01); - player01.setBounds(25, 25, 200, 200); + player01.setBounds(200, 200, 25, 25); - // Keybindings as object so we can load different bindings depending on number of players. - KeyBindings kb = new KeyBindings(); - GameInterface.getGamePanel().setFocusable(true); + final KeyBindings kb = new KeyBindings(); kb.setKeyBindings(); + GameInterface.getGamePanel().setFocusable(true); + //dynO3.setRescale(1.5,3.5); @@ -54,5 +59,35 @@ public class GameLoop { logic.startMovement(dynO3); //logic.startMovement(dynO4); + + + Thread t = new Thread(new Runnable() { + @Override + public void run() { + while (true) { +// System.out.println(kb.isDown() + " UD " + kb.isUp() + " - " + kb.isLeft() + " LR " + kb.isRight()); + + if ( kb.isLeft() ) { + KeyBindings.step('x',-1); + } else if ( kb.isRight()) { + KeyBindings.step('x',1); + } else if ( kb.isUp()) { + KeyBindings.step('y',-1); + } else if ( kb.isDown()){ + KeyBindings.step('y',1); + } else { + + } + + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }); + t.start(); } } diff --git a/HeatUp/src/de/heatup/ui/KeyBindings.java b/HeatUp/src/de/heatup/ui/KeyBindings.java index b71b30d..41a5063 100644 --- a/HeatUp/src/de/heatup/ui/KeyBindings.java +++ b/HeatUp/src/de/heatup/ui/KeyBindings.java @@ -10,67 +10,110 @@ import de.heatup.testing.Player; import de.heatup.testing.Query; public class KeyBindings { + + static boolean ignoreKeys = false; + static int counter; + final static Object obj = new Object(); + public static boolean left=false; + public static boolean right=false; + public static boolean up=false; + public static boolean down=false; + + + + + public boolean isDown() { + return down; + } + + public boolean isLeft() { + return left; + } + + public boolean isRight() { + return right; + } + + public boolean isUp() { + return up; + } + + public static void setKeyBindings() { - public void setKeyBindings() { KeyListener kl= new KeyAdapter() { @Override public void keyPressed(KeyEvent evt) { int keyCode = evt.getKeyCode(); switch (keyCode) { - case KeyEvent.VK_LEFT: step('x',-1); break; - case KeyEvent.VK_RIGHT: step('x',1); break; - case KeyEvent.VK_UP: step('y',-1); break; - case KeyEvent.VK_DOWN: step('y',1); break; + case KeyEvent.VK_LEFT: left=true; break; + case KeyEvent.VK_RIGHT: right=true; break; + case KeyEvent.VK_UP: up=true; break; + case KeyEvent.VK_DOWN: down=true; break; } - //System.out.println(KeyEvent.getKeyText(evt.getKeyCode())); + } + @Override public void keyReleased(KeyEvent evt) { + int keyCode = evt.getKeyCode(); + switch (keyCode) { + case KeyEvent.VK_LEFT: left=false; break; + case KeyEvent.VK_RIGHT: right=false; break; + case KeyEvent.VK_UP: up=false; break; + case KeyEvent.VK_DOWN: down=false; break; + } } }; GameInterface.getGamePanel().addKeyListener(kl); } - private synchronized void step(final char d, final int i) { - final Player playerX = Query.getPlayer(); - final int stepx = 25; - final int stepy = 25; - - - Thread t = new Thread(new Runnable() { - @Override - public void run() { - int newx = playerX.getX(); - int newy = playerX.getY(); - int countx = stepx; - int county = stepy; - - while (true) { - - if (d == 'x' && i == -1 && countx != 0) { - countx--; - newx--; - } else if ( d == 'x' && i == 1 && countx != 0 ) { - countx--; - newx++; - } else if ( d == 'y' && i == -1 && county != 0 ) { - county--; - newy--; - } else if ( d == 'y' && i == 1 && county != 0 ) { - county--; - newy++; - } else { - break; - } - playerX.setAutoBounds(newx, newy); - - try { - Thread.sleep(20); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - }); - t.start(); - + public static void step(final char d, final int i) { + //if ( ! ignoreKeys ) { + ignoreKeys = true; + counter++; + final Player playerX = Query.getPlayer(); + final int stepx = 25; + final int stepy = 25; + + + +// Thread t = new Thread(new Runnable() { +// @Override +// public void run() { +// synchronized (obj) { + + int newx = playerX.getX(); + int newy = playerX.getY(); + int countx = stepx; + int county = stepy; + + while (true) { + if (d == 'x' && i == -1 && countx != 0) { + countx=countx-1; + newx=newx-1; + } else if ( d == 'x' && i == 1 && countx != 0 ) { + countx=countx-1; + newx=newx+1; + } else if ( d == 'y' && i == -1 && county != 0 ) { + county=county-1; + newy=newy-1; + } else if ( d == 'y' && i == 1 && county != 0 ) { + county=county-1; + newy=newy+1; + } else { + break; + } + playerX.setAutoBounds(newx, newy); +// try { +// Thread.sleep(10); +// } catch (InterruptedException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + } + ignoreKeys = false; +// } +// } +// }); +// t.start(); + //} + } } -} +