From: fred-boy Date: Fri, 13 Apr 2018 03:19:59 +0000 (+0700) Subject: Update input handling (pretty messy) X-Git-Tag: alpha0.1~6 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=c9d399d3ae0b7f797d6fd62a4c22202a12a8825b;p=cavedroid.git Update input handling (pretty messy) --- diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index 209d32b..e1eea18 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -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 && - screenY0 && 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 index 0000000..4ceb0f8 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/InputHandler.java @@ -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 diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index c288aff..490960b 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -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 && + screenXgameProc.renderer.camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2 && + screenY0; 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= 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 (screenYgameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && + screenX 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) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 0e3993d..d6378d3 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -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