From a6ec412e508cfa8679dcf32e69600fbb939122af Mon Sep 17 00:00:00 2001 From: fred-boy Date: Thu, 5 Apr 2018 23:43:42 +0700 Subject: [PATCH] Add player and physics --- .../ru/deadsoftware/cavecraft/GameScreen.java | 12 ++- .../cavecraft/game/GameInputHandler.java | 51 +++++++++--- .../cavecraft/game/GamePhysics.java | 77 +++++++++++++++++ .../deadsoftware/cavecraft/game/GameProc.java | 15 +++- .../cavecraft/game/GameRenderer.java | 82 ++++++++++++++----- .../deadsoftware/cavecraft/game/WorldGen.java | 2 +- .../cavecraft/game/objects/Player.java | 24 ++++++ 7 files changed, 227 insertions(+), 36 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java create mode 100644 core/src/ru/deadsoftware/cavecraft/game/objects/Player.java diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index 299dfa1..451f2a2 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -40,7 +40,7 @@ public class GameScreen implements Screen { @Override public void resize(int width, int height) { - + gameProc.resetRenderer(); } @Override @@ -67,11 +67,13 @@ public class GameScreen implements Screen { @Override public boolean keyDown(int keycode) { + gameInput.keyDown(keycode); return false; } @Override public boolean keyUp(int keycode) { + gameInput.keyUp(keycode); return false; } @@ -82,24 +84,32 @@ public class GameScreen implements Screen { @Override 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); return false; } @Override 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); return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { + screenX *= gameProc.renderer.camera.viewportWidth/getWidth(); + screenY *= gameProc.renderer.camera.viewportHeight/getHeight(); gameInput.touchDragged(screenX, screenY); return false; } @Override public boolean mouseMoved(int screenX, int screenY) { + screenX *= gameProc.renderer.camera.viewportWidth/getWidth(); + screenY *= gameProc.renderer.camera.viewportHeight/getHeight(); gameInput.mouseMoved(screenX,screenY); return false; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 1273c05..c3aa564 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -11,9 +11,27 @@ public class GameInputHandler { this.gameProc = gameProc; } + public void keyDown(int keyCode) { + if (keyCode == Input.Keys.LEFT) { + gameProc.player.moveX.add(-GamePhysics.PL_SPEED,0); + gameProc.player.dir = 0; + } + if (keyCode == Input.Keys.RIGHT) { + gameProc.player.moveX.add(GamePhysics.PL_SPEED,0); + gameProc.player.dir = 1; + } + if (keyCode == Input.Keys.UP) gameProc.player.moveY.add(0,-12); + } + + public void keyUp(int keyCode) { + if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.LEFT) { + gameProc.player.moveX.x = 0; + } + } + 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); + 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()) @@ -26,26 +44,33 @@ public class GameInputHandler { } 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; - } + gameProc.touchDownX = screenX; + gameProc.touchDownY = screenY; + gameProc.touchDownTime = TimeUtils.millis(); + gameProc.isTouchDown = true; } public void touchUp(int screenX, int screenY, int button) { - if (gameProc.isTouchDown && button == Input.Buttons.RIGHT){ + if (gameProc.isTouchDown) { + if (button == Input.Buttons.RIGHT){ gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 1); + } else 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); + } + } } gameProc.isTouchDown = false; } public void touchDragged(int screenX, int screenY) { + gameProc.renderer.camera.position.x += (gameProc.touchDownX-screenX); + gameProc.renderer.camera.position.y += (gameProc.touchDownY-screenY); + gameProc.touchDownX = screenX; + gameProc.touchDownY = screenY; + gameProc.isTouchDown = false; } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java new file mode 100644 index 0000000..1ed455b --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -0,0 +1,77 @@ +package ru.deadsoftware.cavecraft.game; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavecraft.game.objects.Player; + +public class GamePhysics { + + public static final int PL_SPEED = 2; + + private GameProc gameProc; + + private Vector2 gravity; + + public GamePhysics(GameProc gameProc) { + this.gameProc = gameProc; + gravity = new Vector2(0,2); + } + + private boolean checkColl(Rectangle rect) { + int[] bl = new int [6]; + bl[0] = gameProc.world.getForeMap(((int)rect.x/16), ((int)rect.y/16)); + bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16)); + bl[2] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height/2)/16)); + bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16)); + bl[4] = gameProc.world.getForeMap(((int)rect.x/16), ((int)(rect.y+rect.height-1)/16)); + bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16)); + for (int b: bl) if (b>0) { + return true; + } + return false; + } + + private void movePlayer(Player pl) { + pl.position.add(pl.moveX); + if (checkColl(pl.getRect())) { + int d = 0; + if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1; + while (checkColl(pl.getRect())) pl.position.x+=d; + //pl.moveX.setZero(); + } + pl.position.add(pl.moveY); + if (checkColl(pl.getRect())) { + int d = 0; + if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1; + while (checkColl(pl.getRect())) pl.position.y+=d; + pl.moveY.setZero(); + } + pl.moveY.add(gravity); + switch (pl.dir) { + case 0: + gameProc.renderer.camTargetPos.x = pl.position.x-gameProc.renderer.camera.viewportWidth+100; + break; + case 1: + gameProc.renderer.camTargetPos.x = pl.position.x-100; + break; + } + } + + public void update(float delta) { + movePlayer(gameProc.player); + if (gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x <= 8 && + gameProc.renderer.camera.position.x - gameProc.renderer.camTargetPos.x >= -8) { + gameProc.renderer.camera.position.x = gameProc.renderer.camTargetPos.x; + } + if (gameProc.renderer.camera.position.x > gameProc.renderer.camTargetPos.x) { + gameProc.renderer.camera.position.sub(16,0,0); + } + if (gameProc.renderer.camera.position.x < gameProc.renderer.camTargetPos.x) { + gameProc.renderer.camera.position.add(16,0,0); + } + gameProc.renderer.camera.position.y = gameProc.player.position.y+gameProc.player.height/2 + -gameProc.renderer.camera.viewportHeight/2; + } + +} diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index bd91e72..61c64ff 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -1,13 +1,17 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.utils.TimeUtils; +import ru.deadsoftware.cavecraft.game.objects.Player; public class GameProc { public static double RUN_TIME = 0; + public Player player; + public GameWorld world; public GameRenderer renderer; + public GamePhysics physics; public int cursorX, cursorY; @@ -16,12 +20,21 @@ public class GameProc { public long touchDownTime; public GameProc() { - world = new GameWorld(512,16); + world = new GameWorld(512,32); + renderer = new GameRenderer(this); + physics = new GamePhysics(this); + player = new Player(); + } + + public void resetRenderer() { renderer = new GameRenderer(this); } public void update(float delta) { RUN_TIME += delta; + + physics.update(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 c6dd02f..563f095 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -5,53 +5,95 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector3; import ru.deadsoftware.cavecraft.GameScreen; +import ru.deadsoftware.cavecraft.game.objects.Player; public class GameRenderer { private GameProc gameProc; - OrthographicCamera camera; + public Vector3 camTargetPos; + public OrthographicCamera camera; ShapeRenderer shapeRenderer; public GameRenderer(GameProc gameProc) { - Gdx.gl.glClearColor(0f,.8f,.8f,1f); + Gdx.gl.glClearColor(0f,.6f,.6f,1f); this.gameProc = gameProc; camera = new OrthographicCamera(); - camera.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight()); + camera.setToOrtho(true, 320, + 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); camera.position.x=0; camera.position.y=0; + camTargetPos = camera.position.cpy(); shapeRenderer = new ShapeRenderer(); shapeRenderer.setProjectionMatrix(camera.combined); + shapeRenderer.setAutoShapeType(true); } - 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); - } + 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); + int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1; + int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1; + if (minX<0) minX=0; + if (minY<0) minY=0; + if (maxX>=gameProc.world.getWidth()) maxX = gameProc.world.getWidth()-1; + if (maxY>=gameProc.world.getHeight()) maxY = gameProc.world.getHeight()-1; + for (int y=minY; y0) { - shapeRenderer.setColor(Color.GRAY); - shapeRenderer.rect(x*32-camera.position.x, - y*32-camera.position.y,32,32); + setColor(128,128,128); + fillRect(x*16-camera.position.x, + y*16-camera.position.y,16,16); + setColor(0,0,0); + drawRect(x*16-camera.position.x, + y*16-camera.position.y,16,16); + } else { + if (gameProc.world.getBackMap(x,y)>0) { + setColor(64,64,64); + fillRect(x*16-camera.position.x, + y*16-camera.position.y,16,16); + setColor(0,0,0); + drawRect(x*16-camera.position.x, + y*16-camera.position.y,16,16); + } } + } } 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(); + drawRect(gameProc.cursorX*16-camera.position.x, + gameProc.cursorY*16-camera.position.y,16,16); + } + + private void drawPlayer(Player pl) { + setColor(0,128,0); + fillRect(pl.position.x - camera.position.x, + pl.position.y - camera.position.y, pl.width, pl.height); } public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + shapeRenderer.begin(); drawWorld(); + drawPlayer(gameProc.player); + shapeRenderer.end(); } } diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java index e3822d8..faf98ed 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -8,7 +8,7 @@ public class WorldGen { foreMap = new int[width][height]; backMap = new int[width][height]; for (int x=0; x