From 48de33432d9615967da21b22b950bd526b330572 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sat, 7 Apr 2018 21:13:43 +0700 Subject: [PATCH] Add touch controls --- android/assets/touch_gui.png | Bin 0 -> 2137 bytes .../cavecraft/AndroidLauncher.java | 2 +- .../src/ru/deadsoftware/cavecraft/Assets.java | 30 ++++++-- .../ru/deadsoftware/cavecraft/CaveGame.java | 12 +++- .../ru/deadsoftware/cavecraft/GameScreen.java | 67 +++++++++++++++++- .../cavecraft/game/GameInputHandler.java | 56 ++++++++++----- .../deadsoftware/cavecraft/game/GameProc.java | 3 +- .../cavecraft/game/GameRenderer.java | 55 +++++++------- .../deadsoftware/cavecraft/game/WorldGen.java | 12 ++-- 9 files changed, 177 insertions(+), 60 deletions(-) create mode 100644 android/assets/touch_gui.png diff --git a/android/assets/touch_gui.png b/android/assets/touch_gui.png new file mode 100644 index 0000000000000000000000000000000000000000..b1944137a192cc296ac26d7233486d182bc61ec0 GIT binary patch literal 2137 zcmV-f2&VUmP)N+dL3`a+I~Q#wzVr267G^!A(*uU=^-&Fc)4RwQ~aj=~JZ$47ge#Rb<3b z4B7sgZv=`OO4Prldu*+RCBIF11TuIr|*g<+VwPT1{sxVyVkb`U403U<34PN!2R za#RMN&1U%a?OUN?>&R&f6VBKyQ~v)x48znPfQ<1~briCZxldJ>Ohnx}UDt(e+wA>f zS?)SVv@)SSuU~ZaX^j9Bo(BaD)Vau(qDE2YiN@b%^O&jg*c9}-|uskQjM7}FE832?>J7O5**P~(5VlAl5Az8 z7|G*^z(t_ma)7+dpA;bD6A{QrO<}*^W3$;H2m&~cqcoOI!o*FvJkKjMY#hgkq6nVn zWpaQzp_%jvf&iP%CUGd4IH%L8)KJqJ0mMd8W8c1gn@QS4MdW+`{rjiYh%gMb49|__ zWs*zT^Ej`g@?2!o3C%_1l!+kga(8#vX$~;a={)j&vEwYu%0vMAdj4Ke&9F`w%cv(1iIBBK=l6W^_pC}Uay%cQ=MsG-8BT=v007+ z9Kz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2a&6hbz(}g~RCxEzSE_Pa(EM5mBhm7z0eriET zG(1hqs(55q`izDEU7c_tIiEg#>O{_vPVDqP1ax)6Wo&W=O@OhTq#5fruGR?9)d`oe z$uTf6FfcF}AX!G)k)REU`9rUBuO|*f-;9nW@8c_F%qiW_-^s1fKW&x^K z#u#)5)vI>eg7zz848{rND`O1$LvOzp-o}__FpvmfhJf2am%kO9A%GbInn9gIrCshj zn6$0TYsFCZ6HLaW%vjLnW9NzK?%Ruiz2*SMQ2j^1USAqDN3f8A?nJ zU?SA*5TNSIq+Wv;-uya1U+izgW16nBDxq$Lh-L`riBPvP92f$;au@>KAV&Yn7=s&( z0C}&BQQ>en;B-2rvR3i4c&}W~hYuf=b?VByg*uvOgWPWh1_lNO1|8r(n3D5XR9)@j P00000NkvXXu0mjfhEoB6 literal 0 HcmV?d00001 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