DEADSOFTWARE

Update input handling (pretty messy)
authorfred-boy <fred-boy@protonmail.com>
Fri, 13 Apr 2018 03:19:59 +0000 (10:19 +0700)
committerfred-boy <fred-boy@protonmail.com>
Fri, 13 Apr 2018 03:19:59 +0000 (10:19 +0700)
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/InputHandler.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java

index 209d32bb058b39ea8d62d5087e667ee6142e7fc6..e1eea188e0d7ab2335260d776f7a573ad819a8b8 100644 (file)
@@ -12,14 +12,12 @@ public class GameScreen implements Screen {
     public static int FPS;
 
     private GameProc gameProc;
-    private GameInputHandler gameInput;
 
     public GameScreen() {
         Assets.load();
         Items.load();
         gameProc = new GameProc();
-        gameInput = new GameInputHandler(gameProc);
-        Gdx.input.setInputProcessor(new InputHandler());
+        Gdx.input.setInputProcessor(new InputHandler(gameProc));
     }
 
     public static int getWidth() {
@@ -67,128 +65,4 @@ public class GameScreen implements Screen {
 
     }
 
-    private class InputHandler implements InputProcessor {
-
-        @Override
-        public boolean keyDown(int keycode) {
-            if (keycode == Input.Keys.N) {
-                gameProc = new GameProc();
-                gameInput = new GameInputHandler(gameProc);
-            } else {
-                gameInput.keyDown(keycode);
-            }
-            return false;
-        }
-
-        @Override
-        public boolean keyUp(int keycode) {
-            gameInput.keyUp(keycode);
-            return false;
-        }
-
-        @Override
-        public boolean keyTyped(char character) {
-            return false;
-        }
-
-        @Override
-        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
-            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
-            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
-            if (CaveGame.TOUCH) {
-                if (screenX > 26 && screenX < 52 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 52 &&
-                        screenY < gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.W);
-                } else if (screenX > 0 && screenX < 26 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.A);
-                } else if (screenX > 26 && screenX < 52 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.S);
-                } else if (screenX > 52 && screenX < 78 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.D);
-                } else if (screenX > 78 && screenX < 104 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.ALT_LEFT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth - 52 &&
-                        screenX < gameProc.renderer.camera.viewportWidth - 26 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth - 26 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.touchDown(screenX, screenY, Input.Buttons.RIGHT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth / 2 - 52 &&
-                        screenX < gameProc.renderer.camera.viewportWidth / 2 + 52 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyDown(Input.Keys.SPACE);
-                }
-            } else {
-                gameInput.touchDown(screenX, screenY, button);
-            }
-            return false;
-        }
-
-        @Override
-        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
-            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
-            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
-            if (CaveGame.TOUCH) {
-                if (screenX>26 && screenX<52 &&
-                        screenY>gameProc.renderer.camera.viewportHeight-52 &&
-                        screenY<gameProc.renderer.camera.viewportHeight-26) {
-                    gameInput.keyUp(Input.Keys.W);
-                } else if (screenX>0 && screenX<26 &&
-                        screenY>gameProc.renderer.camera.viewportHeight-26) {
-                    gameInput.keyUp(Input.Keys.A);
-                } else if (screenX>26 && screenX<52 &&
-                        screenY>gameProc.renderer.camera.viewportHeight-26) {
-                    gameInput.keyUp(Input.Keys.S);
-                } else if (screenX>52 && screenX<78 &&
-                        screenY>gameProc.renderer.camera.viewportHeight-26) {
-                    gameInput.keyUp(Input.Keys.D);
-                } else if (screenX > 78 && screenX < 104 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyUp(Input.Keys.ALT_LEFT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth - 52 &&
-                        screenX < gameProc.renderer.camera.viewportWidth - 26 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth - 26 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.touchUp(screenX, screenY, Input.Buttons.RIGHT);
-                } else if (screenX > gameProc.renderer.camera.viewportWidth / 2 - 52 &&
-                        screenX < gameProc.renderer.camera.viewportWidth / 2 + 52 &&
-                        screenY > gameProc.renderer.camera.viewportHeight - 26) {
-                    gameInput.keyUp(Input.Keys.SPACE);
-                }
-            } else {
-                gameInput.touchUp(screenX, screenY, button);
-            }
-            return false;
-        }
-
-        @Override
-        public boolean touchDragged(int screenX, int screenY, int pointer) {
-            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
-            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
-            gameInput.touchDragged(screenX, screenY);
-            return false;
-        }
-
-        @Override
-        public boolean mouseMoved(int screenX, int screenY) {
-            screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
-            screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
-            gameInput.mouseMoved(screenX,screenY);
-            return false;
-        }
-
-        @Override
-        public boolean scrolled(int amount) {
-            gameInput.scrolled(amount);
-            return false;
-        }
-    }
 }
