From bd5d36de7d5fd8d8bf8b27dbe64cc79010f1f4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Fri, 12 Sep 2014 16:24:10 +0200 Subject: move camera position clamp from render() to input processor --- core/src/ch/asynk/tankontank/screens/GameScreen.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/src/ch/asynk/tankontank/screens/GameScreen.java b/core/src/ch/asynk/tankontank/screens/GameScreen.java index 9f86bcb..a460519 100644 --- a/core/src/ch/asynk/tankontank/screens/GameScreen.java +++ b/core/src/ch/asynk/tankontank/screens/GameScreen.java @@ -69,6 +69,7 @@ public class GameScreen extends AbstractScreen { cam.zoom += ((initialDistance - distance) / 300.f); cam.zoom = MathUtils.clamp(cam.zoom, 0.4f, maxZoom); + clampCameraPos(); return true; } })); @@ -79,6 +80,7 @@ public class GameScreen extends AbstractScreen cam.translate((touchX - x) * cam.zoom, (y - touchY) * cam.zoom, 0); touchX = x; touchY = y; + clampCameraPos(); return true; } @Override @@ -95,6 +97,7 @@ public class GameScreen extends AbstractScreen { cam.zoom += amount / ZOOM_FACTOR; cam.zoom = MathUtils.clamp(cam.zoom, 0.4f, maxZoom); + clampCameraPos(); return true; } }); @@ -102,17 +105,20 @@ public class GameScreen extends AbstractScreen return multiplexer; } + private void clampCameraPos() + { + float cx = cam.viewportWidth * cam.zoom / 2f; + float cy = cam.viewportHeight * cam.zoom / 2f; + cam.position.x = MathUtils.clamp(cam.position.x, cx, (map.getWidth() - cx)); + cam.position.y = MathUtils.clamp(cam.position.y, cy, (map.getHeight() - cy)); + } + @Override public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - // handleInput(); - float width = cam.viewportWidth * cam.zoom / 2f; - float height = cam.viewportHeight * cam.zoom / 2f; - cam.position.x = MathUtils.clamp(cam.position.x, width, (mapSprite.getWidth() - width)); - cam.position.y = MathUtils.clamp(cam.position.y, height, (mapSprite.getHeight() - height)); cam.update(); batch.setProjectionMatrix(cam.combined); -- cgit v1.1-2-g2b99