From 5953f033f372856446501ddb8ca423a17e9ee0c6 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 3 Feb 2022 00:09:24 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=20=D0=B7=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D1=81=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=82=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/ru/dbotthepony/kstarbound/Main.kt | 29 ++++++++++---- .../ru/dbotthepony/kstarbound/Starbound.kt | 34 +++++++++++++++- .../kstarbound/defs/TileDefinition.kt | 39 ++++++++++++++----- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt index b5e14b53..088b608a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt @@ -125,7 +125,7 @@ private fun init() { glfwShowWindow(window) Starbound.addFilePath(File("./unpacked_assets/")) - Starbound.loadTileDefinition("alienrock") + Starbound.loadTileMaterials() } private fun loop() { @@ -138,10 +138,10 @@ private fun loop() { state.blend = true glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) - val rock = ChunkTile(Starbound.loadTileDefinition("alienrock")) - val obsidian = ChunkTile(Starbound.loadTileDefinition("obsidian")) - val corruptdirt = ChunkTile(Starbound.loadTileDefinition("corruptdirt")) - val darkwood = ChunkTile(Starbound.loadTileDefinition("darkwood")) + val rock = ChunkTile(Starbound.getTileDefinition("alienrock")!!) + val obsidian = ChunkTile(Starbound.getTileDefinition("obsidian")!!) + val corruptdirt = ChunkTile(Starbound.getTileDefinition("corruptdirt")!!) + val darkwood = ChunkTile(Starbound.getTileDefinition("darkwood")!!) val chunk = Starbound.world.setTile(Vector2i(2, 2), rock) chunk[3, 2] = rock @@ -161,13 +161,28 @@ private fun loop() { chunk[2, 1] = darkwood chunk[3, 1] = darkwood - for (x in 0 until 1) { for (y in 0 until 4) { //chunk[x, y] = ChunkTile(Starbound.loadTileDefinition("alienrock")) } } + var x = 0 + var y = 0 + + for (tile in Starbound.tilesAccess.values) { + chunk[x++, y] = ChunkTile(tile) + chunk[x++, y] = ChunkTile(tile) + chunk[x++, y] = ChunkTile(tile) + chunk[x++, y] = ChunkTile(tile) + chunk[x++, y] = ChunkTile(tile) + + if (x >= 24) { + x = 0 + y++ + } + } + val chunkRenderer = ChunkRenderer(state, chunk) chunkRenderer.tesselateStatic() chunkRenderer.uploadStatic() @@ -206,7 +221,7 @@ private fun loop() { //glDrawElements(GL_TRIANGLES, chunkRenderer.indexCount, GL_UNSIGNED_INT, 0) //checkForGLError() - state.matrixStack.push().scale(x = 100f, y = 100f).translateWithScale(0f, 0f) + state.matrixStack.push().scale(x = 20f, y = 20f).translateWithScale(0f, 0f) chunkRenderer.render() //state.matrixStack.translateWithScale(18f) //chunkRenderer.render() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index 57334633..58e374f7 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -9,9 +9,14 @@ import ru.dbotthepony.kstarbound.defs.TileRenderTemplate import ru.dbotthepony.kstarbound.world.World import java.io.File import java.io.FileNotFoundException +import java.io.FilenameFilter + +class TileDefLoadingException(message: String, cause: Throwable? = null) : IllegalStateException(message, cause) object Starbound { - val tiles = HashMap() + private val tiles = HashMap() + val tilesAccess = object : Map by tiles {} + val world = World() private val _filepath = ArrayList() @@ -52,4 +57,31 @@ object Starbound { return@computeIfAbsent TileDefinitionBuilder.fromJson(JsonParser.parseReader(foundPath.bufferedReader()) as JsonObject).build(foundPath.parent) } } + + fun getTileDefinition(name: String): TileDefinition? { + return tiles[name] + } + + fun loadTileMaterials() { + for (sPath in _filepath) { + val newPath = File(sPath.path, "tiles/materials") + + if (newPath.exists() && newPath.isDirectory) { + val findFiles = newPath.listFiles()!! + + for (listedFile in findFiles) { + if (listedFile.path.endsWith(".material")) { + try { + val tileDef = TileDefinitionBuilder.fromJson(JsonParser.parseReader(listedFile.bufferedReader()) as JsonObject).build(listedFile.parent) + + check(tiles[tileDef.materialName] == null) { "Already has material with ID ${tileDef.materialName} loaded!" } + tiles[tileDef.materialName] = tileDef + } catch(err: Throwable) { + throw TileDefLoadingException("Loading tile file ${listedFile.name}", err) + } + } + } + } + } + } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/TileDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/TileDefinition.kt index 383bee61..0e6ccb5c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/TileDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/TileDefinition.kt @@ -82,9 +82,9 @@ class TileDefinitionBuilder { require(builder.materialId >= 0) { "Invalid materialId ${builder.materialId}" } builder.particleColor = Color(input["particleColor"].asJsonArray) - builder.itemDrop = input["itemDrop"].asString - builder.description = input["description"].asString - builder.shortdescription = input["shortdescription"].asString + builder.itemDrop = input["itemDrop"]?.asString + builder.description = input["description"]?.asString ?: "..." + builder.shortdescription = input["shortdescription"]?.asString ?: "..." builder.footstepSound = input["footstepSound"]?.asString builder.health = input["health"].asInt builder.category = input["category"].asString @@ -98,9 +98,9 @@ class TileDefinitionBuilder { input["renderParameters"]?.asJsonObject?.let { builder.render.texture = it["texture"].asString builder.render.variants = it["variants"].asInt - builder.render.lightTransparent = it["lightTransparent"].asBoolean - builder.render.occludesBelow = it["occludesBelow"].asBoolean - builder.render.multiColored = it["multiColored"].asBoolean + builder.render.lightTransparent = it["lightTransparent"]?.asBoolean ?: false + builder.render.occludesBelow = it["occludesBelow"]?.asBoolean ?: true + builder.render.multiColored = it["multiColored"]?.asBoolean ?: false builder.render.zLevel = it["zLevel"].asInt } @@ -168,7 +168,10 @@ sealed class RenderRule(params: Map) { "EqualsSelf" -> RenderRuleEqualsSelf(params) "Shadows" -> RenderRuleShadows(params) - else -> throw IllegalArgumentException("Unknown tile render rule $name") + // неизвестно что оно делает + "Connects" -> AlwaysFailingRenderRule(params) + + else -> throw IllegalArgumentException("Unknown render rule '$name'") } } @@ -213,6 +216,18 @@ class RenderRuleShadows(params: Map) : RenderRule(params) { } } +class AlwaysPassingRenderRule(params: Map) : RenderRule(params) { + override fun test(getter: IChunk, thisRef: TileDefinition, thisPos: Vector2i, offsetPos: Vector2i): Boolean { + return inverse + } +} + +class AlwaysFailingRenderRule(params: Map) : RenderRule(params) { + override fun test(getter: IChunk, thisRef: TileDefinition, thisPos: Vector2i, offsetPos: Vector2i): Boolean { + return !inverse + } +} + enum class RenderRuleCombination { ALL, ANY @@ -384,6 +399,8 @@ data class TileRenderMatch( } } +class TileRenderTemplateParseException(message: String, cause: Throwable?) : IllegalStateException(message, cause) + data class TileRenderTemplate( val representativePiece: String, val pieces: Map, @@ -395,8 +412,12 @@ data class TileRenderTemplate( fun load(path: String): TileRenderTemplate { return map.computeIfAbsent(path) { - val json = Starbound.loadJson(path).asJsonObject - return@computeIfAbsent fromJson(json) + try { + val json = Starbound.loadJson(path).asJsonObject + return@computeIfAbsent fromJson(json) + } catch (err: Throwable) { + throw TileRenderTemplateParseException("Failed to load tile render definition from $path", err) + } } }