Merge remote-tracking branch 'origin/main'

# Conflicts:
#	assets/tiled/level_herbst.tmx
main
lihell 1 year ago
commit cfd51597cf

@ -22,10 +22,6 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga
position = Vector2() position = Vector2()
} }
fun addItemToInventory(name: String) {
gameState.inventory.add(name)
}
fun getX(): Float { fun getX(): Float {
return position.x return position.x
} }

@ -1,21 +1,33 @@
package com.last.commit.inventory package com.last.commit.inventory
class Inventory { import com.last.commit.GameState
class Inventory() {
val items: MutableList<InventoryItem> = ArrayList() val items: MutableList<InventoryItem> = ArrayList()
public var updated = false public var updated = false
/** /**
* @param name the name of the subtexture loaded from xml * @param name the name of the subtexture loaded from xml
* @return wether the item was added
*/ */
fun add(name: String) { fun add(name: String, state: GameState): Boolean {
if (this.items.size < 8) { if (this.items.find { it.name == name } == null) {
items.add(InventoryItem(name)) if (this.items.size < 8) {
this.updated = true 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) { fun remove(name: String) {
items.removeIf() {item -> item.name == name} items.removeIf() { item -> item.name == name }
} }
} }

@ -24,8 +24,9 @@ class Collectible(
override fun interact(otherCollider: Rectangle, state: GameState) { override fun interact(otherCollider: Rectangle, state: GameState) {
println("Interacting with item $name") println("Interacting with item $name")
state.soundEngine.play(GameSoundEffect.GRAB) state.soundEngine.play(GameSoundEffect.GRAB)
state.inventory.add(this.name) if (state.inventory.add(this.name, state)) {
state.map?.collectibles?.remove(this) state.map?.collectibles?.remove(this)
}
} }
override fun canInteract(state: GameState): Boolean { override fun canInteract(state: GameState): Boolean {

@ -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,13 @@ 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")
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 +132,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 +144,8 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
uiStage.draw() uiStage.draw()
gameState.dialogStage.draw() gameState.dialogStage.draw()
viewport.apply()
updateCamera()
} }
} }
@ -163,7 +169,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
private fun getMousePosition(): Vector2 { private fun getMousePosition(): Vector2 {
val unprojectedMousePosition = 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) return Vector2(unprojectedMousePosition.x, unprojectedMousePosition.y)
} }
@ -227,60 +233,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
@ -310,7 +289,7 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() {
} }
fun toWorldCoordinates(x: Float, y: Float): Vector2 { 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( 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()

@ -5,19 +5,20 @@ import com.badlogic.gdx.Input
import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.TextArea import com.badlogic.gdx.scenes.scene2d.ui.TextArea
import com.badlogic.gdx.utils.viewport.ExtendViewport
/** /**
* Stage for dialog * Stage for dialog
*/ */
class DialogStage(skin: Skin?) : Stage() { class DialogStage(skin: Skin?) : Stage(ExtendViewport(512f, 512f)) {
private var isVisible = false private var isVisible = false
private val texts = com.badlogic.gdx.utils.Array<String>() private val texts = com.badlogic.gdx.utils.Array<String>()
private val area: TextArea private val area: TextArea
init { init {
area = TextArea("#", skin) area = TextArea("#", skin)
area.width = Gdx.graphics.width.toFloat() area.width = viewport.worldWidth
area.height = 100f area.height = 100f
addActor(area) addActor(area)
} }
@ -38,10 +39,13 @@ 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)
area.width = viewport.worldWidth
this.camera.update()
} }
override fun draw() { override fun draw() {
if (isVisible) { if (isVisible) {
this.viewport.apply()
super.draw() super.draw()
} }
} }
@ -74,6 +78,10 @@ class DialogStage(skin: Skin?) : Stage() {
return true return true
} }
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return keyDown(Input.Keys.SPACE)
}
private operator fun next() { private operator fun next() {
area.clear() area.clear()
area.text = texts.first() area.text = texts.first()

@ -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