diff --git a/core/src/ru/deadsoftware/cavecraft/InputHandler.java b/core/src/ru/deadsoftware/cavecraft/InputHandler.java
new file mode 100644 (file)
index 0000000..4ceb0f8
--- /dev/null
@@ -0,0 +1,128 @@
+package ru.deadsoftware.cavecraft;
+
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.InputProcessor;
+import ru.deadsoftware.cavecraft.game.GameInputHandler;
+import ru.deadsoftware.cavecraft.game.GameProc;
+
+import static ru.deadsoftware.cavecraft.GameScreen.getHeight;
+import static ru.deadsoftware.cavecraft.GameScreen.getWidth;
+
+public class InputHandler implements InputProcessor {
+
+    private GameProc gameProc;
+    private GameInputHandler gameInput;
+
+    private float tWidth, tHeight;
+
+    public InputHandler(GameProc gameProc) {
+        this.gameProc = gameProc;
+        this.gameInput = new GameInputHandler(gameProc);
+        tWidth = gameProc.renderer.touchCam.viewportWidth;
+        tHeight = gameProc.renderer.touchCam.viewportHeight;
+    }
+
+    @Override
+    public boolean keyDown(int keycode) {
+        gameInput.keyDown(keycode);
+        return false;
+    }
+
+    @Override
+    public boolean keyUp(int keycode) {
+        gameInput.keyUp(keycode);
+        return false;
+    }
+
+    @Override
+    public boolean keyTyped(char character) {
+        return false;
+    }
+
+    @Override
+    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
+        int tx = (int)(screenX * tWidth/getWidth());
+        int ty = (int)(screenY * tHeight/getHeight());
+        screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+        screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
+
+        if (CaveGame.TOUCH) {
+            if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
+                if (gameProc.ctrlMode==1) gameInput.keyDown(Input.Keys.W);
+                    else gameInput.keyDown(Input.Keys.SPACE);
+            } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
+                gameInput.keyDown(Input.Keys.A);
+            } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
+                if (gameProc.ctrlMode==1) gameInput.keyDown(Input.Keys.S);
+                    else gameInput.keyDown(Input.Keys.CONTROL_LEFT);
+            } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
+                gameInput.keyDown(Input.Keys.D);
+            } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
+                gameInput.keyDown(Input.Keys.ALT_LEFT);
+            } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
+                gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
+            } else if (tx > tWidth - 26 && screenY > ty - 26) {
+                gameInput.touchDown(screenX, screenY, Input.Buttons.RIGHT);
+            } else {
+                gameInput.touchDown(screenX, screenY, Input.Buttons.LEFT);
+            }
+        } else {
+            gameInput.touchDown(screenX, screenY, button);
+        }
+        return false;
+    }
+
+    @Override
+    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
+        int tx = (int)(screenX * tWidth/getWidth());
+        int ty = (int)(screenY * tHeight/getHeight());
+        screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+        screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
+        if (CaveGame.TOUCH) {
+            if (tx > 26 && tx < 52 && ty > tHeight - 52 && ty < tHeight - 26) {
+                if (gameProc.ctrlMode==1) gameInput.keyUp(Input.Keys.W);
+                else gameInput.keyUp(Input.Keys.SPACE);
+            } else if (tx > 0 && tx < 26 && ty > tHeight - 26) {
+                gameInput.keyUp(Input.Keys.A);
+            } else if (tx > 26 && tx < 52 && ty > tHeight - 26) {
+                if (gameProc.ctrlMode==1) gameInput.keyUp(Input.Keys.S);
+                else gameInput.keyUp(Input.Keys.CONTROL_LEFT);
+            } else if (tx > 52 && tx < 78 && ty > tHeight - 26) {
+                gameInput.keyUp(Input.Keys.D);
+            } else if (tx > 78 && tx < 104 && ty > tHeight - 26) {
+                gameInput.keyUp(Input.Keys.ALT_LEFT);
+            } else if (tx > tWidth - 52 && tx < tWidth - 26 && ty > tHeight - 26) {
+                gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
+            } else if (tx > tWidth - 26 && screenY > ty - 26) {
+                gameInput.touchUp(screenX, screenY, Input.Buttons.RIGHT);
+            } else {
+                gameInput.touchUp(screenX, screenY, Input.Buttons.LEFT);
+            }
+        } else {
+            gameInput.touchUp(screenX, screenY, button);
+        }
+        return false;
+    }
+
+    @Override
+    public boolean touchDragged(int screenX, int screenY, int pointer) {
+        screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+        screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
+        gameInput.touchDragged(screenX, screenY);
+        return false;
+    }
+
+    @Override
+    public boolean mouseMoved(int screenX, int screenY) {
+        screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
+        screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
+        gameInput.mouseMoved(screenX,screenY);
+        return false;
+    }
+
+    @Override
+    public boolean scrolled(int amount) {
+        gameInput.scrolled(amount);
+        return false;
+    }
+}
\ No newline at end of file
index c288afface0331516b5feb2969414f3fecfd1c46..490960b8986efffa5c4a24a81b1ef55136a0b65a 100644 (file)
@@ -15,28 +15,32 @@ public class GameInputHandler {
         this.gameProc = gameProc;
     }
 
