From 3e6bf5223f70f4db8c87140403ae2b4782842131 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 18 May 2020 16:04:17 +0200 Subject: [PATCH] made camera take into account map size --- core/src/netwerkprog/game/MainGame.java | 21 +++++++-- .../game/client/map/GameInputProcessor.java | 46 ++++++++++++++----- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/core/src/netwerkprog/game/MainGame.java b/core/src/netwerkprog/game/MainGame.java index ebbbc2f..bb7fbc8 100644 --- a/core/src/netwerkprog/game/MainGame.java +++ b/core/src/netwerkprog/game/MainGame.java @@ -39,8 +39,6 @@ public class MainGame extends ApplicationAdapter { screenHeight = Gdx.graphics.getHeight(); frameRate = new FrameRate(); camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); - camera.translate(0, 0); - camera.update(); String[] strings = new String[]{ @@ -62,7 +60,10 @@ public class MainGame extends ApplicationAdapter { gameInputProcessor = new GameInputProcessor(camera, this); Gdx.input.setInputProcessor(gameInputProcessor); mapRenderer = new MapRenderer(map, 32, batch, camera); - camera.translate(screenWidth/2,screenHeight/2); + camera.position.set(screenWidth/2,screenHeight/2,0); + camera.viewportWidth = screenWidth / 2; + camera.viewportHeight = screenHeight / 2; + camera.update(); // playSong(); @@ -130,7 +131,19 @@ public class MainGame extends ApplicationAdapter { batch.dispose(); } - private void keyDown() { + public float getScreenWidth() { + return screenWidth; + } + public float getScreenHeight() { + return screenHeight; + } + + public int getVerticalTileAmount() { + return map.getHeight(); + } + + public int getHorizontalTileAmount() { + return map.getWidth(); } } diff --git a/core/src/netwerkprog/game/client/map/GameInputProcessor.java b/core/src/netwerkprog/game/client/map/GameInputProcessor.java index 41cde72..6494842 100644 --- a/core/src/netwerkprog/game/client/map/GameInputProcessor.java +++ b/core/src/netwerkprog/game/client/map/GameInputProcessor.java @@ -3,6 +3,7 @@ package netwerkprog.game.client.map; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.TimeUtils; import netwerkprog.game.MainGame; @@ -35,6 +36,14 @@ public class GameInputProcessor implements InputProcessor { keysList.add(Input.Keys.S); keysList.add(Input.Keys.D); + camera.zoom = MathUtils.clamp(camera.zoom, 1.5f, 1.8f); +// +// float effectiveViewportWidth = camera.viewportWidth * camera.zoom; +// float effectiveViewportHeight = camera.viewportHeight * camera.zoom; +// +// camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, game.getScreenWidth() - effectiveViewportWidth / 2f); +// camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, game.getScreenHeight() - effectiveViewportHeight / 2f); + } public boolean isWPressed() { @@ -79,6 +88,7 @@ public class GameInputProcessor implements InputProcessor { @Override public boolean keyUp(int keycode) { + System.out.println(camera.position.x + " , " + camera.position.y); if (keysList.contains(keycode)) { if (keycode == keysList.get(0)) { this.isWPressed = false; @@ -96,6 +106,7 @@ public class GameInputProcessor implements InputProcessor { this.isDPressed = false; return true; } + return true; } return false; @@ -128,23 +139,34 @@ public class GameInputProcessor implements InputProcessor { @Override public boolean scrolled(int amount) { + float nextZoom = camera.zoom + amount / 5f; + if (nextZoom >= 0.5 && nextZoom <= 2.5) { + camera.zoom += amount / 2f; + return true; + } return false; } public void update() { long delta = TimeUtils.timeSinceMillis(lastTimeCounted); lastTimeCounted = TimeUtils.millis(); - if (isWPressed()) { - camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0); - } - if (isSPressed()) { - camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0); - } - if (isAPressed()) { - camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0); - } - if (isDPressed()) { - camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0); - } + if (camera.position.x > 5 * game.getHorizontalTileAmount()) + if (isAPressed()) { + camera.position.add(-CAMERA_MOVE_SPEED * delta, 0, 0); + } + if (camera.position.y < 30 * game.getVerticalTileAmount()) + if (isWPressed()) { + camera.position.add(0, CAMERA_MOVE_SPEED * delta, 0); + } + + if (camera.position.y > 5 * game.getVerticalTileAmount()) + if (isSPressed()) { + camera.position.add(0, -CAMERA_MOVE_SPEED * delta, 0); + } + + if (camera.position.x < game.getScreenWidth() / 2 + 5 * game.getHorizontalTileAmount()) + if (isDPressed()) { + camera.position.add(CAMERA_MOVE_SPEED * delta, 0, 0); + } } }