Skip to content

Commit

Permalink
Update to KorGE 6.0.0-alpha5
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jun 20, 2024
1 parent 7c531a4 commit bd2e2df
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 185 deletions.
36 changes: 4 additions & 32 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
import korlibs.korge.gradle.*

plugins {
alias(libs.plugins.korge)
}

korge {
id = "com.sample.demo"

// To enable all targets at once

//targetAll()

// To enable targets based on properties/environment variables
//targetDefault()

// To selectively enable targets

targetJvm()
targetJs()
targetWasmJs()
targetDesktop()
targetIos()
targetAndroid()

serializationJson()
}


dependencies {
add("commonMainApi", project(":deps"))
//add("commonMainApi", project(":korge-dragonbones"))
}
plugins { id("com.soywiz.korge") version korlibs.korge.gradle.common.KorgeGradlePluginVersion.VERSION }
korge { id = "org.korge.unknown.game"; loadYaml(file("korge.yaml")) }
dependencies { findProject(":deps")?.also { add("commonMainApi", it) } }
val baseFile = file("build.extra.gradle.kts").takeIf { it.exists() }?.also { apply(from = it) }
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml

This file was deleted.

2 changes: 2 additions & 0 deletions korge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: org.korge.snake
targets: [all]
20 changes: 2 additions & 18 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@ pluginManagement {
repositories { mavenLocal(); mavenCentral(); google(); gradlePluginPortal() }
}

buildscript {
val libsTomlFile = File(this.sourceFile?.parentFile, "gradle/libs.versions.toml").readText()
var plugins = false
var version = ""
for (line in libsTomlFile.lines().map { it.trim() }) {
if (line.startsWith("#")) continue
if (line.startsWith("[plugins]")) plugins = true
if (plugins && line.startsWith("korge") && Regex("^korge\\s*=.*").containsMatchIn(line)) version = Regex("version\\s*=\\s*\"(.*?)\"").find(line)?.groupValues?.get(1) ?: error("Can't find korge version")
}
if (version.isEmpty()) error("Can't find korge version in $libsTomlFile")

repositories { mavenLocal(); mavenCentral(); google(); gradlePluginPortal() }

dependencies {
classpath("com.soywiz.korge.settings:com.soywiz.korge.settings.gradle.plugin:$version")
}
plugins {
id("com.soywiz.korge.settings") version "6.0.0-alpha5"
}

apply(plugin = "com.soywiz.korge.settings")

rootProject.name = "korge-snake"
76 changes: 5 additions & 71 deletions src/TileRules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,6 @@ import korlibs.memory.*
import kotlinx.atomicfu.*
import kotlin.math.*

open class ObservableIntArray2(val width: Int, val height: Int, val default: Int = 0) {
val data = IntArray2(width, height, default)
//private val listeners = Signal2<ObservableIntArray2, RectangleInt>()

open fun updated(rect: RectangleInt) {
}

private var locked = atomic(0)
private var bb = BoundsBuilder()

inline fun <T> lock(block: () -> T): T {
lock()
try {
return block()
} finally {
unlock()
}
}

@PublishedApi internal fun lock() {
locked.incrementAndGet()
}
@PublishedApi internal fun unlock() {
if (locked.decrementAndGet() <= 0) {
flush()
}
}

private fun flush() {
if (locked.value == 0 && bb.isNotEmpty) {
updated(bb.bounds.toInt())
bb = BoundsBuilder.EMPTY
}
}

operator fun set(rect: RectangleInt, value: Int) {
val l = rect.left.coerceIn(0, width)
val r = rect.right.coerceIn(0, width)
val u = rect.top.coerceIn(0, height)
val d = rect.bottom.coerceIn(0, height)
for (x in l until r) {
for (y in u until d) {
data.setOr(x, y, value)
}
}
bb += rect.toFloat()
flush()
}

operator fun get(p: PointInt): Int = this[p.x, p.y]
operator fun get(x: Int, y: Int): Int = if (data.inside(x, y)) data[x, y] else default
operator fun set(p: PointInt, value: Int) { this[p.x, p.y] = value }

operator fun set(x: Int, y: Int, value: Int) {
if (data.inside(x, y)) data[x, y] = value
bb += Rectangle(x, y, 1, 1)
flush()
}
}

fun IntGridToTileGrid(ints: IntArray2, rules: IRuleMatcher, tiles: TileMapData, updated: RectangleInt = RectangleInt(0, 0, ints.width, ints.height)) {
val l = (updated.left - rules.maxDist).coerceIn(0, ints.width)
val r = (updated.right + rules.maxDist).coerceIn(0, ints.width)
Expand All @@ -77,16 +17,6 @@ fun IntGridToTileGrid(ints: IntArray2, rules: IRuleMatcher, tiles: TileMapData,
}
}

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,
Expand Down Expand Up @@ -114,7 +44,9 @@ data class SimpleRule(
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))
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)
Expand Down Expand Up @@ -204,6 +136,8 @@ class SimpleTileProvider(val value: Int) : ISimpleTileProvider, IRuleMatcher {
val down = ints.getOr(x, y + 1) == value
return get(SimpleTileSpec(left, up, right, down))
}

