From 1373d2f790f33688565537b5780ab59faff163ab Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sun, 29 Sep 2019 13:01:07 +0700 Subject: [PATCH] Upgrade touch controls --- android/assets/json/touch_buttons.json | 23 +++-- .../cavedroid/game/GameInput.java | 58 ++++++----- .../cavedroid/game/GameRenderer.java | 2 +- .../cavedroid/game/objects/TouchButton.java | 29 ++++++ .../deadsoftware/cavedroid/misc/Assets.java | 3 +- .../cavedroid/misc/InputHandlerGame.java | 98 ++++++++----------- 6 files changed, 124 insertions(+), 89 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java diff --git a/android/assets/json/touch_buttons.json b/android/assets/json/touch_buttons.json index ef5c42e..cddaf53 100644 --- a/android/assets/json/touch_buttons.json +++ b/android/assets/json/touch_buttons.json @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index df002fa..58d3778 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -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; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index b454799..1f18589 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -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 index 0000000..3cf4483 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java @@ -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; + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index 93a5f86..b01153a 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -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 textureRegions = new HashMap<>(); - public static final ArrayMap guiMap = new ArrayMap<>(); + public static final ArrayMap 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")))); diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java index 31e6dd0..359ac98 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java @@ -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 { -- 2.29.2