DEADSOFTWARE

Add input handling
authorfred-boy <fred-boy@protonmail.com>
Thu, 5 Apr 2018 11:25:21 +0000 (18:25 +0700)
committerfred-boy <fred-boy@protonmail.com>
Thu, 5 Apr 2018 11:25:21 +0000 (18:25 +0700)
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GameProc.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/GameWorld.java
core/src/ru/deadsoftware/cavecraft/game/WorldGen.java [new file with mode: 0644]

index f227302c5febd06e0ef93b1fe9c47d08f3a137f6..299dfa15ebefa8e54453e710422f7ff89f221352 100644 (file)
@@ -1,15 +1,22 @@
 package ru.deadsoftware.cavecraft;
 
 import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.InputProcessor;
 import com.badlogic.gdx.Screen;
+import ru.deadsoftware.cavecraft.game.GameInputHandler;
+import ru.deadsoftware.cavecraft.game.GameProc;
 import ru.deadsoftware.cavecraft.game.GameRenderer;
 
 public class GameScreen implements Screen {
 
-    private GameRenderer renderer;
+    private GameProc gameProc;
+    private GameInputHandler gameInput;
 
     public GameScreen() {
-        renderer = new GameRenderer();
+        gameProc = new GameProc();
+        gameInput = new GameInputHandler(gameProc);
+
+        Gdx.input.setInputProcessor(new InputHandler());
     }
 
     public static int getWidth() {
@@ -27,7 +34,8 @@ public class GameScreen implements Screen {
 
     @Override
     public void render(float delta) {
-        renderer.render();
+        gameProc.update(delta);
+        gameProc.renderer.render();
     }
 
     @Override
@@ -54,4 +62,51 @@ public class GameScreen implements Screen {
     public void dispose() {
 
     }
+
+    private class InputHandler implements InputProcessor {
+
+        @Override
+        public boolean keyDown(int keycode) {
+            return false;
+        }
+
+        @Override
+        public boolean keyUp(int keycode) {
+            return false;
+        }
+
+        @Override
+        public boolean keyTyped(char character) {
+            return false;
+        }
+
+        @Override
+        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
+            gameInput.touchDown(screenX, screenY, button);
+            return false;
+        }
+
+        @Override
+        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
+            gameInput.touchUp(screenX, screenY, button);
+            return false;
+        }
+
+        @Override
+        public boolean touchDragged(int screenX, int screenY, int pointer) {
+            gameInput.touchDragged(screenX, screenY);
+            return false;
+        }
+
+        @Override
+        public boolean mouseMoved(int screenX, int screenY) {
+            gameInput.mouseMoved(screenX,screenY);
+            return false;
+        }
+
+        @Override
+        public boolean scrolled(int amount) {
+            return false;
+        }
+    }
 }
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java
new file mode 100644 (file)
index 0000000..1273c05
--- /dev/null
@@ -0,0 +1,51 @@
+package ru.deadsoftware.cavecraft.game;
+
+import com.badlogic.gdx.Input;
+import com.badlogic.gdx.utils.TimeUtils;
+
+public class GameInputHandler {
+
+    private GameProc gameProc;
+
+    public GameInputHandler(GameProc gameProc) {
+        this.gameProc = gameProc;
+    }
+
+    public void mouseMoved(int screenX, int screenY) {
+        gameProc.cursorX = (int)((screenX+gameProc.renderer.camera.position.x)/32);
+        gameProc.cursorY = (int)((screenY+gameProc.renderer.camera.position.y)/32);
+        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) {
+        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);
+            }
+        } else {
+            gameProc.touchDownTime = TimeUtils.millis();
+            gameProc.isTouchDown = true;
+        }
+    }
+
+    public void touchUp(int screenX, int screenY, int button) {
+        if (gameProc.isTouchDown && button == Input.Buttons.RIGHT){
+                gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 1);
+        }
+        gameProc.isTouchDown = false;
+    }
+
+    public void touchDragged(int screenX, int screenY) {
+    }
+
+}
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java
new file mode 100644 (file)
index 0000000..bd91e72
--- /dev/null
@@ -0,0 +1,31 @@
+package ru.deadsoftware.cavecraft.game;
+
+import com.badlogic.gdx.utils.TimeUtils;
+
+public class GameProc {
+
+    public static double RUN_TIME = 0;
+
+    public GameWorld world;
+    public GameRenderer renderer;
+
+    public int cursorX, cursorY;
+
+    public boolean isTouchDown = false;
+    public int touchDownX, touchDownY;
+    public long touchDownTime;
+
+    public GameProc() {
+        world = new GameWorld(512,16);
+        renderer = new GameRenderer(this);
+    }
+
+    public void update(float delta) {
+        RUN_TIME += delta;
+        if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
+            world.placeToBackground(cursorX,cursorY,1);
+            isTouchDown = false;
+        }
+    }
+
+}
index 61f7a3af2302ab4792588f60f85125845d6309a7..c6dd02f7053c11493d05474c99245aea3ab96e00 100644 (file)
@@ -9,16 +9,49 @@ import ru.deadsoftware.cavecraft.GameScreen;
 
 public class GameRenderer {
 
-    private OrthographicCamera camera;
-    private ShapeRenderer shapeRenderer;
+    private GameProc gameProc;
 
-    public GameRenderer() {
+    OrthographicCamera camera;
+    ShapeRenderer shapeRenderer;
+
+    public GameRenderer(GameProc gameProc) {
         Gdx.gl.glClearColor(0f,.8f,.8f,1f);
+        this.gameProc = gameProc;
+        camera = new OrthographicCamera();
+        camera.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
+        camera.position.x=0;
+        camera.position.y=0;
+        shapeRenderer = new ShapeRenderer();
+        shapeRenderer.setProjectionMatrix(camera.combined);
+    }
+
+    public void drawWorld() {
+        shapeRenderer.setAutoShapeType(true);
+        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
+        for (int y=0; y<gameProc.world.getHeight(); y++) {
+            for (int x=0; x<gameProc.world.getWidth(); x++) {
+                if (gameProc.world.getBackMap(x,y)>0) {
+                    shapeRenderer.setColor(Color.DARK_GRAY);
+                    shapeRenderer.rect(x*32-camera.position.x,
+                            y*32-camera.position.y,32,32);
+                }
+                if (gameProc.world.getForeMap(x,y)>0) {
+                    shapeRenderer.setColor(Color.GRAY);
+                    shapeRenderer.rect(x*32-camera.position.x,
+                            y*32-camera.position.y,32,32);
+                }
+            }
+        }
+        shapeRenderer.setColor(Color.ORANGE);
+        shapeRenderer.set(ShapeRenderer.ShapeType.Line);
+        shapeRenderer.rect(gameProc.cursorX*32-camera.position.x,
+                gameProc.cursorY*32-camera.position.y,32,32);
+        shapeRenderer.end();
     }
 
     public void render() {
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
-
+        drawWorld();
     }
 
 }
index 68a7a01cb5c8b5faa117f83b5767477c35094032..faf0322ed38491d995f717417069d4f46a291722 100644 (file)
@@ -1,5 +1,7 @@
 package ru.deadsoftware.cavecraft.game;
 
+import com.badlogic.gdx.Gdx;
+
 public class GameWorld {
 
     private final int WIDTH, HEIGHT;
@@ -9,8 +11,10 @@ public class GameWorld {
     public GameWorld(int w, int h) {
         WIDTH = w;
         HEIGHT = h;
-        foreMap = new int[WIDTH][HEIGHT];
-        backMap = new int[WIDTH][HEIGHT];
+        WorldGen.genWorld(WIDTH,HEIGHT);
+        foreMap = WorldGen.getForeMap();
+        backMap = WorldGen.getBackMap();
+        WorldGen.clear();
     }
 
     public int getWidth() {
@@ -22,19 +26,47 @@ public class GameWorld {
     }
 
     public int getForeMap(int x, int y) {
-        return foreMap[x][y];
+        int ret = 0;
+        try {
+            ret = foreMap[x][y];
+        } catch (ArrayIndexOutOfBoundsException e) {
+            Gdx.app.error("GameWorld",e.toString());
+        }
+        return ret;
     }
 
     public void setForeMap(int x, int y, int value) {
-        foreMap[x][y] = value;
+        try {
+            foreMap[x][y] = value;
+        } catch (ArrayIndexOutOfBoundsException e) {
+            Gdx.app.error("GameWorld", e.toString());
+        }
     }
 
     public int getBackMap(int x, int y) {
-        return backMap[x][y];
+        int ret = 0;
+        try {
+            ret = backMap[x][y];
+        } catch (ArrayIndexOutOfBoundsException e) {
+            Gdx.app.error("GameWorld",e.toString());
+        }
+        return ret;
     }
 
     public void setBackMap(int x, int y, int value) {
-        backMap[x][y] = value;
+        try {
+            backMap[x][y] = value;
+        } catch (ArrayIndexOutOfBoundsException e) {
+            Gdx.app.error("GameWorld", e.toString());
+        }
+    }
+
+    public void placeToForeground(int x, int y, int value) {
+        if (getForeMap(x,y) == 0 || value == 0) setForeMap(x,y,value);
+    }
+
+    public void placeToBackground(int x, int y, int value) {
+        if (getBackMap(x,y) == 0 || value == 0) setBackMap(x,y,value);
     }
 
 }
diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java
new file mode 100644 (file)
index 0000000..e3822d8
--- /dev/null
@@ -0,0 +1,30 @@
+package ru.deadsoftware.cavecraft.game;
+
+public class WorldGen {
+
+    private static int[][] foreMap, backMap;
+
+    static void genWorld(int width, int height) {
+        foreMap = new int[width][height];
+        backMap = new int[width][height];
+        for (int x=0; x<width; x++) {
+            for (int y=height-6; y<height; y++) {
+                foreMap[x][y]=1;
+                backMap[x][y]=1;
+            }
+        }
+    }
+
+    static int[][] getForeMap() {
+        return foreMap;
+    }
+
+    static int[][] getBackMap() {
+        return backMap;
+    }
+
+    static void clear() {
+        foreMap = null;
+        backMap = null;
+    }
+}