Add adding an item to the inventory when interacting with it

viewport-stuff
trivernis 1 year ago
parent d23f0bdab0
commit cf99f9b6f7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -20,9 +20,10 @@ import com.last.commit.map.Interactable
import com.last.commit.map.TimeMap
import com.last.commit.stages.InventoryStage
import kotlin.math.floor
import GameState
/** First screen of the application. Displayed after the application is created. */
class FirstScreen : Screen, InputProcessor {
class FirstScreen(val gameState: GameState) : Screen, InputProcessor {
val viewportSize = 800f
@ -34,7 +35,7 @@ class FirstScreen : Screen, InputProcessor {
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))
val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43), gameState)
var shapeRenderer = ShapeRenderer()
val highlightColor = Color(0f, 0f, 1f, 0.5f)
@ -46,14 +47,14 @@ class FirstScreen : Screen, InputProcessor {
val gameConfig = this.loadGameConfig()
val randomMap = gameConfig.getRandomMap()
map = TimeMap(randomMap)
map = TimeMap(randomMap, gameState)
handleRatioChange()
this.spawnPlayer()
this.updateCamera()
player.addItemToInventory("drill")
inventoryStage = InventoryStage(player.inventory)
inventoryStage = InventoryStage("sprites/genericItems_spritesheet_colored", gameState.inventory)
shapeRenderer.setAutoShapeType(true)
Gdx.input.setInputProcessor(this)
@ -258,7 +259,7 @@ class FirstScreen : Screen, InputProcessor {
} else if (character == 'i') {
inventoryStage.visible = !inventoryStage.visible
} else if (character == 'p') {
player.inventory.add("compass")
gameState.inventory.add("compass")
inventoryStage.refresh()
}
return false

@ -1,11 +1,22 @@
package com.last.commit
import com.badlogic.gdx.Game
import com.last.commit.inventory.Inventory
import GameState
/** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */
class Game : Game() {
private lateinit var state: GameState
override fun create() {
setScreen(FirstScreen())
createState()
setScreen(FirstScreen(state))
}
fun createState() {
state = GameState(
Inventory()
)
}
}

@ -0,0 +1,5 @@
import com.last.commit.inventory.Inventory
public data class GameState(
public val inventory: Inventory,
)

@ -5,9 +5,10 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
import com.last.commit.inventory.Inventory
import GameState
class Player(private val textureRegion: TextureRegion) : Collidable {
class Player(private val textureRegion: TextureRegion, private val gameState: GameState) : Collidable {
private var collider: Rectangle = Rectangle(0f, 0f, 0f, 0f)
var position: Vector2 = Vector2.Zero
@ -15,8 +16,6 @@ class Player(private val textureRegion: TextureRegion) : Collidable {
private val movementSpeed = 200f
private val interactionRange = 60f
val inventory = Inventory("sprites/genericItems_spritesheet_colored")
init {
val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat()
collider = Rectangle(0f, 0f, size, size)
@ -24,7 +23,7 @@ class Player(private val textureRegion: TextureRegion) : Collidable {
}
fun addItemToInventory(name: String) {
this.inventory.add(name)
gameState.inventory.add(name)
}
fun getX(): Float {

@ -1,18 +1,20 @@
package com.last.commit.inventory
class Inventory(path: String) {
class Inventory {
val items: MutableList<InventoryItem> = ArrayList()
val textureLoader = InventoryItemTextureLoader(path)
init {
textureLoader.parse()
}
public var updated = false
private set
/**
* @param name the name of the subtexture loaded from xml
*/
fun add(name: String) {
items.add(InventoryItem(name, textureLoader.loadTexture(name)))
items.add(InventoryItem(name))
this.updated = true
}
fun remove(name: String) {
items.removeIf() {item -> item.name == name}
}
}

@ -2,6 +2,5 @@ package com.last.commit.inventory
import com.badlogic.gdx.graphics.g2d.TextureRegion
class InventoryItem(val name: String, val texture: TextureRegion) {
data class InventoryItem(public val name: String) {
}

@ -12,19 +12,26 @@ class InventoryItemTextureLoader(path: String) {
private val itemsSpriteSheet: Texture
private val textureMapping: FileHandle
private lateinit var subTextures: Array<XmlReader.Element>
private val textures: HashMap<String, TextureRegion> = HashMap()
init {
itemsSpriteSheet = Texture("${path}.png")
textureMapping = Gdx.files.local("${path}.xml")
}
fun loadTexture(itemName: String): TextureRegion {
var subtexture = subTextures.first { it.getAttribute("name") == itemName }
val x = subtexture.getIntAttribute("x")
val y = subtexture.getIntAttribute("y")
val width = subtexture.getIntAttribute("width")
val height = subtexture.getIntAttribute("height")
return TextureRegion(itemsSpriteSheet, x, y, width, height)
fun getTexture(itemName: String): TextureRegion {
var itemTexture = textures.get(itemName)
if (itemTexture == null) {
var subtexture = subTextures.first { it.getAttribute("name") == itemName }
val x = subtexture.getIntAttribute("x")
val y = subtexture.getIntAttribute("y")
val width = subtexture.getIntAttribute("width")
val height = subtexture.getIntAttribute("height")
itemTexture = TextureRegion(itemsSpriteSheet, x, y, width, height)
this.textures.set(itemName, itemTexture)
}
return itemTexture
}
fun parse() {
@ -33,5 +40,4 @@ class InventoryItemTextureLoader(path: String) {
this.subTextures = textureAtlasElement.getChildrenByName("SubTexture")
println("Found ${subTextures.size} textures")
}
}
}

@ -2,6 +2,7 @@ package com.last.commit.map
import com.badlogic.gdx.math.Rectangle
import Position
import GameState
class Collectible(
name: String,
@ -18,8 +19,9 @@ class Collectible(
this.collider = Rectangle(pos.x, pos.y, width, height)
}
override fun interact(otherCollider: Rectangle) {
override fun interact(otherCollider: Rectangle, state: GameState) {
println("Interacting with item $name")
state.inventory.add(this.name)
}
override fun getCollider(): Rectangle {

@ -1,13 +1,14 @@
package com.last.commit.map
import GameState
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell
import com.badlogic.gdx.math.Rectangle
import com.last.commit.Wall
class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
Wall(gridX, gridY, wallCollider, cell), Interactable {
override fun interact(otherCollider: Rectangle) {
Wall(gridX, gridY, wallCollider, cell), Interactable {
override fun interact(otherCollider: Rectangle, state: GameState) {
println("interacting with door $this")
if (isClosed) {
isOpen = true
@ -42,8 +43,12 @@ class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) :
override fun toString(): String {
return String.format(
"Door: %f:%f - %f:%f (isOpen: %b)", wallCollider.x, wallCollider.y, wallCollider.width,
wallCollider.height, isOpen
"Door: %f:%f - %f:%f (isOpen: %b)",
wallCollider.x,
wallCollider.y,
wallCollider.width,
wallCollider.height,
isOpen
)
}
}
}

@ -1,9 +1,10 @@
package com.last.commit.map
import com.badlogic.gdx.math.Rectangle
import GameState
interface Interactable {
fun getCollider(): Rectangle
fun interact(otherCollider: Rectangle)
fun interact(otherCollider: Rectangle, state: GameState)
}

@ -15,9 +15,10 @@ import com.last.commit.Collidable
import com.last.commit.Player
import com.last.commit.Wall
import Position
import GameState
class TimeMap(fileName: String) {
class TimeMap(fileName: String, val state: GameState) {
private val CELL_SIZE = 64
private val walls = Array<Wall>()
@ -168,7 +169,7 @@ class TimeMap(fileName: String) {
val interactable: Interactable = this.findInteractableAtPosition(gridX, gridY) ?: return
//else continue
interactable.interact(blockingCollider)
interactable.interact(blockingCollider, state)
}

@ -3,8 +3,14 @@ package com.last.commit.stages
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.last.commit.inventory.Inventory
import com.last.commit.inventory.InventoryItemTextureLoader
class InventoryStage(val inventory: Inventory) : Stage() {
class InventoryStage(path: String, val inventory: Inventory) : Stage() {
val textureLoader = InventoryItemTextureLoader(path)
init {
textureLoader.parse()
}
var visible = false
set(visible) {
@ -17,7 +23,7 @@ class InventoryStage(val inventory: Inventory) : Stage() {
fun refresh() {
super.clear()
inventory.items.forEachIndexed { index, inventoryItem ->
val image = Image(inventoryItem.texture)
val image = Image(textureLoader.getTexture(inventoryItem.name))
image.x = index * 32f
image.width = 32f
image.height = 32f
@ -31,6 +37,9 @@ class InventoryStage(val inventory: Inventory) : Stage() {
}
override fun draw() {
if (inventory.updated) {
this.refresh()
}
if (visible) {
super.draw()
}

Loading…
Cancel
Save