From: fred-boy Date: Thu, 5 Apr 2018 11:25:21 +0000 (+0700) Subject: Add input handling X-Git-Tag: alpha0.1~27 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=5f7d6ee2ded67d412e861f09e102b52efb74d12c;p=cavedroid.git Add input handling --- diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index f227302..299dfa1 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -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 index 0000000..1273c05 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -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 index 0000000..bd91e72 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -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; + } + } + +} diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 61f7a3a..c6dd02f 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -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; y0) { + 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(); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index 68a7a01..faf0322 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -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 index 0000000..e3822d8 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -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