fun IntArray2.getOr(x: Int, y: Int): Int = if (inside(x, y)) get(x, y) else 0
}

data class TileMatch(val id: Int, val offset: PointInt, val eq: Boolean = true) : IRuleMatcherMatch {
Expand Down
85 changes: 24 additions & 61 deletions src/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import korlibs.image.color.*
import korlibs.image.format.*
import korlibs.image.tiles.*
import korlibs.io.file.std.*
import korlibs.io.file.sync.*
import korlibs.korge.*
import korlibs.korge.input.*
import korlibs.korge.scene.*
Expand All @@ -17,27 +18,27 @@ import korlibs.math.random.*
import korlibs.time.*
import kotlin.random.*

suspend fun main() = Korge(windowSize = Size(256 * 2, 196 * 2), backgroundColor = Colors.DIMGRAY) {
suspend fun main() = Korge(windowSize = Size(256 * 2, 196 * 2), backgroundColor = Colors.DIMGRAY, displayMode = KorgeDisplayMode.CENTER.copy(clipBorders = false)) {
println("fir-korge-extension-test")
val sceneContainer = sceneContainer()

sceneContainer.changeTo { MyScene() }
}

//class MirroredInt

class MyScene : PixelatedScene(256 * 2, 196 * 2, sceneSmoothing = true) {
//class MyScene : PixelatedScene(256 * 2, 196 * 2, sceneSmoothing = true) {
class MyScene : ScaledScene(256 * 2, 196 * 2, sceneSmoothing = true) {
override suspend fun SContainer.sceneMain() {
val tilesIDC = resourcesVfs["tiles.ase"].readImageDataContainer(ASE)
val tiles = tilesIDC.mainBitmap.slice()

val tileSet = TileSet(tiles.splitInRows(16, 16).mapIndexed { index, slice -> TileSetTileInfo(index, slice) })
val tileMap = tileMap(TileMapData(32, 24, tileSet = tileSet))
val snakeMap = tileMap(TileMapData(32, 24, tileSet = tileSet))
val ints = object : ObservableIntArray2(tileMap.map.width, tileMap.map.height, GROUND) {
val rules = CombinedRuleMatcher(WallsProvider, AppleProvider)
override fun updated(rect: RectangleInt) {
IntGridToTileGrid(this.data, rules, tileMap.map, rect)
}
val rules = CombinedRuleMatcher(WallsProvider, AppleProvider)
val ints = IntArray2(tileMap.map.width, tileMap.map.height, GROUND).observe { rect ->
IntGridToTileGrid(this.base as IntArray2, rules, tileMap.map, rect)
}
ints.lock {
ints[RectangleInt(0, 0, ints.width, 1)] = WALL
Expand All @@ -59,32 +60,11 @@ class MyScene : PixelatedScene(256 * 2, 196 * 2, sceneSmoothing = true) {

putRandomApple()

//for (n in 0 until 64) tileMap.map[n, 0] = Tile(n, SliceOrientation.NORMAL)

//var tile = 1
//var tile = 11
//var tile = 4
//var tile = 5
//var offset = Point(0, 0)
//var orientation = SliceOrientation.NORMAL
//tileMap.map.push(4, 4, Tile(7))
//tileMap.map.push(4, 4, Tile(tile, orientation))

var snake = Snake(listOf(PointInt(5, 5)), maxLen = 8)
.withExtraMove(SnakeMove.RIGHT)
//.withExtraMove(SnakeMove.RIGHT)
snake.render(ints, snakeMap.map)

//fun updateOrientation(
// updateOffset: (Point) -> Point = { it },
// update: (SliceOrientation) -> SliceOrientation = { it }
//) {
// offset = updateOffset(offset)
// orientation = update(orientation)
// println("orientation=$orientation")
// tileMap.map[4, 4] = Tile(tile, orientation, offset.x.toInt(), offset.y.toInt())
//}

var direction: SnakeMove = SnakeMove.RIGHT

fun snakeMove(move: SnakeMove) {
Expand All @@ -110,27 +90,6 @@ class MyScene : PixelatedScene(256 * 2, 196 * 2, sceneSmoothing = true) {
down(Key.UP) { direction = SnakeMove.UP }
down(Key.DOWN) { direction = SnakeMove.DOWN }
down(Key.SPACE) { speed = if (speed != 0.0) 0.0 else 1.0 }

//down(Key.LEFT) { updateOrientation { it.rotatedLeft() } }
//down(Key.RIGHT) { updateOrientation { it.rotatedRight() } }
//down(Key.Y) { updateOrientation { it.flippedY() } }
//down(Key.X) { updateOrientation { it.flippedX() } }
//downFrame(Key.W, dt = 16.milliseconds) { updateOrientation(updateOffset = { it + Point(0, -1) }) }
//downFrame(Key.A, dt = 16.milliseconds) { updateOrientation(updateOffset = { it + Point(-1, 0) }) }
//downFrame(Key.S, dt = 16.milliseconds) { updateOrientation(updateOffset = { it + Point(0, +1) }) }
//downFrame(Key.D, dt = 16.milliseconds) { updateOrientation(updateOffset = { it + Point(+1, 0) }) }
}
}
}

operator fun IntArray2.set(rect: RectangleInt, value: Int) {
val l = rect.left.coerceIn(0, width)
val r = rect.right.coerceIn(0, width)
val u = rect.top.coerceIn(0, height)
val d = rect.bottom.coerceIn(0, height)
for (x in l until r) {
for (y in u until d) {
this.setOr(x, y, value)
}
}
}
Expand Down Expand Up @@ -203,14 +162,23 @@ data class Snake(val pos: List<PointInt>, val maxLen: Int = 10) {
val tile = when {
isFirst || isLast -> {
val p = if (isLast) p0 else p1
(if (isLast) SnakeHeadProvider else SnakeProvider).get(SimpleTileSpec(p.left, p.up, p.right, p.down))
(if (isLast) SnakeHeadProvider else SnakeProvider).get(
SimpleTileSpec(
p.left,
p.up,
p.right,
p.down
)
)
}
else -> SnakeProvider.get(SimpleTileSpec(
left = p0.comingFromLeft || p1.left,
up = p0.comingFromUp || p1.up,
right = p0.comingFromRight || p1.right,
down = p0.comingFromDown || p1.down
))
else -> SnakeProvider.get(
SimpleTileSpec(
left = p0.comingFromLeft || p1.left,
up = p0.comingFromUp || p1.up,
right = p0.comingFromRight || p1.right,
down = p0.comingFromDown || p1.down
)
)
}
map.pushInside(p.x, p.y, tile)
ints[p] = 3
Expand Down Expand Up @@ -242,11 +210,6 @@ object WallsProvider : ISimpleTileProvider by (SimpleTileProvider(value = 1).als
it.rule(SimpleRule(Tile(21), up = true, left = true, right = true, down = true))
})

operator fun IntArray2.get(p: PointInt): Int = this.getOr(p.x, p.y)
operator fun IntArray2.set(p: PointInt, value: Int) { if (inside(p.x, p.y)) this[p.x, p.y] = value }
//fun IStackedIntArray2.get(p: PointInt): Int = this[p.x, p.y]
//fun IStackedLongArray2.get(p: PointInt): Long = this[p.x, p.y]

val GROUND = 0
val WALL = 1
val APPLE = 2

0 comments on commit bd2e2df

Please sign in to comment.