From 1d4da585ed4ee81d428a0b433ecaf4ebbd1552dd Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 10:58:10 +0100 Subject: [PATCH] Fix viewport issues --- .../com/last/commit/screen/FirstScreen.kt | 76 +++++++------------ .../com/last/commit/stages/DialogStage.kt | 2 + .../kotlin/com/last/commit/stages/UIStage.kt | 6 +- 3 files changed, 35 insertions(+), 49 deletions(-) diff --git a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt index 4e82b04..8c7662d 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -13,6 +13,9 @@ import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.utils.Json +import com.badlogic.gdx.utils.viewport.Viewport +import com.badlogic.gdx.utils.viewport.StretchViewport +import com.badlogic.gdx.utils.viewport.FillViewport import com.last.commit.Game import com.last.commit.Player import com.last.commit.audio.GameMusic @@ -34,6 +37,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { private var isColliding = false val batch = SpriteBatch() + val viewport = FillViewport(viewportSize, viewportSize) val camera = OrthographicCamera(viewportSize, viewportSize) var map: TimeMap @@ -56,14 +60,14 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { this.spawnPlayer() this.updateCamera() - handleRatioChange() - uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState) shapeRenderer.setAutoShapeType(true) player.addItemToInventory("drill") gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f) + viewport.camera = camera + viewport.apply() } override fun getInputProcessors(): Array { @@ -129,6 +133,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { val mousePosition: Vector2 = getMousePosition() player.lookAt(mousePosition) + batch.projectionMatrix = camera.combined batch.begin() this.map.render(batch, camera, delta) @@ -140,6 +145,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { uiStage.draw() gameState.dialogStage.draw() + viewport.apply() + updateCamera() } } @@ -227,60 +234,33 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { private fun checkCollision() { this.isColliding = map.isCollidingWith(player) } - - fun updateCamera() { - val cX: Float - val cY: Float - - val halfScreenWidth = camera.viewportWidth / 2 - val halfScreenHeight = camera.viewportHeight / 2 - val playerXPosition: Float = this.player.getX() - val playerYPosition: Float = this.player.getY() - val mapWidth: Int = map.width - val mapHeight: Int = map.height - - cX = if (playerXPosition < halfScreenWidth) { - halfScreenWidth - } else if (playerXPosition > mapWidth - halfScreenWidth) { - mapWidth - halfScreenWidth - } else { - playerXPosition - } - - cY = if (playerYPosition < halfScreenHeight) { - halfScreenHeight - } else if (playerYPosition > mapHeight - halfScreenHeight) { - mapHeight - halfScreenHeight - } else { - playerYPosition - } - - camera.position[cX, cY] = 0f + + + private fun updateCamera() { + val mapSize = Vector2(map.width.toFloat(), map.height.toFloat()) + + val scale = viewport.worldHeight / viewport.screenHeight + camera.position.x = MathUtils.clamp( + player.position.x, + (viewport.worldWidth / 2f) + (viewport.leftGutterWidth * scale), + mapSize.x - (viewport.worldWidth / 2f) - (viewport.rightGutterWidth * scale) + ) + camera.position.y = MathUtils.clamp( + player.position.y, + (viewport.worldHeight / 2f) + (viewport.topGutterHeight * scale), + mapSize.y - (viewport.worldHeight / 2f) - (viewport.bottomGutterHeight * scale) + ) camera.update() } - override fun resize(width: Int, height: Int) { // Resize your screen here. The parameters represent the new window size. uiStage.resize(width, height) gameState.dialogStage.resize(width, height) - handleRatioChange() + viewport.update(width, height) + viewport.apply() + camera.update() } - fun handleRatioChange() { - val height = Gdx.graphics.height - val width = Gdx.graphics.width - - val wRatio = width.toFloat() / height.toFloat() - val hRatio = height.toFloat() / width.toFloat() - if (wRatio < 1) { - camera.viewportWidth = viewportSize * wRatio - camera.viewportHeight = viewportSize - } else { - camera.viewportHeight = viewportSize * hRatio - camera.viewportWidth = viewportSize - } - updateCamera() - } override fun pause() { pause = true diff --git a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt index dff56d1..92fca30 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -38,10 +38,12 @@ class DialogStage(skin: Skin?) : Stage() { fun resize(width: Int, height: Int) { viewport.update(width, height, true) + this.camera.update() } override fun draw() { if (isVisible) { + this.viewport.apply() super.draw() } } diff --git a/core/src/main/kotlin/com/last/commit/stages/UIStage.kt b/core/src/main/kotlin/com/last/commit/stages/UIStage.kt index 7602727..2946df5 100644 --- a/core/src/main/kotlin/com/last/commit/stages/UIStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/UIStage.kt @@ -5,10 +5,13 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Label +import com.badlogic.gdx.utils.viewport.FitViewport +import com.badlogic.gdx.utils.viewport.ScreenViewport +import com.badlogic.gdx.utils.viewport.ExtendViewport import com.last.commit.GameState import com.last.commit.inventory.InventoryItemTextureLoader -class UIStage(path: String, val state: GameState) : Stage() { +class UIStage(path: String, val state: GameState) : Stage(ExtendViewport(512f, 512f)) { val textureLoader = InventoryItemTextureLoader(path) private val labelStyle = Label.LabelStyle(BitmapFont(), Color.BLACK) var mapLabel = Label("unknown time", labelStyle) @@ -45,6 +48,7 @@ class UIStage(path: String, val state: GameState) : Stage() { } override fun draw() { + this.viewport.apply() super.draw() }