Fix ratio change not being handled correctly

viewport-stuff
trivernis 1 year ago
parent edfeb7b645
commit 55fe170808
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -21,15 +21,17 @@ import com.last.commit.map.TimeMap
import com.last.commit.stages.InventoryStage
import kotlin.math.floor
/** First screen of the application. Displayed after the application is created. */
/** First screen of the application. Displayed after the application is created. */
class FirstScreen : Screen, InputProcessor {
val viewportSize = 800f
private var delta = 0f
private var isColliding = false
val state = ColorState()
val batch = SpriteBatch()
val camera = OrthographicCamera(800f, 600f)
val camera = OrthographicCamera(viewportSize, viewportSize)
lateinit var map: TimeMap // = TimeMap("tiled/base.tmx")
val playerTexture = Texture("sprites/characters.png")
val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43))
@ -45,6 +47,7 @@ class FirstScreen : Screen, InputProcessor {
val gameConfig = this.loadGameConfig()
val randomMap = gameConfig.getRandomMap()
map = TimeMap(randomMap)
handleRatioChange()
this.spawnPlayer()
this.updateCamera()
@ -65,7 +68,7 @@ class FirstScreen : Screen, InputProcessor {
override fun render(delta: Float) {
this.delta = delta
// state.step((delta * 1000).toLong())
// state.step((delta * 1000).toLong())
// Draw your screen here. "delta" is the time since last render in seconds.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
Gdx.gl.glClearColor(state.red, state.green, state.blue, 1f)
@ -75,10 +78,7 @@ class FirstScreen : Screen, InputProcessor {
val mousePosition: Vector2 = getMousePosition()
player.lookAt(mousePosition)
val interactables = map.getInteractablesAt(
player.getAbsoluteDirection()
)
val interactables = map.getInteractablesAt(player.getAbsoluteDirection())
batch.projectionMatrix = camera.combined
batch.begin()
@ -86,10 +86,9 @@ class FirstScreen : Screen, InputProcessor {
this.player.render(batch)
batch.end()
//TODO: auslagern in sperate Methode
// TODO: auslagern in sperate Methode
renderInteractables(interactables)
inventoryStage.draw()
}
@ -101,10 +100,10 @@ class FirstScreen : Screen, InputProcessor {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
for (interactable in interactables) {
shapeRenderer.rect(
interactable.getCollider().x,
interactable.getCollider().y,
interactable.getCollider().width,
interactable.getCollider().height
interactable.getCollider().x,
interactable.getCollider().y,
interactable.getCollider().width,
interactable.getCollider().height
)
}
shapeRenderer.end()
@ -112,7 +111,8 @@ class FirstScreen : Screen, InputProcessor {
}
private fun getMousePosition(): Vector2 {
val unprojectedMousePosition = camera.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f))
val unprojectedMousePosition =
camera.unproject(Vector3(Gdx.input.x.toFloat(), Gdx.input.y.toFloat(), 0f))
return Vector2(unprojectedMousePosition.x, unprojectedMousePosition.y)
}
@ -177,29 +177,47 @@ class FirstScreen : Screen, InputProcessor {
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
}
halfScreenWidth
} else if (playerXPosition > mapWidth - halfScreenWidth) {
mapWidth - halfScreenWidth
} else {
playerXPosition
}
cY = if (playerYPosition < halfScreenHeight) {
halfScreenHeight
} else if (playerYPosition > mapHeight - halfScreenHeight) {
mapHeight - halfScreenHeight
} else {
playerYPosition
}
halfScreenHeight
} else if (playerYPosition > mapHeight - halfScreenHeight) {
mapHeight - halfScreenHeight
} else {
playerYPosition
}
camera.position[cX, cY] = 0f
camera.update()
}
override fun resize(width: Int, height: Int) {
inventoryStage.resize(width, height)
// Resize your screen here. The parameters represent the new window size.
inventoryStage.resize(width, height)
handleRatioChange()
}
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() {
@ -267,28 +285,28 @@ class FirstScreen : Screen, InputProcessor {
fun toWorldCoordinates(x: Float, y: Float): Vector2 {
val mouseInWorldPosition = camera.unproject(Vector3(x, y, 0f))
return Vector2(
floor(mouseInWorldPosition.x.toDouble() / this.map.getTileWidth()).toFloat(),
floor(mouseInWorldPosition.y.toDouble() / this.map.getTileHeight()).toFloat()
floor(mouseInWorldPosition.x.toDouble() / this.map.getTileWidth()).toFloat(),
floor(mouseInWorldPosition.y.toDouble() / this.map.getTileHeight()).toFloat()
)
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
//TODO: ("Not yet implemented")
// TODO: ("Not yet implemented")
return false
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
//TODO: ("Not yet implemented")
// TODO: ("Not yet implemented")
return false
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
//TODO: "Not yet implemented"
// TODO: "Not yet implemented"
return false
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
//TODO: Not yet implemented
// TODO: Not yet implemented
return false
}
}
}

Loading…
Cancel
Save