From: fred-boy Date: Sat, 7 Apr 2018 14:13:43 +0000 (+0700) Subject: Add touch controls X-Git-Tag: alpha0.1~18 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=48de33432d9615967da21b22b950bd526b330572;p=cavedroid.git Add touch controls --- diff --git a/android/assets/touch_gui.png b/android/assets/touch_gui.png new file mode 100644 index 0000000..b194413 Binary files /dev/null and b/android/assets/touch_gui.png differ diff --git a/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java b/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java index f97468f..aac1e66 100644 --- a/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java +++ b/android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java @@ -11,6 +11,6 @@ public class AndroidLauncher extends AndroidApplication { protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); - initialize(new CaveGame(), config); + initialize(new CaveGame(true), config); } } diff --git a/core/src/ru/deadsoftware/cavecraft/Assets.java b/core/src/ru/deadsoftware/cavecraft/Assets.java index 45634eb..96dbbdc 100644 --- a/core/src/ru/deadsoftware/cavecraft/Assets.java +++ b/core/src/ru/deadsoftware/cavecraft/Assets.java @@ -10,7 +10,7 @@ public class Assets { public static final int BLOCK_TEXTURES = 3; public static Texture charTexture; - public static Sprite[] playerSprite = new Sprite[2]; + public static TextureRegion[] playerSkin = new TextureRegion[2]; public static Sprite shade; @@ -21,12 +21,18 @@ public class Assets { public static TextureRegion invBar; public static TextureRegion invCur; + public static Texture touchGui; + public static TextureRegion[] touchArrows = new TextureRegion[4]; + public static TextureRegion touchLMB, touchRMB; + public static TextureRegion touchToggleMode; + public static TextureRegion touchSpace; + public static void load() { charTexture = new Texture(Gdx.files.internal("char.png")); - playerSprite[0] = new Sprite(new TextureRegion(charTexture, 0,0,8,30)); - playerSprite[0].flip(false,true); - playerSprite[1] = new Sprite(new TextureRegion(charTexture, 8,0,8,30)); - playerSprite[1].flip(false,true); + playerSkin[0] = new TextureRegion(charTexture, 0,0,8,30); + playerSkin[0].flip(false,true); + playerSkin[1] = new TextureRegion(charTexture, 8,0,8,30); + playerSkin[1].flip(false,true); shade = new Sprite(new Texture(Gdx.files.internal("shade.png"))); @@ -34,6 +40,20 @@ public class Assets { invBar = new TextureRegion(gui,0,0,182,22); invCur = new TextureRegion(gui,0,22,24,24); + touchGui = new Texture(Gdx.files.internal("touch_gui.png")); + for (int i=0; i<4; i++) { + touchArrows[i] = new TextureRegion(touchGui, i*26, 0, 26,26); + touchArrows[i].flip(false, true); + } + touchLMB = new TextureRegion(touchGui, 0, 26, 26,26); + touchLMB.flip(false, true); + touchRMB = new TextureRegion(touchGui, 52, 26, 26,26); + touchRMB.flip(false, true); + touchToggleMode = new TextureRegion(touchGui, 26, 26, 26, 26); + touchToggleMode.flip(false, true); + touchSpace = new TextureRegion(touchGui, 0, 52, 104, 26); + touchSpace.flip(false, true); + terrain = new Texture(Gdx.files.internal("terrain.png")); for (int i=0; i 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; } @@ -94,7 +126,38 @@ public class GameScreen implements Screen { public boolean touchUp(int screenX, int screenY, int pointer, int button) { screenX *= gameProc.renderer.camera.viewportWidth/getWidth(); screenY *= gameProc.renderer.camera.viewportHeight/getHeight(); - gameInput.touchUp(screenX, screenY, button); + 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; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 899fbba..313675d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -12,16 +12,45 @@ public class GameInputHandler { } public void keyDown(int keyCode) { - if (keyCode == Input.Keys.A) { - 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.D) { - 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.SPACE && - 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) { @@ -31,17 +60,6 @@ public class GameInputHandler { } 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) { diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index cf70144..6fc7dd9 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -15,13 +15,14 @@ public class GameProc { public int cursorX, cursorY; public int invSlot; + public int ctrlMode; public boolean isTouchDown = false; public int touchDownX, touchDownY; public long touchDownTime; public GameProc() { - world = new GameWorld(512,32); + world = new GameWorld(512,256); renderer = new GameRenderer(this); physics = new GamePhysics(this); player = new Player(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 3c6d7b3..4ab0704 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.math.Vector3; import ru.deadsoftware.cavecraft.Assets; +import ru.deadsoftware.cavecraft.CaveGame; import ru.deadsoftware.cavecraft.Items; import ru.deadsoftware.cavecraft.GameScreen; import ru.deadsoftware.cavecraft.game.objects.Player; @@ -37,20 +38,6 @@ public class GameRenderer { spriteBatch.setProjectionMatrix(camera.combined); } - private void setColor(int r, int g, int b) { - shapeRenderer.setColor(r/255f, g/255f, b/255f, 1f); - } - - private void fillRect(float x, float y, float w, float h) { - shapeRenderer.set(ShapeRenderer.ShapeType.Filled); - shapeRenderer.rect(x,y,w,h); - } - - private void drawRect(float x, float y, float w, float h) { - shapeRenderer.set(ShapeRenderer.ShapeType.Line); - shapeRenderer.rect(x,y,w,h); - } - private void drawWorld() { int minX = (int) (camera.position.x/16); int minY = (int) (camera.position.y/16); @@ -78,39 +65,57 @@ public class GameRenderer { } private void drawPlayer(Player pl) { - Assets.playerSprite[pl.dir].setPosition(pl.position.x - camera.position.x, - pl.position.y - camera.position.y); - Assets.playerSprite[pl.dir].draw(spriteBatch); + spriteBatch.draw(Assets.playerSkin[pl.dir], + pl.position.x - camera.position.x, pl.position.y - camera.position.y); } private void drawGUI() { spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, - camera.viewportHeight - Assets.invBar.getRegionHeight()); + 0);//camera.viewportHeight - Assets.invBar.getRegionHeight()); for (int i=0; i<8; i++) { if (gameProc.player.inventory[i]>0) { spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(), camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20, - camera.viewportHeight-19); + 3); } } spriteBatch.draw(Assets.invCur, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot, - camera.viewportHeight - Assets.invBar.getRegionHeight() - 2); + -1); + + 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); + } + } } public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + spriteBatch.begin(); drawWorld(); drawPlayer(gameProc.player); drawGUI(); spriteBatch.end(); - shapeRenderer.begin(ShapeRenderer.ShapeType.Line); - shapeRenderer.setColor(Color.ORANGE); - drawRect(gameProc.cursorX*16-camera.position.x, - gameProc.cursorY*16-camera.position.y,16,16); - shapeRenderer.end(); + if (gameProc.ctrlMode==1) { + shapeRenderer.begin(ShapeRenderer.ShapeType.Line); + shapeRenderer.setColor(Color.ORANGE); + shapeRenderer.set(ShapeRenderer.ShapeType.Line); + shapeRenderer.rect(gameProc.cursorX * 16 - camera.position.x, + gameProc.cursorY * 16 - camera.position.y, 16, 16); + shapeRenderer.end(); + } } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java index 84f0ae1..9f6e693 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -6,9 +6,9 @@ import com.badlogic.gdx.utils.TimeUtils; public class WorldGen { private static int[][] foreMap, backMap; - private static int[] noise; + private static int[] hMap; - static int[] genNoise(int width, int mid, int min, int max) { + static int[] genLandscape(int width, int mid, int min, int max) { RandomXS128 rand = new RandomXS128(TimeUtils.millis()); int[] res = new int[width]; int t; @@ -25,13 +25,13 @@ public class WorldGen { static void genWorld(int width, int height) { foreMap = new int[width][height]; backMap = new int[width][height]; - noise = genNoise(width, height/2, 1, height); + hMap = genLandscape(width, height/2, height/4, height/4*3); for (int x=0; x