DEADSOFTWARE

Add touch controls
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameInputHandler.java
index ff8fa2e89438cec46984e53333740fcc9e72521b..313675d414b6788dfa8b1ca935677f87dd0cbd87 100644 (file)
@@ -12,36 +12,54 @@ public class GameInputHandler {
     }
 
     public void  keyDown(int keyCode) {
-        if (keyCode == Input.Keys.LEFT) {
-            gameProc.player.moveX.add(-GamePhysics.PL_SPEED,0);
-            gameProc.player.dir = 0;
+        if (gameProc.ctrlMode==0) {
+            if (keyCode == Input.Keys.A) {
+                gameProc.player.moveX.add(-GamePhysics.PL_SPEED, 0);
+                gameProc.player.dir = 0;
+            }
+            if (keyCode == Input.Keys.D) {
+                gameProc.player.moveX.add(GamePhysics.PL_SPEED, 0);
+                gameProc.player.dir = 1;
+            }
+        } else {
+            if (keyCode == Input.Keys.A) {
+                gameProc.cursorX--;
+            }
+            if (keyCode == Input.Keys.D) {
+                gameProc.cursorX++;
+            }
+            if (keyCode == Input.Keys.W) {
+                gameProc.cursorY--;
+            }
+            if (keyCode == Input.Keys.S) {
+                gameProc.cursorY++;
+            }
+            if (gameProc.cursorX < 0)
+                gameProc.cursorX = 0;
+            if (gameProc.cursorX >= gameProc.world.getWidth())
+                gameProc.cursorX = gameProc.world.getWidth()-1;
+            if (gameProc.cursorY < 0)
+                gameProc.cursorY = 0;
+            if (gameProc.cursorY >= gameProc.world.getHeight())
+                gameProc.cursorY = gameProc.world.getHeight()-1;
         }
-        if (keyCode == Input.Keys.RIGHT) {
-            gameProc.player.moveX.add(GamePhysics.PL_SPEED,0);
-            gameProc.player.dir = 1;
+        if (keyCode == Input.Keys.ALT_LEFT) {
+            gameProc.ctrlMode++;
+            gameProc.cursorX = (int)(gameProc.player.position.x/16);
+            gameProc.cursorY = (int)(gameProc.player.position.y/16);
+            if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0;
         }
-        if (keyCode == Input.Keys.UP &&
-                gameProc.player.canJump) gameProc.player.moveY.add(0,-8);
+         if (keyCode == Input.Keys.SPACE &&
+                 gameProc.player.canJump) gameProc.player.moveY.add(0, -8);
     }
 
     public void keyUp(int keyCode) {
-        if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.LEFT) {
+        if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
             gameProc.player.moveX.x = 0;
         }
     }
 
     public void mouseMoved(int screenX, int screenY) {
-        gameProc.cursorX = (int)((screenX+gameProc.renderer.camera.position.x)/16);
-        gameProc.cursorY = (int)((screenY+gameProc.renderer.camera.position.y)/16);
-        if (gameProc.cursorX < 0)
-            gameProc.cursorX = 0;
-        if (gameProc.cursorX >= gameProc.world.getWidth())
-            gameProc.cursorX = gameProc.world.getWidth()-1;
-        if (gameProc.cursorY < 0)
-            gameProc.cursorY = 0;
-        if (gameProc.cursorY >= gameProc.world.getHeight())
-            gameProc.cursorY = gameProc.world.getHeight()-1;
-
     }
 
     public void touchDown(int screenX, int screenY, int button) {
@@ -54,7 +72,8 @@ public class GameInputHandler {
     public void touchUp(int screenX, int screenY, int button) {
         if (gameProc.isTouchDown) {
             if (button == Input.Buttons.RIGHT){
-                gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 1);
+                gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
+                        gameProc.player.inventory[gameProc.invSlot]);
             } else if (button == Input.Buttons.LEFT) {
                 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
                     gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
@@ -67,11 +86,12 @@ public class GameInputHandler {
     }
 
     public void touchDragged(int screenX, int screenY) {
-        /*gameProc.renderer.camera.position.x += (gameProc.touchDownX-screenX);
-        gameProc.renderer.camera.position.y += (gameProc.touchDownY-screenY);
-        gameProc.touchDownX = screenX;
-        gameProc.touchDownY = screenY;
-        gameProc.isTouchDown = false;*/
+    }
+
+    public void scrolled(int amount) {
+        gameProc.invSlot += amount;
+        if (gameProc.invSlot < 0) gameProc.invSlot = 8;
+        if (gameProc.invSlot > 8) gameProc.invSlot = 0;
     }
 
 }