From 94bdfd6046b45718fbf8dac6d4d76ddef1a8362f Mon Sep 17 00:00:00 2001 From: soywiz Date: Sun, 16 Feb 2020 17:48:42 +0100 Subject: [PATCH] Some cleanups --- .../soywiz/korge/samples/minesweeper/Board.kt | 4 +- .../korge/samples/minesweeper/Process.kt | 63 ++++++++++--------- .../korge/samples/minesweeper/RandomLight.kt | 14 ++--- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Board.kt b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Board.kt index 9e971b1..c326110 100644 --- a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Board.kt +++ b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Board.kt @@ -11,7 +11,7 @@ import com.soywiz.korma.random.* import kotlin.random.* // Proceso que se encarga del tablero -class Board( +open class Board( parent: Container, val imageset: BmpSlice, val imagenes: List, @@ -66,7 +66,7 @@ class Board( } // Destructor, aquí se quita el texto cuando se borra el tablero - override fun onDestroy() { + override protected fun onDestroy() { timeText.removeFromParent() } diff --git a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Process.kt b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Process.kt index 288828e..183e69b 100644 --- a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Process.kt +++ b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/Process.kt @@ -22,19 +22,13 @@ abstract class Process(parent: Container) : Container() { override val stage: Stage get() = super.stage!! val views: Views get() = stage.views - val type: KClass get() = this::class var fps: Double = 60.0 val key get() = stage.views.key - val Mouse get() = views.mouseV val Screen get() = views.screenV val audio get() = views.audioV - var angle: Double - get() = rotationDegrees - set(value) = run { rotationDegrees = value } - suspend fun frame() { //delayFrame() delay((1.0 / fps).seconds) @@ -46,30 +40,19 @@ abstract class Process(parent: Container) : Container() { abstract suspend fun main() - private lateinit var job: Job - - fun destroy() { - removeFromParent() - } - - open fun onDestroy() { - } - - class ChangeActionException(val action: KSuspendFunction0) : Exception() - - init { - job = views.launchAsap { - var action = ::main - while (true) { - try { - action() - break - } catch (e: ChangeActionException) { - action = e.action - } + private val job: Job = views.launchAsap { + var action = ::main + while (true) { + try { + action() + break + } catch (e: ChangeActionException) { + action = e.action } } + } + init { addComponent(object : StageComponent { override val view: View = this@Process @@ -85,12 +68,34 @@ abstract class Process(parent: Container) : Container() { }) } - fun collision(type: KClass): Boolean { - return false + fun destroy() { + removeFromParent() } + + protected open fun onDestroy() { + } + + class ChangeActionException(val action: KSuspendFunction0) : Exception() + + inline fun collision(): T? = views.stage.findCollision(this) + fun collision(matcher: (View) -> Boolean): View? = views.stage.findCollision(this, matcher) } +inline fun Container.findCollision(subject: View): T? = findCollision(subject) { it is T && it != subject } as T? + +fun Container.findCollision(subject: View, matcher: (View) -> Boolean): View? { + var collides: View? = null + this.foreachDescendant { + if (matcher(it)) { + if (subject.collidesWith(it)) { + collides = it + } + } + } + return collides +} + /** * Component with [added] and [removed] methods that are executed * once the view is going to be displayed, and when the view has been removed diff --git a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/RandomLight.kt b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/RandomLight.kt index 79c597d..d14def8 100644 --- a/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/RandomLight.kt +++ b/sample-minesweeper/src/commonMain/kotlin/com/soywiz/korge/samples/minesweeper/RandomLight.kt @@ -37,17 +37,17 @@ class RandomLight( alpha = 0.1 while (true) { - angle += inca - x = w2 - cos(angle) * w2 * excx + sx - y = h2 - sin(angle) * h2 * excy + sy - scale = 1 + (cos(angle) / 6) * incs + rotationDegrees += inca + x = w2 - cos(rotationDegrees) * w2 * excx + sx + y = h2 - sin(rotationDegrees) * h2 * excy + sy + scale = 1 + (cos(rotationDegrees) / 6) * incs // Comprueba si una esfera de luz ha chocado con otra // El sistema de colisión por defecto es inner circle - if (this.collision(this.type)) { - if (alpha <= 0.8) alpha += 0.01 + if (this.collision() != null) { + alpha = (alpha + 0.01).coerceIn(0.1, 0.8) } else { - if (alpha >= 0.1) alpha -= 0.01 + alpha = (alpha - 0.05).coerceIn(0.1, 0.8) } frame()