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 4e82b04..8a1e7ef 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,13 @@ 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 +132,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 +144,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { uiStage.draw() gameState.dialogStage.draw() + viewport.apply() + updateCamera() } } @@ -163,7 +169,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) } @@ -227,60 +233,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 @@ -310,7 +289,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() 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..2f230d8 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -5,19 +5,20 @@ 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 init { area = TextArea("#", skin) - area.width = Gdx.graphics.width.toFloat() + area.width = viewport.worldWidth area.height = 100f addActor(area) } @@ -38,10 +39,13 @@ class DialogStage(skin: Skin?) : Stage() { fun resize(width: Int, height: Int) { viewport.update(width, height, true) + area.width = viewport.worldWidth + this.camera.update() } override fun draw() { if (isVisible) { + this.viewport.apply() super.draw() } } @@ -74,6 +78,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() 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() }