Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
korge-game-engine committed Jun 17, 2024
1 parent 75d4b6f commit 1942800
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- { uses: actions/checkout@v4 }
- { name: Set up JDK, uses: actions/setup-java@v3, with: { distribution: "${{ env.JAVA_DISTRIBUTION }}", java-version: "${{ env.JAVA_VERSION }}" } }
- { name: Prepare Gradle, uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 } # v3.1.0
- { name: Buid JS bundle, run: ./gradlew browserReleaseEsbuild }
#- { name: Buid JS bundle, run: ./gradlew browserReleaseWebpack } # available after 6.0.0-beta3
#- { name: Buid JS bundle, run: ./gradlew browserReleaseEsbuild }
- { name: Buid JS bundle, run: ./gradlew browserReleaseWebpack }
- { name: Upload artifact, uses: actions/upload-pages-artifact@v3, with: { path: 'build/www' } }
- { name: Deploy 🚀 to GitHub Pages, id: deployment, uses: actions/deploy-pages@v4}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[plugins]
korge = { id = "com.soywiz.korge", version = "6.0.0-alpha2" }
korge = { id = "com.soywiz.korge", version = "6.0.0-alpha3" }
#korge = { id = "com.soywiz.korge", version = "999.0.0.999" }
2 changes: 1 addition & 1 deletion kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2543,7 +2543,7 @@ [email protected]:
iconv-lite "^0.6.3"
source-map-js "^1.0.2"

source-map-support@0.5.21, source-map-support@~0.5.20:
source-map-support@~0.5.20:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
Expand Down
Binary file modified resources/tiles.ase
Binary file not shown.
142 changes: 142 additions & 0 deletions src/TileRules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import korlibs.datastructure.*
import korlibs.image.tiles.*
import korlibs.memory.*

fun IntGridToTileGrid(grid: IntArray2, rules: IRuleMatcher, tiles: TileMapData) {
for (y in 0 until grid.height) {
for (x in 0 until grid.width) {
tiles[x, y] = rules.get(grid, x, y)
}
}
}

fun IntArray2.getOr(x: Int, y: Int, default: Int = -1): Int {
if (!inside(x, y)) return default
return this[x, y]
}

fun IntArray2.setOr(x: Int, y: Int, value: Int) {
if (!inside(x, y)) return
this[x, y] = value
}

data class SimpleTileSpec(
val left: Boolean = false,
val up: Boolean = false,
val right: Boolean = false,
val down: Boolean = false,
) {
val bits: Int = bits(left, up, right, down)

companion object {
fun bits(left: Boolean, up: Boolean, right: Boolean, down: Boolean): Int = 0
.insert(left, 0).insert(up, 1).insert(right, 2).insert(down, 3)
}
}

fun Tile.flippedX(): Tile = Tile(tile, orientation.flippedX(), offsetX, offsetY)
fun Tile.flippedY(): Tile = Tile(tile, orientation.flippedY(), offsetX, offsetY)
fun Tile.rotated(): Tile = Tile(tile, orientation.rotatedRight(), offsetX, offsetY)

data class SimpleRule(
val tile: Tile,
val spec: SimpleTileSpec,
) {
val left get() = spec.left
val right get() = spec.right
val up get() = spec.up
val down get() = spec.down

constructor(tile: Tile, left: Boolean = false, up: Boolean = false, right: Boolean = false, down: Boolean = false) : this(tile, SimpleTileSpec(left, up, right, down))

fun flippedX(): SimpleRule = SimpleRule(tile.flippedX(), right, up, left, down)
fun flippedY(): SimpleRule = SimpleRule(tile.flippedY(), left, down, right, up)
fun rotated(): SimpleRule = SimpleRule(tile.rotated(), up = left, right = up, down = right, left = down)
//fun rotated(): SimpleRule = SimpleRule(tile.rotated(), up = right, right = down, down = left, left = up)

fun match(spec: SimpleTileSpec): Boolean {
return this.spec == spec
}
}

interface ISimpleTileProvider : IRuleMatcher {
fun get(spec: SimpleTileSpec): Tile
}

interface IRuleMatcher {
val maxDist: Int
fun get(ints: IntArray2, x: Int, y: Int): Tile
}

class CombinedRuleMatcher(val rules: List<IRuleMatcher>) : IRuleMatcher {
constructor(vararg rules: IRuleMatcher) : this(rules.toList())

override val maxDist: Int by lazy { rules.maxOf { it.maxDist } }

override fun get(ints: IntArray2, x: Int, y: Int): Tile {
for (rule in rules) {
val tile = rule.get(ints, x, y)
if (tile.isValid) return tile
}
return Tile.INVALID
}

}

open class SimpleTileProvider(val value: Int) : ISimpleTileProvider, IRuleMatcher {
override val maxDist: Int = 1

//val rules = mutableSetOf<SimpleRule>()
val ruleTable = arrayOfNulls<SimpleRule>(16)

companion object {
val FALSE = listOf(false)
val BOOLS = listOf(false, true)
}

fun rule(
rule: SimpleRule,
registerFlipX: Boolean = true,
registerFlipY: Boolean = true,
registerRotated: Boolean = true,
) {
for (fx in if (registerFlipX) BOOLS else FALSE) {
for (fy in if (registerFlipY) BOOLS else FALSE) {
for (rot in if (registerRotated) BOOLS else FALSE) {
var r = rule
if (rot) r = r.rotated()
if (fx) r = r.flippedX()
if (fy) r = r.flippedY()
val bits = r.spec.bits
if (ruleTable[bits] == null) ruleTable[bits] = r
//rules += r
}
}
}
}

override fun get(spec: SimpleTileSpec): Tile {
ruleTable[spec.bits]?.let { return it.tile }
//for (rule in rules) {
// if (rule.match(spec)) {
// return rule.tile
// }
//}
return Tile.INVALID
}

override fun get(ints: IntArray2, x: Int, y: Int): Tile {
if (ints.getOr(x, y) != value) return Tile.INVALID
val left = ints.getOr(x - 1, y) == value
val right = ints.getOr(x + 1, y) == value
val up = ints.getOr(x, y - 1) == value
val down = ints.getOr(x, y + 1) == value
return get(SimpleTileSpec(left, up, right, down))
}
}

fun TileMapData.pushInside(x: Int, y: Int, value: Tile) {
if (inside(x, y)) {
this.data.push(x, y, value.raw)
}
}
Loading

0 comments on commit 1942800

Please sign in to comment.