DEADSOFTWARE

Add touch controls
authorfred-boy <fred-boy@protonmail.com>
Sat, 7 Apr 2018 14:13:43 +0000 (21:13 +0700)
committerfred-boy <fred-boy@protonmail.com>
Sat, 7 Apr 2018 14:13:43 +0000 (21:13 +0700)
android/assets/touch_gui.png [new file with mode: 0644]
android/src/ru/deadsoftware/cavecraft/AndroidLauncher.java
core/src/ru/deadsoftware/cavecraft/Assets.java
core/src/ru/deadsoftware/cavecraft/CaveGame.java
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/WorldGen.java

diff --git a/android/assets/touch_gui.png b/android/assets/touch_gui.png
new file mode 100644 (file)
index 0000000..b194413
Binary files /dev/null and b/android/assets/touch_gui.png differ
index f97468fcd48f193f0a6c18eafba957c52c811b80..aac1e66e1c51cc7e88c39c81bc7ee8f72a81da6b 100644 (file)
@@ -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);
        }
 }
index 45634ebfa449f2ed7fd4ec1357570d6f3e3d9562..96dbbdc328e46d8c69ddcdd70493b25fd7440389 100644 (file)
@@ -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<BLOCK_TEXTURES; i++) {
             blockTextures[i] = new TextureRegion(terrain,
index 447a91e60281b4845e03f8bdf36a80ba245b6e9c..65d332c0054e26aec32968b9fede49913be17990 100644 (file)
@@ -4,9 +4,19 @@ import com.badlogic.gdx.Game;
 
 public class CaveGame extends Game {
 
+       public static boolean TOUCH;
+
+       public CaveGame() {
+               this(true);
+       }
+
+       public CaveGame(boolean touch) {
+               TOUCH = touch;
+       }
+
        @Override
        public void create () {
                setScreen(new GameScreen());
        }
 
-}
\ No newline at end of file
+}
index 30382a83cf63183f947658bd251f5fce656fc9a0..3301805e5854acafb14a33263b1e32ec447167b6 100644 (file)
@@ -1,6 +1,7 @@
 package ru.deadsoftware.cavecraft;
 
 import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
 import com.badlogic.gdx.InputProcessor;
 import com.badlogic.gdx.Screen;
 import ru.deadsoftware.cavecraft.game.GameInputHandler;
@@ -86,7 +87,38 @@ public class GameScreen implements Screen {
         public boolean touchDown(int screenX, int screenY, int pointer, int button) {
             screenX *= gameProc.renderer.camera.viewportWidth/getWidth();
             screenY *= gameProc.renderer.camera.viewportHeight/getHeight();
-            gameInput.touchDown(screenX, screenY, button);
+            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;
         }
 
@@ -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 &&
+                        screenY<gameProc.renderer.camera.viewportHeight-26) {
+                    gameInput.keyUp(Input.Keys.W);
+                } else if (screenX>0 && 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;
         }
 
index 899fbba6f06578dd4ca66ec47162e596cdc3a231..313675d414b6788dfa8b1ca935677f87dd0cbd87 100644 (file)
@@ -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) {
index cf70144d75948eeac988b7c517b12a8247d11d0a..6fc7dd956592fd37fe372bf8ef4e177de3ca77db 100644 (file)
@@ -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();
index 3c6d7b38b7aa26b3ee4ec750220c0c1862867b9c..4ab0704af7a016e18dc63956affbe4bd5c4f279b 100644 (file)
@@ -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();
+        }
     }
 
 }
index 84f0ae1b7591fe64d52368e6f3bc9019223186c9..9f6e693886a11db7386bebeb2b9a3f7f4ced59dc 100644 (file)
@@ -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<width; x++) {
-            for (int y=height-noise[x]; y<height; y++) {
-                if (y==height-noise[x]) {
+            for (int y = height- hMap[x]; y<height; y++) {
+                if (y==height- hMap[x]) {
                     foreMap[x][y] = 3;
                     backMap[x][y] = 3;
-                } else if (y<height-noise[x]+4) {
+                } else if (y<height- hMap[x]+4) {
                     foreMap[x][y] = 2;
                     backMap[x][y] = 2;
                 } else {