generated from korlibs/korge-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75d4b6f
commit 1942800
Showing
7 changed files
with
278 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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== | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.