DEADSOFTWARE

Upgrade touch controls
authorfred-boy <fredboy@protonmail.com>
Sun, 29 Sep 2019 06:01:07 +0000 (13:01 +0700)
committerfred-boy <fredboy@protonmail.com>
Sun, 29 Sep 2019 06:01:07 +0000 (13:01 +0700)
android/assets/json/touch_buttons.json
core/src/ru/deadsoftware/cavedroid/game/GameInput.java
core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/misc/Assets.java
core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java

index ef5c42e7ee7fde8d197a8c85d27b28d76c094432..cddaf534397ea531931a299d935742d52e4b3b82 100644 (file)
@@ -3,42 +3,51 @@
     "x": 26,
     "y": -52,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "key": "W"
   },
   "down": {
     "x": 26,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "key": "S"
   },
   "left": {
     "x": 0,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "key": "A"
   },
   "right": {
     "x": 52,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "key": "D"
   },
   "alt": {
     "x": 78,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "key": "L-Alt"
   },
   "lmb": {
     "x": -52,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "mouse": true,
+    "key": "Left"
   },
   "rmb": {
     "x": -26,
     "y": -26,
     "w": 26,
-    "h": 26
+    "h": 26,
+    "mouse": true,
+    "key": "Right"
   }
 }
\ No newline at end of file
index df002fa8df09fd5d6e8836e5fb50e549f28c81ca..58d377885204430900497a5a5a5daf845e64b186 100644 (file)
@@ -33,6 +33,19 @@ public class GameInput {
         return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY()));
     }
 
