From 1d4da585ed4ee81d428a0b433ecaf4ebbd1552dd Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 10:58:10 +0100 Subject: [PATCH 1/5] 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() } From 4d108068b3843db1247b96e78cb863b27fd45b03 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 20 Nov 2022 11:00:40 +0100 Subject: [PATCH 2/5] add inventory check items can only be used once --- assets/tiled/level_herbst.tmx | 12 ++++++++- .../src/main/kotlin/com/last/commit/Player.kt | 4 --- .../com/last/commit/inventory/Inventory.kt | 26 ++++++++++++++----- .../kotlin/com/last/commit/map/Collectible.kt | 5 ++-- .../com/last/commit/screen/FirstScreen.kt | 1 - .../com/last/commit/stages/DialogStage.kt | 4 +++ 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/assets/tiled/level_herbst.tmx b/assets/tiled/level_herbst.tmx index 647bfbf..f6941a1 100644 --- a/assets/tiled/level_herbst.tmx +++ b/assets/tiled/level_herbst.tmx @@ -1,5 +1,5 @@ - + @@ -504,6 +504,16 @@ + + + + + + + + + + diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index 0d320b9..65e0af5 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -22,10 +22,6 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga position = Vector2() } - fun addItemToInventory(name: String) { - gameState.inventory.add(name) - } - fun getX(): Float { return position.x } diff --git a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt index b76e193..6dbfcc0 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -1,21 +1,33 @@ package com.last.commit.inventory -class Inventory { +import com.last.commit.GameState + +class Inventory() { val items: MutableList = ArrayList() public var updated = false /** * @param name the name of the subtexture loaded from xml + * @return wether the item was added */ - fun add(name: String) { - if (this.items.size < 8) { - items.add(InventoryItem(name)) - this.updated = true + fun add(name: String, state: GameState): Boolean { + if (this.items.find { it.name == name } == null) { + if (this.items.size < 8) { + items.add(InventoryItem(name)) + this.updated = true + + return true + } + } else { + // Item is already in inventory + state.dialogStage.setTexts("You already have this item.") + state.dialogStage.show() } + return false } - + fun remove(name: String) { - items.removeIf() {item -> item.name == name} + items.removeIf() { item -> item.name == name } } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/map/Collectible.kt b/core/src/main/kotlin/com/last/commit/map/Collectible.kt index ce1a9e7..d4b8b7a 100644 --- a/core/src/main/kotlin/com/last/commit/map/Collectible.kt +++ b/core/src/main/kotlin/com/last/commit/map/Collectible.kt @@ -24,8 +24,9 @@ class Collectible( override fun interact(otherCollider: Rectangle, state: GameState) { println("Interacting with item $name") state.soundEngine.play(GameSoundEffect.GRAB) - state.inventory.add(this.name) - state.map?.collectibles?.remove(this) + if (state.inventory.add(this.name, state)) { + state.map?.collectibles?.remove(this) + } } override fun canInteract(state: GameState): Boolean { 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 8c7662d..34eb4fc 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -63,7 +63,6 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState) shapeRenderer.setAutoShapeType(true) - player.addItemToInventory("drill") gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f) viewport.camera = camera 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 92fca30..82f37b5 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -76,6 +76,10 @@ class DialogStage(skin: Skin?) : Stage() { return true } + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + return keyDown(Input.Keys.SPACE) + } + private operator fun next() { area.clear() area.text = texts.first() From 29225109941f44d41ba1c9ec7ec42c34e7732ed8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 11:03:21 +0100 Subject: [PATCH 3/5] Fix viewport issue in ui stage --- core/src/main/kotlin/com/last/commit/stages/DialogStage.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 92fca30..fed24ca 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -5,12 +5,13 @@ import com.badlogic.gdx.Input import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.scenes.scene2d.ui.TextArea +import com.badlogic.gdx.utils.viewport.ExtendViewport /** * Stage for dialog */ -class DialogStage(skin: Skin?) : Stage() { +class DialogStage(skin: Skin?) : Stage(ExtendViewport(512f, 512f)) { private var isVisible = false private val texts = com.badlogic.gdx.utils.Array() private val area: TextArea From 20c4e152512ee306ee713ab55c7e2976d043a803 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 11:08:13 +0100 Subject: [PATCH 4/5] Fix player rotation --- core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 8c7662d..afdb188 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -170,7 +170,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { private fun getMousePosition(): Vector2 { val unprojectedMousePosition = - camera.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f)) + viewport.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f)) return Vector2(unprojectedMousePosition.x, unprojectedMousePosition.y) } @@ -290,7 +290,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { } fun toWorldCoordinates(x: Float, y: Float): Vector2 { - val mouseInWorldPosition = camera.unproject(Vector3(x, y, 0f)) + val mouseInWorldPosition = viewport.unproject(Vector3(x, y, 0f)) return Vector2( floor(mouseInWorldPosition.x.toDouble() / this.map.getTileWidth()).toFloat(), floor(mouseInWorldPosition.y.toDouble() / this.map.getTileHeight()).toFloat() From c5db7ca5ac946970ee4c62d90c803f4fc1f5ec9e Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 11:11:05 +0100 Subject: [PATCH 5/5] Fix scaling of dialog in dialog stage --- core/src/main/kotlin/com/last/commit/stages/DialogStage.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 4ddb52f..2f230d8 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -18,7 +18,7 @@ class DialogStage(skin: Skin?) : Stage(ExtendViewport(512f, 512f)) { init { area = TextArea("#", skin) - area.width = Gdx.graphics.width.toFloat() + area.width = viewport.worldWidth area.height = 100f addActor(area) } @@ -39,6 +39,7 @@ class DialogStage(skin: Skin?) : Stage(ExtendViewport(512f, 512f)) { fun resize(width: Int, height: Int) { viewport.update(width, height, true) + area.width = viewport.worldWidth this.camera.update() }