diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-17 16:54:04 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-17 16:54:04 +0100 |
commit | 3d970117fea5853cc055b9d13ffd71f869546d28 (patch) | |
tree | d04abf6eb3830bd207594d39dcda65235351b216 | |
parent | 38e9dd8d6b75ef112390acc9f5bc7dd603749a99 (diff) | |
download | RustAndDust-3d970117fea5853cc055b9d13ffd71f869546d28.zip RustAndDust-3d970117fea5853cc055b9d13ffd71f869546d28.tar.gz |
GameScreen: use GameCamera, notify Hud on resize()
-rw-r--r-- | core/src/ch/asynk/tankontank/screens/GameScreen.java | 102 |
1 files changed, 22 insertions, 80 deletions
diff --git a/core/src/ch/asynk/tankontank/screens/GameScreen.java b/core/src/ch/asynk/tankontank/screens/GameScreen.java index 8d7a8b3..f1eff5a 100644 --- a/core/src/ch/asynk/tankontank/screens/GameScreen.java +++ b/core/src/ch/asynk/tankontank/screens/GameScreen.java @@ -10,18 +10,11 @@ import com.badlogic.gdx.input.GestureDetector; import com.badlogic.gdx.input.GestureDetector.GestureAdapter; import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.Camera; -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.math.MathUtils; -import com.badlogic.gdx.utils.viewport.FitViewport; -import com.badlogic.gdx.utils.viewport.ScreenViewport; import ch.asynk.tankontank.TankOnTank; @@ -33,20 +26,15 @@ public class GameScreen implements Screen private static final float INPUT_DELAY = 0.1f; private static final float ZOOM_IN_MAX = 0.3f; + private static final float ZOOM_OUT_MAX = 1f; private static final float ZOOM_GESTURE_FACTOR = .01f; private static final float ZOOM_SCROLL_FACTOR = .1f; private static final int DRAGGED_Z_INDEX = 10; - private float maxZoomOut; - private float virtualWidth; - private float virtualHeight; + private final GameCamera cam; - private final OrthographicCamera cam; - private final FitViewport mapViewport; - private final ScreenViewport hudViewport; - - private final Batch mapBatch; - private final Batch hudBatch; + private final SpriteBatch mapBatch; + private final SpriteBatch hudBatch; private ShapeRenderer debugShapes = null; private final TankOnTank game; @@ -57,7 +45,6 @@ public class GameScreen implements Screen private Vector2 dragPos = new Vector2(); private Vector3 mapTouch = new Vector3(); private Vector3 hudTouch = new Vector3(); - private Vector2 screenToWorld = new Vector2(); public GameScreen(final TankOnTank game) { @@ -67,16 +54,7 @@ public class GameScreen implements Screen DEBUG = game.config.debug; - virtualWidth = ctrl.map.getWidth(); - virtualHeight = ctrl.map.getHeight(); - - cam = new OrthographicCamera(virtualWidth, virtualHeight); - cam.setToOrtho(false); - mapViewport = new FitViewport(virtualWidth, virtualHeight, cam); - mapViewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); - - hudViewport = new ScreenViewport(); - hudViewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); + cam = new GameCamera(ctrl.map.getWidth(), ctrl.map.getHeight(), ZOOM_OUT_MAX, ZOOM_IN_MAX); mapBatch = new SpriteBatch(); hudBatch = new SpriteBatch(); @@ -95,11 +73,9 @@ public class GameScreen implements Screen public boolean zoom(float initialDistance, float distance) { if (initialDistance > distance) - cam.zoom += ZOOM_GESTURE_FACTOR; + cam.zoom(ZOOM_GESTURE_FACTOR); else - cam.zoom -= ZOOM_GESTURE_FACTOR; - cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_IN_MAX, maxZoomOut); - clampCameraPos(); + cam.zoom(-ZOOM_GESTURE_FACTOR); blocked = true; inputDelay = INPUT_DELAY; return true; @@ -109,11 +85,8 @@ public class GameScreen implements Screen @Override public boolean touchDragged(int x, int y, int pointer) { - float deltaX = ((x - dragPos.x) * cam.zoom * screenToWorld.x); - float deltaY = ((dragPos.y - y) * cam.zoom * screenToWorld.y); + cam.translate((dragPos.x - x), (dragPos.y - y)); dragPos.set(x, y); - cam.translate(-deltaX, -deltaY, 0); - clampCameraPos(); return true; } @Override @@ -122,8 +95,8 @@ public class GameScreen implements Screen if (blocked) return true; if (button == Input.Buttons.LEFT) { dragPos.set(x, y); - unprojectToHud(x, y, hudTouch); - unprojectToMap(x, y, mapTouch); + cam.unproject(x, y, mapTouch); + cam.unprojectHud(x, y, hudTouch); ctrl.touchDown(hudTouch.x, hudTouch.y, mapTouch.x, mapTouch.y); } return true; @@ -133,8 +106,8 @@ public class GameScreen implements Screen { if (blocked) return true; if (button == Input.Buttons.LEFT) { - unprojectToHud(x, y, hudTouch); - unprojectToMap(x, y, mapTouch); + cam.unproject(x, y, mapTouch); + cam.unprojectHud(x, y, hudTouch); ctrl.touchUp(hudTouch.x, hudTouch.y, mapTouch.x, mapTouch.y); } return true; @@ -142,9 +115,7 @@ public class GameScreen implements Screen @Override public boolean scrolled(int amount) { - cam.zoom += amount * ZOOM_SCROLL_FACTOR; - cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_IN_MAX, maxZoomOut); - clampCameraPos(); + cam.zoom(amount * ZOOM_SCROLL_FACTOR); return true; } }); @@ -152,28 +123,6 @@ public class GameScreen implements Screen return multiplexer; } - private void unprojectToMap(int x, int y, Vector3 v) - { - cam.unproject(v.set(x, y, 0), mapViewport.getScreenX(), mapViewport.getScreenY(), - mapViewport.getScreenWidth(), mapViewport.getScreenHeight()); - } - - private void unprojectToHud(int x, int y, Vector3 v) - { - x -= mapViewport.getLeftGutterWidth(); - y += mapViewport.getBottomGutterHeight(); - hudViewport.getCamera().unproject(v.set(x, y, 0), hudViewport.getScreenX(), hudViewport.getScreenY(), - mapViewport.getScreenWidth(), mapViewport.getScreenHeight()); - } - - 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, (virtualWidth - cx)); - cam.position.y = MathUtils.clamp(cam.position.y, cy, (virtualHeight - cy)); - } - @Override public void render(float delta) { @@ -182,19 +131,20 @@ public class GameScreen implements Screen if (inputDelay <= 0f) blocked = false; } - Gdx.gl.glClearColor(0, 0, 0, 1); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - - cam.update(); ctrl.hud.animate(delta); ctrl.map.animate(delta); + Gdx.gl.glClearColor(0, 0, 0, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + // cam.update(); mapBatch.setProjectionMatrix(cam.combined); mapBatch.begin(); ctrl.map.draw(mapBatch); mapBatch.end(); + if (DEBUG) { Gdx.gl.glEnable(GL20.GL_BLEND); debugShapes.setAutoShapeType(true); @@ -204,9 +154,7 @@ public class GameScreen implements Screen debugShapes.end(); } - Camera hudCam = hudViewport.getCamera(); - hudCam.update(); - hudBatch.setProjectionMatrix(hudCam.combined); + hudBatch.setProjectionMatrix(cam.getHudMatrix()); hudBatch.begin(); ctrl.hud.draw(hudBatch, DEBUG); hudBatch.end(); @@ -214,7 +162,7 @@ public class GameScreen implements Screen if (DEBUG) { Gdx.gl.glEnable(GL20.GL_BLEND); debugShapes.setAutoShapeType(true); - debugShapes.setProjectionMatrix(hudCam.combined); + debugShapes.setProjectionMatrix(cam.getHudMatrix()); debugShapes.begin(); ctrl.hud.drawDebug(debugShapes); debugShapes.end(); @@ -225,14 +173,8 @@ public class GameScreen implements Screen public void resize(int width, int height) { // TankOnTank.debug("GameScreen", "resize (" + width + "," + height + ")"); - mapViewport.update(width, height); - // FIXME hudViewort must always match with map - // hudViewport.update(width, height); - - maxZoomOut = Math.min((virtualWidth / cam.viewportWidth), (virtualHeight / cam.viewportHeight)); - cam.zoom = MathUtils.clamp(cam.zoom, ZOOM_IN_MAX, maxZoomOut); - - screenToWorld.set((cam.viewportWidth / width), (cam.viewportHeight / height)); + cam.updateViewport(width, height); + ctrl.hud.resize(cam.getHudWidth(), cam.getHudHeight()); } @Override |