-    public void  keyDown(int keyCode) {
+    private void wasdPressed(int keyCode) {
         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;
+            switch (keyCode) {
+                case Input.Keys.A:
+                    gameProc.player.moveX.add(-GamePhysics.PL_SPEED, 0);
+                    gameProc.player.dir = 0;
+                    break;
+                case Input.Keys.D:
+                    gameProc.player.moveX.add(GamePhysics.PL_SPEED, 0);
+                    gameProc.player.dir = 1;
+                    break;
             }
         } 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++;
+            switch (keyCode) {
+                case Input.Keys.A:
+                    gameProc.cursorX--;
+                    break;
+                case Input.Keys.D:
+                    gameProc.cursorX++;
+                    break;
+                case Input.Keys.W:
+                    gameProc.cursorY--;
+                    break;
+                case Input.Keys.S:
+                    gameProc.cursorY++;
+                    break;
             }
             if (gameProc.cursorX < 0)
                 gameProc.cursorX = 0;
@@ -47,39 +51,50 @@ public class GameInputHandler {
             if (gameProc.cursorY >= gameProc.world.getHeight())
                 gameProc.cursorY = gameProc.world.getHeight()-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.SPACE) {
-            if (gameProc.player.canJump) {
-                gameProc.player.moveY.add(0, -7);
-            } else if (!gameProc.player.flyMode) {
-                gameProc.player.flyMode = true;
-                gameProc.player.moveY.setZero();
-            } else {
-                gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
-            }
-        }
-        if (keyCode == Input.Keys.CONTROL_LEFT) {
-            gameProc.player.moveY.y = GamePhysics.PL_SPEED;
-        }
-        if (keyCode == Input.Keys.E) {
-            gameProc.renderer.showCreative = !gameProc.renderer.showCreative;
+    }
+
+    public void  keyDown(int keyCode) {
+        if (keyCode == Input.Keys.W || keyCode == Input.Keys.A ||
+                keyCode == Input.Keys.S || keyCode == Input.Keys.D) {
+            wasdPressed(keyCode);
+        } else switch (keyCode) {
+            case 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;
+                break;
+
+            case Input.Keys.SPACE:
+                if (gameProc.player.canJump) {
+                    gameProc.player.moveY.add(0, -7);
+                } else if (!gameProc.player.flyMode) {
+                    gameProc.player.flyMode = true;
+                    gameProc.player.moveY.setZero();
+                } else {
+                    gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
+                }
+                break;
+
+            case Input.Keys.CONTROL_LEFT:
+                gameProc.player.moveY.y = GamePhysics.PL_SPEED;
+                break;
+
+            case Input.Keys.E:
+                gameProc.renderer.showCreative = !gameProc.renderer.showCreative;
+                break;
         }
     }
 
     public void keyUp(int keyCode) {
-        if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
-            gameProc.player.moveX.x = 0;
-        }
-        if (keyCode == Input.Keys.SPACE) {
-            if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
-        }
-        if (keyCode == Input.Keys.CONTROL_LEFT) {
-            if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
+        switch (keyCode) {
+            case Input.Keys.A: case Input.Keys.D:
+                gameProc.player.moveX.x = 0;
+                break;
+
+            case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT:
+                if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
+                break;
         }
     }
 
@@ -87,35 +102,53 @@ public class GameInputHandler {
     }
 
     public void touchDown(int screenX, int screenY, int button) {
-        gameProc.touchDownX = screenX;
-        gameProc.touchDownY = screenY;
-        gameProc.touchDownTime = TimeUtils.millis();
-        gameProc.isTouchDown = true;
-        if (gameProc.renderer.showCreative) {
+        if (gameProc.renderer.showCreative &&
+                screenX>gameProc.renderer.camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2 &&
+                screenX<gameProc.renderer.camera.viewportWidth/2+Assets.creativeInv.getRegionWidth()/2 &&
+                screenY>gameProc.renderer.camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2 &&
+                screenY<gameProc.renderer.camera.viewportHeight/2+Assets.creativeInv.getRegionHeight()/2) {
             try {
-                int ix = (int)(screenX-(gameProc.renderer.camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2+8))/18;
-                int iy = (int)(screenY-(gameProc.renderer.camera.viewportHeight/2 - Assets.creativeInv.getRegionHeight()/2+18))/18;
-                int item = ix+iy*8;
-                for (int i=8; i>0; i--) {
-                    gameProc.player.inventory[i] = gameProc.player.inventory[i-1];
+                int ix = (int) (screenX - (gameProc.renderer.camera.viewportWidth / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18;
+                int iy = (int) (screenY - (gameProc.renderer.camera.viewportHeight / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18;
+                int item = ix + iy * 8;
+                for (int i = 8; i > 0; i--) {
+                    gameProc.player.inventory[i] = gameProc.player.inventory[i - 1];
                 }
-                if (item>=0 && item<Items.BLOCKS.size) gameProc.player.inventory[0] = item;
+                if (item >= 0 && item < Items.BLOCKS.size) gameProc.player.inventory[0] = item;
             } catch (Exception e) {
                 Gdx.app.error("GameInputHandler", e.toString());
             }
+        } else if (gameProc.renderer.showCreative) {
+            gameProc.renderer.showCreative = !gameProc.renderer.showCreative;
+        } else {
+            gameProc.touchDownX = screenX;
+            gameProc.touchDownY = screenY;
+            gameProc.touchDownTime = TimeUtils.millis();
+            gameProc.isTouchDown = true;
+            gameProc.touchDownButton = button;
         }
     }
 
     public void touchUp(int screenX, int screenY, int button) {
         if (gameProc.isTouchDown) {
             if (button == Input.Buttons.RIGHT){
-                gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
-                        gameProc.player.inventory[gameProc.invSlot]);
+                if (gameProc.ctrlMode==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);
-                } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
-                    gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
+                if (gameProc.ctrlMode==1) {
+                    if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
+                        gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
+                    } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
+                        gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
+                    }
+                } else {
+                    if (screenY<Assets.invBar.getRegionHeight() &&
+                            screenX>gameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
+                            screenX<gameProc.renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
+                        gameProc.invSlot = (int)((screenX-(gameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2))/20);
+                    }
                 }
             }
         }
index e461a9399381d38c59b31dfcf518b6cba66176da..1bedc729471a0a022c701bca28c56ebfbb674d04 100644 (file)
@@ -1,6 +1,7 @@
 package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.math.*;
+import ru.deadsoftware.cavecraft.CaveGame;
 import ru.deadsoftware.cavecraft.Items;
 import ru.deadsoftware.cavecraft.game.mobs.Mob;
 import ru.deadsoftware.cavecraft.game.objects.Player;
@@ -94,10 +95,10 @@ public class GamePhysics {
         if (pl.position.y > gameProc.world.getHeight()*16) {
             pl.position = gameProc.world.getSpawnPoint().cpy();
         }
-        /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
+        if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
             pl.moveY.add(0, -8);
             pl.canJump = false;
-        }*/
+        }
     }
 
     private void mobPhy(Mob mob) {
index 0e3993d8a4caf0018cd28e9e5e5279d1e8ad3b74..d6378d3d028ea2e98a240ec74847cc22d7741ed4 100644 (file)
@@ -1,8 +1,10 @@
 package ru.deadsoftware.cavecraft.game;
 
+import com.badlogic.gdx.Input;
 import com.badlogic.gdx.math.RandomXS128;
 import com.badlogic.gdx.utils.Array;
 import com.badlogic.gdx.utils.TimeUtils;
+import ru.deadsoftware.cavecraft.Assets;
 import ru.deadsoftware.cavecraft.game.mobs.Human;
 import ru.deadsoftware.cavecraft.game.mobs.Mob;
 import ru.deadsoftware.cavecraft.game.objects.Player;
@@ -25,6 +27,7 @@ public class GameProc {
 
     public boolean isTouchDown = false;
     public int touchDownX, touchDownY;
+    public int touchDownButton;
     public long touchDownTime;
 
     public GameProc() {
@@ -46,8 +49,15 @@ public class GameProc {
         physics.update(delta);
 
         if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
-            world.placeToBackground(cursorX,cursorY,
-                    player.inventory[invSlot]);
+            if (touchDownButton== Input.Buttons.RIGHT) {
+                world.placeToBackground(cursorX, cursorY,
+                        player.inventory[invSlot]);
+            } else if (touchDownButton==Input.Buttons.LEFT &&
+                    touchDownY< Assets.invBar.getRegionHeight() &&
+                    touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
+                    touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
+                renderer.showCreative = !renderer.showCreative;
+            }
             isTouchDown = false;
         }
     }
index 00508b2b74e153bdeb219000c0bb5c9cc99b6274..682984a5611adc334464374b8aad5fa46223746f 100644 (file)
@@ -21,9 +21,9 @@ public class GameRenderer {
 
     public boolean showCreative = false;
 
-    public OrthographicCamera camera, fontCam;
+    public OrthographicCamera camera, fontCam, touchCam;
     ShapeRenderer shapeRenderer;
-    SpriteBatch spriteBatch, fontBatch;
+    SpriteBatch spriteBatch, fontBatch, touchBatch;
 
     public GameRenderer(GameProc gameProc) {
         Gdx.gl.glClearColor(0f,.6f,.6f,1f);
@@ -42,6 +42,11 @@ public class GameRenderer {
         fontCam.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
         fontBatch = new SpriteBatch();
         fontBatch.setProjectionMatrix(fontCam.combined);
+        touchCam = new OrthographicCamera();
+        touchCam.setToOrtho(true, 240,
+                240*((float)GameScreen.getHeight()/GameScreen.getWidth()));
+        touchBatch = new SpriteBatch();
+        touchBatch.setProjectionMatrix(touchCam.combined);
     }
 
     private void setFontColor(int r, int g, int b) {
@@ -145,7 +150,7 @@ public class GameRenderer {
 
     private void drawCreative() {
         float x = camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2;
-        float y = camera.viewportHeight/2 - Assets.creativeInv.getRegionHeight()/2;
+        float y = camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2;
         spriteBatch.draw(Assets.creativeInv, x, y);
         spriteBatch.draw(Assets.creativeScroll, x+156, y+18);
         for (int i=1; i<Items.BLOCKS.size; i++) {
@@ -173,21 +178,17 @@ public class GameRenderer {
                 -1);
 
         if (showCreative) drawCreative();
+    }
 
-        if (CaveGame.TOUCH) {
-            spriteBatch.draw(Assets.touchArrows[0],26,camera.viewportHeight-52);
-            spriteBatch.draw(Assets.touchArrows[1],0,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchArrows[2],26,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchArrows[3],52,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchSpace, camera.viewportWidth/2-52, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchLMB, camera.viewportWidth-52, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchRMB, camera.viewportWidth-26, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchToggleMode, 78, camera.viewportHeight-26);
-            if (gameProc.ctrlMode==1) {
-                Assets.shade.setPosition(83, camera.viewportHeight-21);
-                Assets.shade.draw(spriteBatch);
-            }
-        }
+    private void drawTouchGui() {
+        touchBatch.draw(Assets.touchArrows[0],26,touchCam.viewportHeight-52);
+        touchBatch.draw(Assets.touchArrows[1],0,touchCam.viewportHeight-26);
+        touchBatch.draw(Assets.touchArrows[2],26,touchCam.viewportHeight-26);
+        touchBatch.draw(Assets.touchArrows[3],52,touchCam.viewportHeight-26);
+        //touchBatch.draw(Assets.touchSpace, touchCam.viewportWidth/2-52, touchCam.viewportHeight-26);
+        touchBatch.draw(Assets.touchLMB, touchCam.viewportWidth-52, touchCam.viewportHeight-26);
+        touchBatch.draw(Assets.touchRMB, touchCam.viewportWidth-26, touchCam.viewportHeight-26);
+        touchBatch.draw(Assets.touchToggleMode, 78, touchCam.viewportHeight-26);
     }
 
     public void render() {
@@ -199,9 +200,14 @@ public class GameRenderer {
         drawPlayer(gameProc.player);
         drawWorldForeground();
         drawGUI();
-
         spriteBatch.end();
 
+        if (CaveGame.TOUCH) {
+            touchBatch.begin();
+            drawTouchGui();
+            touchBatch.end();
+        }
+
         if (gameProc.ctrlMode==1) {
             shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
             shapeRenderer.setColor(Color.ORANGE);