Merge remote-tracking branch 'origin/main'

# Conflicts:
#	assets/tiled/level_herbst.tmx
main
lihell 2 years ago
commit cfd51597cf

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

@ -1,21 +1,33 @@
package com.last.commit.inventory
class Inventory {
import com.last.commit.GameState
class Inventory() {
val items: MutableList<InventoryItem> = 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 }
}
}

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

@ -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<InputProcessor> {
@ -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()

@ -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<String>()
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()

@ -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()
}

Loading…
Cancel
Save