From 54066d0514c1eed421f8a8d15e947601b02010ec Mon Sep 17 00:00:00 2001 From: soywiz Date: Sat, 7 Oct 2017 18:41:37 +0200 Subject: [PATCH] Added simon --- .../{build.gradle => build.gradle.old} | 0 korge-simon/common/build.gradle | 12 ++ korge-simon/common/settings.gradle | 1 + .../src/generated/resources/kotlin.atlas.json | 135 ++++++++++++++++ .../resources/kotlin.atlas.json.meta | 1 + .../src/generated/resources/kotlin.atlas.png | Bin 0 -> 664 bytes .../com/soywiz/korge/samples/simon/Simon.kt | 47 ++++-- .../src/main}/resources/index.html | 0 .../src/main}/resources/korge-ui.png | Bin .../src/main}/resources/kotlin.atlas | 0 .../src/main}/resources/kotlin.psd | Bin .../src/main}/resources/kotlin/0.png | Bin .../src/main}/resources/kotlin/1.png | Bin .../src/main}/resources/kotlin/2.png | Bin .../src/main}/resources/kotlin/3.png | Bin .../src/main}/resources/kotlin/4.png | Bin .../src/main}/resources/kotlin/5.png | Bin .../src/main}/resources/kotlin_images.png | Bin .../src/main}/resources/sounds/0.mp3 | Bin .../src/main}/resources/sounds/1.mp3 | Bin .../src/main}/resources/sounds/2.mp3 | Bin .../src/main}/resources/sounds/3.mp3 | Bin .../src/main}/resources/sounds/4.mp3 | Bin .../src/main}/resources/sounds/5.mp3 | Bin .../src/main}/resources/sounds/6.mp3 | Bin .../src/main}/resources/sounds/7.mp3 | Bin .../src/main}/resources/sounds/8.mp3 | Bin .../src/main}/resources/sounds/fail.mp3 | Bin .../src/main}/resources/sounds/success.wav | Bin korge-simon/jvm/build.gradle | 17 ++ korge-simon/jvm/settings.gradle | 1 + .../soywiz/korge/tictactoe/BoardMediator.kt | 3 +- .../src/com.soywiz.korge.tictactoe/Board.kt | 64 -------- .../BoardMediator.kt | 92 ----------- .../com.soywiz.korge.tictactoe/InjectExt.kt | 10 -- .../com.soywiz.korge.tictactoe/TicTacToe.kt | 146 ------------------ settings.gradle | 4 + 37 files changed, 204 insertions(+), 329 deletions(-) rename korge-simon/{build.gradle => build.gradle.old} (100%) create mode 100644 korge-simon/common/build.gradle create mode 100644 korge-simon/common/settings.gradle create mode 100644 korge-simon/common/src/generated/resources/kotlin.atlas.json create mode 100644 korge-simon/common/src/generated/resources/kotlin.atlas.json.meta create mode 100644 korge-simon/common/src/generated/resources/kotlin.atlas.png rename korge-simon/{src => common/src/main/kotlin}/com/soywiz/korge/samples/simon/Simon.kt (78%) rename korge-simon/{ => common/src/main}/resources/index.html (100%) rename korge-simon/{ => common/src/main}/resources/korge-ui.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin.atlas (100%) rename korge-simon/{ => common/src/main}/resources/kotlin.psd (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/0.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/1.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/2.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/3.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/4.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin/5.png (100%) rename korge-simon/{ => common/src/main}/resources/kotlin_images.png (100%) rename korge-simon/{ => common/src/main}/resources/sounds/0.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/1.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/2.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/3.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/4.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/5.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/6.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/7.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/8.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/fail.mp3 (100%) rename korge-simon/{ => common/src/main}/resources/sounds/success.wav (100%) create mode 100644 korge-simon/jvm/build.gradle create mode 100644 korge-simon/jvm/settings.gradle delete mode 100644 korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/Board.kt delete mode 100644 korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/BoardMediator.kt delete mode 100644 korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/InjectExt.kt delete mode 100644 korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/TicTacToe.kt diff --git a/korge-simon/build.gradle b/korge-simon/build.gradle.old similarity index 100% rename from korge-simon/build.gradle rename to korge-simon/build.gradle.old diff --git a/korge-simon/common/build.gradle b/korge-simon/common/build.gradle new file mode 100644 index 0000000..70a61a1 --- /dev/null +++ b/korge-simon/common/build.gradle @@ -0,0 +1,12 @@ +apply plugin: 'kotlin-platform-common' + +sourceSets { + main.resources.srcDirs = [ 'src/main/resources', 'src/generated/resources' ] +} + +dependencies { + compile "com.soywiz:korge-common:$korVersion" + compile "com.soywiz:korge-ext-swf-common:$korVersion" + compile "com.soywiz:korge-ext-ui-common:$korVersion" + compile "com.soywiz:korau-mp3-common:$korVersion" +} diff --git a/korge-simon/common/settings.gradle b/korge-simon/common/settings.gradle new file mode 100644 index 0000000..9879513 --- /dev/null +++ b/korge-simon/common/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'korge-simon-common' diff --git a/korge-simon/common/src/generated/resources/kotlin.atlas.json b/korge-simon/common/src/generated/resources/kotlin.atlas.json new file mode 100644 index 0000000..3e8fbac --- /dev/null +++ b/korge-simon/common/src/generated/resources/kotlin.atlas.json @@ -0,0 +1,135 @@ +{ + "frames": { + "0.png": { + "frame": { + "x": 2, + "y": 2, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + }, + "1.png": { + "frame": { + "x": 2, + "y": 42, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + }, + "2.png": { + "frame": { + "x": 2, + "y": 82, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + }, + "3.png": { + "frame": { + "x": 2, + "y": 122, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + }, + "4.png": { + "frame": { + "x": 2, + "y": 162, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + }, + "5.png": { + "frame": { + "x": 2, + "y": 202, + "w": 32, + "h": 40 + }, + "rotated": false, + "sourceSize": { + "w": 32, + "h": 40 + }, + "spriteSourceSize": { + "x": 0, + "y": 0, + "w": 32, + "h": 40 + }, + "trimmed": false + } + }, + "meta": { + "app": "korge", + "format": "RGBA8888", + "image": "kotlin.atlas.png", + "scale": 1.0, + "size": { + "w": 32, + "h": 240 + }, + "version": "0.10.0" + } +} \ No newline at end of file diff --git a/korge-simon/common/src/generated/resources/kotlin.atlas.json.meta b/korge-simon/common/src/generated/resources/kotlin.atlas.json.meta new file mode 100644 index 0000000..71878b1 --- /dev/null +++ b/korge-simon/common/src/generated/resources/kotlin.atlas.json.meta @@ -0,0 +1 @@ +{"name":"kotlin.atlas","loaderVersion":0,"sha1":"f096d009db1e83bdd171bdd91f830bcb32e2be70","configSha1":""} \ No newline at end of file diff --git a/korge-simon/common/src/generated/resources/kotlin.atlas.png b/korge-simon/common/src/generated/resources/kotlin.atlas.png new file mode 100644 index 0000000000000000000000000000000000000000..e992e2369b76f78773148e59a49ec1780a265427 GIT binary patch literal 664 zcmV;J0%!e+P)WnfX9vWun^gQ%=0Ma3t zViLCY{$9U(Ux4(&#-5_K@cOc&FPK``STZM5-BHUB002O`Ewg4#82|vRS=czmzj9^1 z5C8y71%MDj=oTTHQb2bIOD4P-6qb(veh%sSnk!aHOhN$8T(Q=0gg7BqM`Q?K&Evkc zbEe?{t#hW~m9w(LuGYx)TmWnMeD;xVOZZGPrQ`sJ?KS`a+y;P}LWB@nW7i|UYh=Ak z4zGB8X`Vs4bz?OuYW`t(qI%{ZrtJG<*9E4{fvbQ0b1zIR&G2*wHTAZ>T69bSz(2H% ze+qy#EQbQrLNos1db?z52?1CaV478c5JCtc`3buzOsUj2cudg);nxAS8n^a>h6eG0fx5jqi^vT${$7x8;2^YM}0w;l5Q_wa43cI25?;ky#OI;xD|kn y%d<7b{dCfISsd57?1nx$fLqYHH2{$h-Busd3#S-jJ*nRS0000) = Korge(this) + @JvmStatic + fun main(args: Array) = Korge(this, injector = AsyncInjector() + .mapPrototype { SelectLevelScene(getPath("kotlin.atlas"), get()) } + .mapPrototype { + IngameScene( + getPath(Atlas::class, "kotlin.atlas"), + getPath(SoundFile::class, "sounds/success.wav"), + getPath(SoundFile::class, "sounds/fail.mp3"), + getOrNull(), + get() + ) + } + ) - override val size: SizeInt = SizeInt(1280, 720) - override val windowSize: SizeInt = size * 0.75 - - override val title: String = "Kotlin Simon" - - override val icon: String = "kotlin/0.png" - //override val mainScene: Class = MainScene::class.java - override val mainScene: Class = SelectLevelScene::class.java + override val size = SizeInt(1280, 720) + override val windowSize = size * 0.75 + override val title = "Kotlin Simon" + override val icon = "kotlin/0.png" + override val mainScene = SelectLevelScene::class + override val plugins: List = super.plugins + listOf(UIPlugin) class Sequence( val max: Int, - val random: Random = Random() + val random: MtRand = MtRand() ) { - val items = arrayListOf() + val items = ArrayList() fun ensure(num: Int): List { - while (items.size < num) items += random[0, max] + while (items.size < num) items.add(random[0, max]) return items } } diff --git a/korge-simon/resources/index.html b/korge-simon/common/src/main/resources/index.html similarity index 100% rename from korge-simon/resources/index.html rename to korge-simon/common/src/main/resources/index.html diff --git a/korge-simon/resources/korge-ui.png b/korge-simon/common/src/main/resources/korge-ui.png similarity index 100% rename from korge-simon/resources/korge-ui.png rename to korge-simon/common/src/main/resources/korge-ui.png diff --git a/korge-simon/resources/kotlin.atlas b/korge-simon/common/src/main/resources/kotlin.atlas similarity index 100% rename from korge-simon/resources/kotlin.atlas rename to korge-simon/common/src/main/resources/kotlin.atlas diff --git a/korge-simon/resources/kotlin.psd b/korge-simon/common/src/main/resources/kotlin.psd similarity index 100% rename from korge-simon/resources/kotlin.psd rename to korge-simon/common/src/main/resources/kotlin.psd diff --git a/korge-simon/resources/kotlin/0.png b/korge-simon/common/src/main/resources/kotlin/0.png similarity index 100% rename from korge-simon/resources/kotlin/0.png rename to korge-simon/common/src/main/resources/kotlin/0.png diff --git a/korge-simon/resources/kotlin/1.png b/korge-simon/common/src/main/resources/kotlin/1.png similarity index 100% rename from korge-simon/resources/kotlin/1.png rename to korge-simon/common/src/main/resources/kotlin/1.png diff --git a/korge-simon/resources/kotlin/2.png b/korge-simon/common/src/main/resources/kotlin/2.png similarity index 100% rename from korge-simon/resources/kotlin/2.png rename to korge-simon/common/src/main/resources/kotlin/2.png diff --git a/korge-simon/resources/kotlin/3.png b/korge-simon/common/src/main/resources/kotlin/3.png similarity index 100% rename from korge-simon/resources/kotlin/3.png rename to korge-simon/common/src/main/resources/kotlin/3.png diff --git a/korge-simon/resources/kotlin/4.png b/korge-simon/common/src/main/resources/kotlin/4.png similarity index 100% rename from korge-simon/resources/kotlin/4.png rename to korge-simon/common/src/main/resources/kotlin/4.png diff --git a/korge-simon/resources/kotlin/5.png b/korge-simon/common/src/main/resources/kotlin/5.png similarity index 100% rename from korge-simon/resources/kotlin/5.png rename to korge-simon/common/src/main/resources/kotlin/5.png diff --git a/korge-simon/resources/kotlin_images.png b/korge-simon/common/src/main/resources/kotlin_images.png similarity index 100% rename from korge-simon/resources/kotlin_images.png rename to korge-simon/common/src/main/resources/kotlin_images.png diff --git a/korge-simon/resources/sounds/0.mp3 b/korge-simon/common/src/main/resources/sounds/0.mp3 similarity index 100% rename from korge-simon/resources/sounds/0.mp3 rename to korge-simon/common/src/main/resources/sounds/0.mp3 diff --git a/korge-simon/resources/sounds/1.mp3 b/korge-simon/common/src/main/resources/sounds/1.mp3 similarity index 100% rename from korge-simon/resources/sounds/1.mp3 rename to korge-simon/common/src/main/resources/sounds/1.mp3 diff --git a/korge-simon/resources/sounds/2.mp3 b/korge-simon/common/src/main/resources/sounds/2.mp3 similarity index 100% rename from korge-simon/resources/sounds/2.mp3 rename to korge-simon/common/src/main/resources/sounds/2.mp3 diff --git a/korge-simon/resources/sounds/3.mp3 b/korge-simon/common/src/main/resources/sounds/3.mp3 similarity index 100% rename from korge-simon/resources/sounds/3.mp3 rename to korge-simon/common/src/main/resources/sounds/3.mp3 diff --git a/korge-simon/resources/sounds/4.mp3 b/korge-simon/common/src/main/resources/sounds/4.mp3 similarity index 100% rename from korge-simon/resources/sounds/4.mp3 rename to korge-simon/common/src/main/resources/sounds/4.mp3 diff --git a/korge-simon/resources/sounds/5.mp3 b/korge-simon/common/src/main/resources/sounds/5.mp3 similarity index 100% rename from korge-simon/resources/sounds/5.mp3 rename to korge-simon/common/src/main/resources/sounds/5.mp3 diff --git a/korge-simon/resources/sounds/6.mp3 b/korge-simon/common/src/main/resources/sounds/6.mp3 similarity index 100% rename from korge-simon/resources/sounds/6.mp3 rename to korge-simon/common/src/main/resources/sounds/6.mp3 diff --git a/korge-simon/resources/sounds/7.mp3 b/korge-simon/common/src/main/resources/sounds/7.mp3 similarity index 100% rename from korge-simon/resources/sounds/7.mp3 rename to korge-simon/common/src/main/resources/sounds/7.mp3 diff --git a/korge-simon/resources/sounds/8.mp3 b/korge-simon/common/src/main/resources/sounds/8.mp3 similarity index 100% rename from korge-simon/resources/sounds/8.mp3 rename to korge-simon/common/src/main/resources/sounds/8.mp3 diff --git a/korge-simon/resources/sounds/fail.mp3 b/korge-simon/common/src/main/resources/sounds/fail.mp3 similarity index 100% rename from korge-simon/resources/sounds/fail.mp3 rename to korge-simon/common/src/main/resources/sounds/fail.mp3 diff --git a/korge-simon/resources/sounds/success.wav b/korge-simon/common/src/main/resources/sounds/success.wav similarity index 100% rename from korge-simon/resources/sounds/success.wav rename to korge-simon/common/src/main/resources/sounds/success.wav diff --git a/korge-simon/jvm/build.gradle b/korge-simon/jvm/build.gradle new file mode 100644 index 0000000..d3916e4 --- /dev/null +++ b/korge-simon/jvm/build.gradle @@ -0,0 +1,17 @@ +apply plugin: 'kotlin-platform-jvm' +apply plugin: 'application' + +mainClassName = 'com.soywiz.korge.samples.simon.Simon' + +sourceSets { + main.resources.srcDirs = [ '../common/src/main/resources', '../common/src/generated/resources' ] +} + +dependencies { + implement project(":korge-simon:common") + + compile "com.soywiz:korge:$korVersion" + compile "com.soywiz:korge-ext-swf:$korVersion" + compile "com.soywiz:korge-ext-ui:$korVersion" + compile "com.soywiz:korau-mp3:$korVersion" +} diff --git a/korge-simon/jvm/settings.gradle b/korge-simon/jvm/settings.gradle new file mode 100644 index 0000000..bdfd8af --- /dev/null +++ b/korge-simon/jvm/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'korge-simon' diff --git a/korge-tic-tac-toe/common/src/main/kotlin/com/soywiz/korge/tictactoe/BoardMediator.kt b/korge-tic-tac-toe/common/src/main/kotlin/com/soywiz/korge/tictactoe/BoardMediator.kt index 5f52d53..646dc0f 100644 --- a/korge-tic-tac-toe/common/src/main/kotlin/com/soywiz/korge/tictactoe/BoardMediator.kt +++ b/korge-tic-tac-toe/common/src/main/kotlin/com/soywiz/korge/tictactoe/BoardMediator.kt @@ -2,6 +2,7 @@ package com.soywiz.korge.tictactoe import com.soywiz.korge.animate.play import com.soywiz.korge.input.onClick +import com.soywiz.korge.input.onUp import com.soywiz.korge.time.milliseconds import com.soywiz.korge.tween.* import com.soywiz.korge.view.View @@ -86,7 +87,7 @@ suspend fun Board.reset() { fun Board.Cell.init(view: View) { this.view = view set(this.value) - view["hit"].onClick { + view["hit"].onUp { onPress(Unit) } } diff --git a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/Board.kt b/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/Board.kt deleted file mode 100644 index b749c38..0000000 --- a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/Board.kt +++ /dev/null @@ -1,64 +0,0 @@ -package com.soywiz.korge.tictactoe - -import com.soywiz.korio.util.Extra -import com.soywiz.korma.ds.Array2 -import com.soywiz.korma.geom.PointInt -import kotlin.collections.ArrayList - -enum class Chip { EMPTY, CROSS, CIRCLE } - -class Board(val width: Int = 3, val height: Int = width, val lineSize: Int = width) { - class Cell(val x: Int, val y: Int) : Extra by Extra.Mixin() { - val pos = PointInt(x, y) - var value = Chip.EMPTY - } - - val cells = Array2(width, height) { Cell(it % width, it / width) } - - fun inside(x: Int, y: Int) = cells.inside(x, y) - - fun select(x: Int, y: Int, dx: Int, dy: Int, size: Int): List? { - if (!inside(x, y)) return null - if (!inside(x + dx * (size - 1), y + dy * (size - 1))) return null - return (0 until size).map { cells[x + dx * it, y + dy * it] } - } - - val lines = kotlin.collections.ArrayList>() - - init { - fun addLine(line: List?) { - if (line != null) lines += line - } - for (y in 0..height) { - for (x in 0..width) { - addLine(select(x, y, 1, 0, lineSize)) - addLine(select(x, y, 0, 1, lineSize)) - addLine(select(x, y, 1, 1, lineSize)) - addLine(select(width - x - 1, y, -1, 1, lineSize)) - } - } - } - - operator fun get(x: Int, y: Int) = cells[x, y] - operator fun set(x: Int, y: Int, value: Chip) = run { cells[x, y].value = value } - - val Iterable.chipLine: Chip? - get() { - val expected = this.first().value - return if (expected == Chip.EMPTY) null else if (this.all { it.value == expected }) expected else null - } - - val moreMovements: Boolean get() = cells.any { it.value == Chip.EMPTY } - - val winnerLine: List? - get() { - val out = kotlin.collections.ArrayList() - for (line in lines) if (line.chipLine != null) out += line - return if (out.isEmpty()) null else out.toSet().toList() - } - - val winner: Chip? - get() { - return winnerLine?.firstOrNull()?.value - } -} diff --git a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/BoardMediator.kt b/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/BoardMediator.kt deleted file mode 100644 index 5f52d53..0000000 --- a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/BoardMediator.kt +++ /dev/null @@ -1,92 +0,0 @@ -package com.soywiz.korge.tictactoe - -import com.soywiz.korge.animate.play -import com.soywiz.korge.input.onClick -import com.soywiz.korge.time.milliseconds -import com.soywiz.korge.tween.* -import com.soywiz.korge.view.View -import com.soywiz.korge.view.get -import com.soywiz.korio.async.Signal -import com.soywiz.korio.async.async -import com.soywiz.korio.util.Extra - -var Board.Cell.view by Extra.Property { null } -val Board.Cell.vview: View get() = this.view!! -val Board.Cell.onPress by Extra.Property { Signal() } - -fun Board.Cell.set(type: Chip) { - this.value = type - view["chip"].play(when (type) { - Chip.EMPTY -> "empty" - Chip.CIRCLE -> "circle" - Chip.CROSS -> "cross" - }) -} - -suspend fun Board.Cell.setAnimate(type: Chip) { - set(type) - async { - view.tween( - (view["chip"]!!::alpha[0.7, 1.0]).linear(), - (view["chip"]!!::scale[0.8, 1.0]).easeOutElastic(), - time = 300.milliseconds - ) - } -} - -var Board.Cell.highlighting by Extra.Property { false } - -suspend fun Board.Cell.highlight(highlight: Boolean) { - view["highlight"].play(if (highlight) "highlight" else "none") - this.highlighting = highlight - if (highlight) { - async { - - val hl = view["highlight"]!! - while (highlighting) { - hl.tween((hl::alpha[0.7]).easeInOutQuad(), time = 300.milliseconds) - hl.tween((hl::alpha[1.0]).easeInOutQuad(), time = 200.milliseconds) - } - } - - async { - val ch = view["chip"]!! - ch.tween((ch::scale[0.4]).easeOutQuad(), time = 100.milliseconds) - ch.tween((ch::scale[1.2]).easeOutElastic(), time = 300.milliseconds) - while (highlighting) { - ch.tween((ch::scale[1.0]).easeOutQuad(), time = 300.milliseconds) - ch.tween((ch::scale[1.2]).easeOutElastic(), time = 300.milliseconds) - } - } - } -} - -suspend fun Board.Cell.lowlight(lowlight: Boolean) { - async { - view.tween( - view!!::scale[1.0, 0.7], - view!!::alpha[0.3], - time = 300.milliseconds, easing = Easings.EASE_OUT_QUAD - ) - } -} - -suspend fun Board.reset() { - for (cell in cells) { - //cell.view?.removeAllComponents() - cell.set(Chip.EMPTY) - cell.highlight(false) - cell.view?.scale = 1.0 - cell.view?.alpha = 1.0 - cell.view["chip"]?.scale = 1.0 - cell.view["chip"]?.alpha = 1.0 - } -} - -fun Board.Cell.init(view: View) { - this.view = view - set(this.value) - view["hit"].onClick { - onPress(Unit) - } -} diff --git a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/InjectExt.kt b/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/InjectExt.kt deleted file mode 100644 index e7ec889..0000000 --- a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/InjectExt.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.soywiz.korge.tictactoe - -import com.soywiz.korge.animate.AnLibrary -import com.soywiz.korge.resources.getPath -import com.soywiz.korio.inject.AsyncInjector - -// AUTOGENERATED: -fun AsyncInjector.generatedInject() = this - //.mapPrototype { TicTacToeMainScene(getPath("main.ani")) } // @TODO: kotlin.js bug - .mapPrototype { TicTacToeMainScene(getPath(AnLibrary::class, "main.ani")) } diff --git a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/TicTacToe.kt b/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/TicTacToe.kt deleted file mode 100644 index cad2b08..0000000 --- a/korge-tic-tac-toe/src/com.soywiz.korge.tictactoe/TicTacToe.kt +++ /dev/null @@ -1,146 +0,0 @@ -package com.soywiz.korge.tictactoe - -import com.soywiz.korge.Korge -import com.soywiz.korge.animate.AnLibrary -import com.soywiz.korge.animate.AnLibraryPlugin -import com.soywiz.korge.input.mouse -import com.soywiz.korge.plugin.KorgePlugin -import com.soywiz.korge.resources.Path -import com.soywiz.korge.scene.Module -import com.soywiz.korge.scene.Scene -import com.soywiz.korge.view.Container -import com.soywiz.korge.view.descendantsWithPropInt -import com.soywiz.korge.view.get -import com.soywiz.korge.view.setText -import com.soywiz.korio.async.Signal -import com.soywiz.korio.async.go -import com.soywiz.korio.async.waitOne -import com.soywiz.korio.error.invalidOp -import com.soywiz.korio.inject.AsyncInjector -import com.soywiz.korio.lang.JvmStatic -import com.soywiz.korma.geom.PointInt - -object TicTacToe { - @JvmStatic - fun main(args: Array) = Korge(TicTacToeModule, injector = AsyncInjector().generatedInject()) -} - -object TicTacToeModule : Module() { - override val mainScene = TicTacToeMainScene::class - override val title: String = "tic-tac-toe" - override val icon: String = "icon.png" - override val plugins: List = super.plugins + listOf( - AnLibraryPlugin - ) - - suspend override fun init(injector: AsyncInjector) { - //injector.get().mapExtensions("swf" to "ani") - //injector.get().mapExtensionsJustInJTransc("swf" to "ani") - } -} - -// Controller -class TicTacToeMainScene( - @Path("main.ani") val mainLibrary: AnLibrary -) : Scene() { - val board = Board(3, 3) - lateinit var game: Game - - suspend override fun sceneInit(sceneView: Container) { - sceneView += mainLibrary.createMainTimeLine() - - for ((rowView, row) in sceneView.descendantsWithPropInt("row")) { - for ((cellView, cell) in rowView.descendantsWithPropInt("cell")) { - board.cells[row, cell].init(cellView) - } - } - - val p1 = InteractivePlayer(board, Chip.CROSS) - val p2 = BotPlayer(board, Chip.CIRCLE) - //val p2 = InteractivePlayer(board, Chip.CIRCLE) - - game = Game(board, listOf(p1, p2)) - cancellables += go { - while (true) { - game.board.reset() - val result = game.game() - - println(result) - - val results = mainLibrary.createMovieClip("Results") - //(results["result"] as AnTextField).format?.face = Html.FontFace.Bitmap(font) - when (result) { - is Game.Result.DRAW -> results["result"].setText("DRAW") - is Game.Result.WIN -> { - results["result"].setText("WIN") - for (cell in result.cells) cell.highlight(true) - for (cell in game.board.cells.toList() - result.cells) cell.lowlight(true) - } - } - sceneView += results - results["hit"]?.mouse?.onClick?.waitOne() - //sceneView -= results - results.removeFromParent() - - } - } - } -} - -interface Player { - val chip: Chip - suspend fun move(): PointInt -} - -class Game(val board: Board, val players: List) { - interface Result { - object DRAW : Result - class WIN(val player: Player?, val cells: List) : Result - } - - suspend fun game(): Result { - var turn = 0 - while (board.moreMovements) { - val currentPlayer = players[turn % players.size] - while (true) { - val pos = currentPlayer.move() - println(pos) - if (board.cells[pos].value == Chip.EMPTY) { - board.cells[pos].setAnimate(currentPlayer.chip) - break - } - } - if (board.winner != null) return Result.WIN(currentPlayer, board.winnerLine ?: listOf()) - turn++ - } - return Result.DRAW - } -} - -class BotPlayer(val board: Board, override val chip: Chip) : Player { - suspend override fun move(): PointInt { - for (cell in board.cells) { - if (cell.value == Chip.EMPTY) { - return cell.pos - } - } - invalidOp("No more movements") - } -} - -class InteractivePlayer(val board: Board, override val chip: Chip) : Player { - val clicked = Signal() - - init { - for (cell in board.cells) { - cell.onPress { - clicked(cell.pos) - } - } - } - - suspend override fun move(): PointInt { - return clicked.waitOne() - } - -} diff --git a/settings.gradle b/settings.gradle index 095004b..e8fa7ac 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,6 +5,10 @@ include( 'korge-tic-tac-toe:js', 'korge-tic-tac-toe:jvm', + 'korge-simon:common', + 'korge-simon:js', + 'korge-simon:jvm', + //'korge-coffee', //'korge-simon', )