From 2d47d57039cfc1864bdd7e232b6bdefeeff2f7a9 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 20 Nov 2022 11:59:50 +0100 Subject: [PATCH] Add victory condition check and screen --- .../main/kotlin/com/last/commit/GameState.kt | 3 +- .../com/last/commit/inventory/Inventory.kt | 9 ++- .../inventory/InventoryItemTextureLoader.kt | 5 ++ .../kotlin/com/last/commit/map/Collectible.kt | 4 +- .../kotlin/com/last/commit/map/MapState.kt | 57 +++++++++++-------- .../kotlin/com/last/commit/map/TimeMap.kt | 4 +- .../com/last/commit/screen/FirstScreen.kt | 7 ++- .../kotlin/com/last/commit/stages/UIStage.kt | 1 + 8 files changed, 59 insertions(+), 31 deletions(-) diff --git a/core/src/main/kotlin/com/last/commit/GameState.kt b/core/src/main/kotlin/com/last/commit/GameState.kt index a6f40b2..a20eb95 100644 --- a/core/src/main/kotlin/com/last/commit/GameState.kt +++ b/core/src/main/kotlin/com/last/commit/GameState.kt @@ -14,4 +14,5 @@ data class GameState( val assetManager: TimeTravelAssetManager, var map: MapState? = null, val dialogStage: DialogStage -) \ No newline at end of file +) { +} \ No newline at end of file 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 6dbfcc0..892aeee 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -5,7 +5,8 @@ import com.last.commit.GameState class Inventory() { val items: MutableList = ArrayList() - public var updated = false + var updated = false + private val requiredItems = listOf("ram", "iphone", "ipod", "screwdriver") /** * @param name the name of the subtexture loaded from xml @@ -30,4 +31,10 @@ class Inventory() { fun remove(name: String) { items.removeIf() { item -> item.name == name } } + + fun checkVictoryCondition(): Boolean { + return requiredItems.all {itemName -> + items.find { it.name == itemName } != null + } + } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt index 4c2322c..a8c917c 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt @@ -13,6 +13,7 @@ class InventoryItemTextureLoader(path: String) { private val textureMapping: FileHandle private lateinit var subTextures: Array private val textures: HashMap = HashMap() + private var initialized: Boolean = false init { itemsSpriteSheet = Texture("${path}.png") @@ -20,6 +21,9 @@ class InventoryItemTextureLoader(path: String) { } fun getTexture(itemName: String): TextureRegion { + if (!initialized) { + this.parse() + } var itemTexture = textures.get(itemName) if (itemTexture == null) { @@ -39,5 +43,6 @@ class InventoryItemTextureLoader(path: String) { val textureAtlasElement = xml.parse(textureMapping) this.subTextures = textureAtlasElement.getChildrenByName("SubTexture") println("Found ${subTextures.size} textures") + this.initialized = true } } 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 d4b8b7a..a294225 100644 --- a/core/src/main/kotlin/com/last/commit/map/Collectible.kt +++ b/core/src/main/kotlin/com/last/commit/map/Collectible.kt @@ -2,6 +2,7 @@ package com.last.commit.map import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 +import com.badlogic.gdx.scenes.scene2d.ui.Image import com.last.commit.GameState import com.last.commit.audio.GameSoundEffect import com.last.commit.inventory.InventoryItem @@ -10,7 +11,8 @@ class Collectible( name: String, val pos: Position, val size: Vector2, - val requiredItem: String + val requiredItem: String, + val image: Image ) : Interactable { val name: String diff --git a/core/src/main/kotlin/com/last/commit/map/MapState.kt b/core/src/main/kotlin/com/last/commit/map/MapState.kt index ea95c37..b5538dc 100644 --- a/core/src/main/kotlin/com/last/commit/map/MapState.kt +++ b/core/src/main/kotlin/com/last/commit/map/MapState.kt @@ -1,29 +1,29 @@ package com.last.commit.map -import com.last.commit.map.Collectible -import com.last.commit.map.Door -import com.last.commit.Wall -import com.badlogic.gdx.maps.tiled.TiledMap -import com.badlogic.gdx.maps.tiled.TiledMapTileLayer import com.badlogic.gdx.maps.MapObject import com.badlogic.gdx.maps.objects.RectangleMapObject -import com.badlogic.gdx.math.Vector2 +import com.badlogic.gdx.maps.tiled.TiledMap +import com.badlogic.gdx.maps.tiled.TiledMapTileLayer import com.badlogic.gdx.math.Rectangle -import kotlin.math.round; +import com.badlogic.gdx.math.Vector2 +import com.badlogic.gdx.scenes.scene2d.ui.Image +import com.last.commit.Wall +import com.last.commit.inventory.InventoryItemTextureLoader +import kotlin.math.round -class MapState(val map: TiledMap) { +class MapState(val map: TiledMap, val textureLoader: InventoryItemTextureLoader) { private val CELL_SIZE = 64 val size: Vector2 val gridSize: Vector2 - val tileSize: Vector2 + val tileSize: Vector2 val description: String? val collectibles: ArrayList = ArrayList() val teleporters: ArrayList = ArrayList() val walls: ArrayList = ArrayList() val doors: ArrayList = ArrayList() - + init { val prop = map.properties val gridWidth = prop.get("width", Int::class.java) @@ -50,8 +50,8 @@ class MapState(val map: TiledMap) { println("No Teleporters defined!") } } - - private fun loadCollectibles() { + + private fun loadCollectibles() { val collectiableLayer = map.layers["Collectibles"] if (collectiableLayer == null) { println("Could not load collectibles layer. Check map.") @@ -62,7 +62,7 @@ class MapState(val map: TiledMap) { println("Loaded ${collectibles.size} collectibles") } - + private fun createCollectible(obj: MapObject): Collectible? { val x = obj.properties.get("x", Float::class.java) val y = obj.properties.get("y", Float::class.java) @@ -72,13 +72,18 @@ class MapState(val map: TiledMap) { val width = obj.properties.get("width", Float::class.java) val height = obj.properties.get("height", Float::class.java) val size = Vector2(width, height) - + return if (obj is RectangleMapObject) { val itemName: String? = obj.properties.get("item", String::class.java) val requiredItem = obj.properties.get("requiredItem", String::class.java) ?: "" if (itemName != null) { - Collectible(itemName, Position(coords, gridCoords), size, requiredItem) + val image = Image(textureLoader.getTexture(itemName)) + image.x = coords.x + tileSize.x * 0.1f + image.y = coords.y + tileSize.y * 0.1f + image.width = tileSize.x * 0.8f + image.height = tileSize.y * 0.8f + Collectible(itemName, Position(coords, gridCoords), size, requiredItem, image) } else { null } @@ -94,14 +99,17 @@ class MapState(val map: TiledMap) { for (column in 0 until wallsLayer.width) { for (row in 0 until wallsLayer.height) { - val cell = wallsLayer.getCell(column, row)?: continue - val isDoor: Boolean = cell.getTile().getProperties().get("isDoor", false, Boolean::class.java) - - val wallCollider = Rectangle( - column.toFloat() * wallsLayer.tileWidth, - row.toFloat() * wallsLayer.tileHeight, wallsLayer.tileWidth.toFloat(), - wallsLayer.tileHeight.toFloat() - ) + val cell = wallsLayer.getCell(column, row) ?: continue + val isDoor: Boolean = + cell.getTile().getProperties().get("isDoor", false, Boolean::class.java) + + val wallCollider = + Rectangle( + column.toFloat() * wallsLayer.tileWidth, + row.toFloat() * wallsLayer.tileHeight, + wallsLayer.tileWidth.toFloat(), + wallsLayer.tileHeight.toFloat() + ) if (java.lang.Boolean.TRUE == isDoor) { doors.add(Door(column, row, wallCollider, cell)) } else { @@ -110,5 +118,4 @@ class MapState(val map: TiledMap) { } } } - -} \ No newline at end of file +} diff --git a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt index b606d0c..736e08a 100644 --- a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt +++ b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt @@ -46,7 +46,7 @@ class TimeMap(fileName: String, val state: GameState) { init { map = mapLoader.load(fileName) - mapState = MapState(map) + mapState = MapState(map, textureLoader) state.map = mapState mapStates[fileName] = mapState mapRenderer = OrthogonalTiledMapRenderer(map) @@ -74,7 +74,7 @@ class TimeMap(fileName: String, val state: GameState) { } else { val map = mapLoader.load(name) mapRenderer.map = map - mapState = MapState(map) + mapState = MapState(map, textureLoader) mapStates[name] = mapState } state.map = mapState 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 8a1e7ef..47c05b8 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -123,7 +123,12 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { } override fun render(delta: Float) { - if (!pause) { + if (gameState.inventory.checkVictoryCondition()) { + gameState.dialogStage.show() + gameState.dialogStage.act(delta) + gameState.dialogStage.setTexts("You won!") + gameState.dialogStage.draw() + } else if (!pause) { uiStage.act(delta) gameState.dialogStage.act(delta) handleInput(delta) 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 2946df5..3b2cddf 100644 --- a/core/src/main/kotlin/com/last/commit/stages/UIStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/UIStage.kt @@ -2,6 +2,7 @@ package com.last.commit.stages import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.BitmapFont +import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Label