+    private void goUpwards() {
+        if (checkSwim()) {
+            GP.player.swim = true;
+        } else if (GP.player.canJump) {
+            GP.player.mov.add(0, -7);
+        } else if (!GP.player.flyMode && GP.player.gameMode == 1) {
+            GP.player.flyMode = true;
+            GP.player.mov.y = 0;
+        } else if (GP.player.flyMode) {
+            GP.player.mov.y = -GamePhysics.PL_SPEED;
+        }
+    }
+
     @SuppressWarnings("IntegerDivisionInFloatingPointContext")
     private boolean insideCreativeInv(float screenX, float screenY) {
         TextureRegion creative = Assets.textureRegions.get("creative");
@@ -47,14 +60,22 @@ public class GameInput {
             switch (keycode) {
                 case Input.Keys.A:
                     GP.player.mov.x = -GamePhysics.PL_SPEED;
-                    GP.player.setDir(0);
+                    GP.player.setDir(Mob.LEFT);
                     if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
                     break;
                 case Input.Keys.D:
                     GP.player.mov.x = GamePhysics.PL_SPEED;
-                    GP.player.setDir(1);
+                    GP.player.setDir(Mob.RIGHT);
                     if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
                     break;
+                case Input.Keys.W:
+                case Input.Keys.SPACE:
+                    goUpwards();
+                    break;
+                case Input.Keys.S:
+                case Input.Keys.CONTROL_LEFT:
+                    GP.player.mov.y = GamePhysics.PL_SPEED;
+                    break;
             }
         } else {
             switch (keycode) {
@@ -195,33 +216,22 @@ public class GameInput {
     public void keyDown(int keycode) {
         keyDown = true;
         keyDownCode = keycode;
-        if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
-                keycode == Input.Keys.S || keycode == Input.Keys.D) {
-            wasdPressed(keycode);
-        } else switch (keycode) {
+        switch (keycode) {
+            case Input.Keys.A:
+            case Input.Keys.D:
+            case Input.Keys.W:
+            case Input.Keys.S:
+            case Input.Keys.SPACE:
+            case Input.Keys.CONTROL_LEFT:
+                wasdPressed(keycode);
+                break;
+
             case Input.Keys.ALT_LEFT:
                 if (CaveGame.TOUCH) {
                     GP.controlMode = GP.controlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
                 }
                 break;
 
-            case Input.Keys.SPACE:
-                if (checkSwim()) {
-                    GP.player.swim = true;
-                } else if (GP.player.canJump) {
-                    GP.player.mov.add(0, -7);
-                } else if (!GP.player.flyMode && GP.player.gameMode == 1) {
-                    GP.player.flyMode = true;
-                    GP.player.mov.y = 0;
-                } else if (GP.player.flyMode) {
-                    GP.player.mov.y = -GamePhysics.PL_SPEED;
-                }
-                break;
-
-            case Input.Keys.CONTROL_LEFT:
-                GP.player.mov.y = GamePhysics.PL_SPEED;
-                break;
-
             case Input.Keys.E:
                 if (CaveGame.GAME_STATE == GameState.PLAY) {
                     switch (GP.player.gameMode) {
@@ -269,6 +279,8 @@ public class GameInput {
                 if (CaveGame.TOUCH && GP.player.swim) GP.player.swim = false;
                 break;
 
+            case Input.Keys.W:
+            case Input.Keys.S:
             case Input.Keys.SPACE:
             case Input.Keys.CONTROL_LEFT:
                 if (GP.player.flyMode) GP.player.mov.y = 0;
index b45479914086c5f8474023502866d4f295b3ddda..1f1858989ac0284644b0e725d2d3f433c4bf36fd 100644 (file)
@@ -170,7 +170,7 @@ public class GameRenderer extends Renderer {
 
     private void drawTouchGui() {
         for (int i = 0; i < guiMap.size; i++) {
-            Rectangle touchKey = guiMap.getValueAt(i);
+            Rectangle touchKey = guiMap.getValueAt(i).getRect();
             spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
                     touchKey.x, touchKey.y, touchKey.width, touchKey.height);
         }
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java b/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java
new file mode 100644 (file)
index 0000000..3cf4483
--- /dev/null
@@ -0,0 +1,29 @@
+package ru.deadsoftware.cavedroid.game.objects;
+
+import com.badlogic.gdx.math.Rectangle;
+
+public class TouchButton {
+
+    private Rectangle rect;
+    private int code;
+    private boolean mouse;
+
+    public TouchButton(Rectangle rect, int code, boolean mouse) {
+        this.rect = rect;
+        this.code = code;
+        this.mouse = mouse;
+    }
+
+    public Rectangle getRect() {
+        return rect;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public boolean isMouse() {
+        return mouse;
+    }
+
+}
index 93a5f86ea4a9509bfcd40f09f240db06a05ccb64..b01153a4c6f697c9d3251b921ea494977e0ef3f2 100644 (file)
@@ -10,6 +10,7 @@ import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.utils.ArrayMap;
 import com.badlogic.gdx.utils.JsonReader;
 import com.badlogic.gdx.utils.JsonValue;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 
 import java.util.HashMap;
 
@@ -23,7 +24,7 @@ public class Assets {
     public static final Sprite[][] playerSprite = new Sprite[2][4];
     public static final Sprite[][] pigSprite = new Sprite[2][2];
     public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
-    public static final ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
+    public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
     public static final Sprite sandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
     public static final Sprite gravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
 
index 31e6dd06919aa20cf1ca70e5ba2cc89d93c0e2ec..359ac98f311aa00a36297cecf1803f58e875a9e6 100644 (file)
@@ -7,24 +7,36 @@ import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.utils.JsonValue;
 import ru.deadsoftware.cavedroid.CaveGame;
 import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 
+import static com.badlogic.gdx.utils.ObjectMap.Entry;
 import static ru.deadsoftware.cavedroid.GameScreen.GP;
 
 public class InputHandlerGame extends InputAdapter {
 
-    private static final int
-            UP = 0,
-            DOWN = 1,
-            LEFT = 2,
-            RIGHT = 3,
-            ALT = 4,
-            LMB = 5,
-            RMB = 6;
+    private static TouchButton nullButton = new TouchButton(null, -1, true);
 
     public InputHandlerGame() {
         loadTouchButtonsFromJSON();
     }
 
+    private int getMouseKey(String name) {
+        switch (name) {
+            case "Left":
+                return Input.Buttons.LEFT;
+            case "Right":
+                return Input.Buttons.RIGHT;
+            case "Middle":
+                return Input.Buttons.MIDDLE;
+            case "Back":
+                return Input.Buttons.BACK;
+            case "Forward":
+                return Input.Buttons.FORWARD;
+            default:
+                return -1;
+        }
+    }
+
     private void loadTouchButtonsFromJSON() {
         JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
         for (JsonValue key = json.child(); key != null; key = key.next()) {
@@ -32,9 +44,12 @@ public class InputHandlerGame extends InputAdapter {
             float y = key.getFloat("y");
             float w = key.getFloat("w");
             float h = key.getFloat("h");
+            boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
+            String name = key.getString("key");
+            int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
             if (x < 0) x = GP.renderer.getWidth() + x;
             if (y < 0) y = GP.renderer.getHeight() + y;
-            Assets.guiMap.put(key.name(), new Rectangle(x, y, w, h));
+            Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
         }
 
     }
@@ -47,13 +62,14 @@ public class InputHandlerGame extends InputAdapter {
         return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
     }
 
-    private int getTouchedKey(float touchX, float touchY) {
-        for (int i = 0; i < Assets.guiMap.size; i++) {
-            if (Assets.guiMap.getValueAt(i).contains(touchX, touchY)) {
-                return i;
+    private TouchButton getTouchedKey(float touchX, float touchY) {
+        for (Entry entry : Assets.guiMap) {
+            TouchButton button = (TouchButton) entry.value;
+            if (button.getRect().contains(touchX, touchY)) {
+                return button;
             }
         }
-        return -1;
+        return nullButton;
     }
 
     @Override
@@ -74,31 +90,11 @@ public class InputHandlerGame extends InputAdapter {
         float touchY = transformScreenY(screenY);
 
         if (CaveGame.TOUCH) {
-            int touchedKey = getTouchedKey(touchX, touchY);
-            switch (touchedKey) {
-                case UP:
-                    GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.W : Input.Keys.SPACE);
-                    break;
-                case DOWN:
-                    GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.S : Input.Keys.CONTROL_LEFT);
-                    break;
-                case LEFT:
-                    GP.input.keyDown(Input.Keys.A);
-                    break;
-                case RIGHT:
-                    GP.input.keyDown(Input.Keys.D);
-                    break;
-                case ALT:
-                    GP.input.keyDown(Input.Keys.ALT_LEFT);
-                    break;
-                case LMB:
-                    GP.input.touchDown(touchX, touchY, Input.Buttons.LEFT);
-                    break;
-                case RMB:
-                    GP.input.touchDown(touchX, touchY, Input.Buttons.RIGHT);
-                    break;
-                default:
-                    GP.input.touchDown(touchX, touchY, touchedKey);
+            TouchButton touchedKey = getTouchedKey(touchX, touchY);
+            if (touchedKey.isMouse()) {
+                GP.input.touchDown(touchX, touchY, touchedKey.getCode());
+            } else {
+                GP.input.keyDown(touchedKey.getCode());
             }
         } else {
             GP.input.touchDown(touchX, touchY, button);
@@ -112,23 +108,11 @@ public class InputHandlerGame extends InputAdapter {
         float touchY = transformScreenY(screenY);
 
         if (CaveGame.TOUCH) {
-            int touchedKey = getTouchedKey(touchX, touchY);
-            switch (touchedKey) {
-                case UP:
-                case DOWN:
-                case LEFT:
-                case RIGHT:
-                case ALT:
-                    GP.input.keyUp(GP.input.getKeyDownCode());
-                    break;
-                case LMB:
-                    GP.input.touchUp(touchX, touchY, Input.Buttons.LEFT);
-                    break;
-                case RMB:
-                    GP.input.touchUp(touchX, touchY, Input.Buttons.RIGHT);
-                    break;
-                default:
-                    GP.input.touchUp(touchX, touchY, touchedKey);
+            TouchButton touchedKey = getTouchedKey(touchX, touchY);
+            if (touchedKey.isMouse()) {
+                GP.input.touchUp(touchX, touchY, touchedKey.getCode());
+            } else {
+                GP.input.keyUp(GP.input.getKeyDownCode());
             }
         } else {
             GP.input.touchUp(touchX, touchY, button);
@@ -141,7 +125,7 @@ public class InputHandlerGame extends InputAdapter {
         float touchX = transformScreenX(screenX);
         float touchY = transformScreenY(screenY);
         if (CaveGame.TOUCH && GP.input.isKeyDown()) {
-            if (getTouchedKey(touchX, touchY) == -1) {
+            if (getTouchedKey(touchX, touchY).getCode() == -1) {
                 GP.input.keyUp(GP.input.getKeyDownCode());
             }
         } else {