Fix ratio change not being handled correctly

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

Loading…
Cancel
Save