Fix viewport issues

main
trivernis 1 year ago
parent 739cebf937
commit 1d4da585ed
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -13,6 +13,9 @@ import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.utils.Json 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.Game
import com.last.commit.Player import com.last.commit.Player
import com.last.commit.audio.GameMusic import com.last.commit.audio.GameMusic
@ -34,6 +37,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
private var isColliding = false private var isColliding = false
val batch = SpriteBatch() val batch = SpriteBatch()
val viewport = FillViewport(viewportSize, viewportSize)
val camera = OrthographicCamera(viewportSize, viewportSize) val camera = OrthographicCamera(viewportSize, viewportSize)
var map: TimeMap var map: TimeMap
@ -56,14 +60,14 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
this.spawnPlayer() this.spawnPlayer()
this.updateCamera() this.updateCamera()
handleRatioChange()
uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState) uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState)
shapeRenderer.setAutoShapeType(true) shapeRenderer.setAutoShapeType(true)
player.addItemToInventory("drill") player.addItemToInventory("drill")
gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f) gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f)
viewport.camera = camera
viewport.apply()
} }
override fun getInputProcessors(): Array<InputProcessor> { override fun getInputProcessors(): Array<InputProcessor> {
@ -129,6 +133,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
val mousePosition: Vector2 = getMousePosition() val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition) player.lookAt(mousePosition)
batch.projectionMatrix = camera.combined batch.projectionMatrix = camera.combined
batch.begin() batch.begin()
this.map.render(batch, camera, delta) this.map.render(batch, camera, delta)
@ -140,6 +145,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
uiStage.draw() uiStage.draw()
gameState.dialogStage.draw() gameState.dialogStage.draw()
viewport.apply()
updateCamera()
} }
} }
@ -227,60 +234,33 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
private fun checkCollision() { private fun checkCollision() {
this.isColliding = map.isCollidingWith(player) this.isColliding = map.isCollidingWith(player)
} }
fun updateCamera() {
val cX: Float private fun updateCamera() {
val cY: Float val mapSize = Vector2(map.width.toFloat(), map.height.toFloat())
val halfScreenWidth = camera.viewportWidth / 2 val scale = viewport.worldHeight / viewport.screenHeight
val halfScreenHeight = camera.viewportHeight / 2 camera.position.x = MathUtils.clamp(
val playerXPosition: Float = this.player.getX() player.position.x,
val playerYPosition: Float = this.player.getY() (viewport.worldWidth / 2f) + (viewport.leftGutterWidth * scale),
val mapWidth: Int = map.width mapSize.x - (viewport.worldWidth / 2f) - (viewport.rightGutterWidth * scale)
val mapHeight: Int = map.height )
camera.position.y = MathUtils.clamp(
cX = if (playerXPosition < halfScreenWidth) { player.position.y,
halfScreenWidth (viewport.worldHeight / 2f) + (viewport.topGutterHeight * scale),
} else if (playerXPosition > mapWidth - halfScreenWidth) { mapSize.y - (viewport.worldHeight / 2f) - (viewport.bottomGutterHeight * scale)
mapWidth - halfScreenWidth )
} else {
playerXPosition
}
cY = if (playerYPosition < halfScreenHeight) {
halfScreenHeight
} else if (playerYPosition > mapHeight - halfScreenHeight) {
mapHeight - halfScreenHeight
} else {
playerYPosition
}
camera.position[cX, cY] = 0f
camera.update() camera.update()
} }
override fun resize(width: Int, height: Int) { override fun resize(width: Int, height: Int) {
// Resize your screen here. The parameters represent the new window size. // Resize your screen here. The parameters represent the new window size.
uiStage.resize(width, height) uiStage.resize(width, height)
gameState.dialogStage.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() { override fun pause() {
pause = true pause = true

@ -38,10 +38,12 @@ class DialogStage(skin: Skin?) : Stage() {
fun resize(width: Int, height: Int) { fun resize(width: Int, height: Int) {
viewport.update(width, height, true) viewport.update(width, height, true)
this.camera.update()
} }
override fun draw() { override fun draw() {
if (isVisible) { if (isVisible) {
this.viewport.apply()
super.draw() super.draw()
} }
} }

@ -5,10 +5,13 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.Label 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.GameState
import com.last.commit.inventory.InventoryItemTextureLoader 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) val textureLoader = InventoryItemTextureLoader(path)
private val labelStyle = Label.LabelStyle(BitmapFont(), Color.BLACK) private val labelStyle = Label.LabelStyle(BitmapFont(), Color.BLACK)
var mapLabel = Label("unknown time", labelStyle) var mapLabel = Label("unknown time", labelStyle)
@ -45,6 +48,7 @@ class UIStage(path: String, val state: GameState) : Stage() {
} }
override fun draw() { override fun draw() {
this.viewport.apply()
super.draw() super.draw()
} }

Loading…
Cancel
Save