Some cleanups

This commit is contained in:
soywiz
2020-02-16 17:48:42 +01:00
parent 8b2654cb77
commit 94bdfd6046
3 changed files with 43 additions and 38 deletions

View File

@@ -11,7 +11,7 @@ import com.soywiz.korma.random.*
import kotlin.random.* import kotlin.random.*
// Proceso que se encarga del tablero // Proceso que se encarga del tablero
class Board( open class Board(
parent: Container, parent: Container,
val imageset: BmpSlice, val imageset: BmpSlice,
val imagenes: List<BmpSlice>, val imagenes: List<BmpSlice>,
@@ -66,7 +66,7 @@ class Board(
} }
// Destructor, aquí se quita el texto cuando se borra el tablero // Destructor, aquí se quita el texto cuando se borra el tablero
override fun onDestroy() { override protected fun onDestroy() {
timeText.removeFromParent() timeText.removeFromParent()
} }

View File

@@ -22,19 +22,13 @@ abstract class Process(parent: Container) : Container() {
override val stage: Stage get() = super.stage!! override val stage: Stage get() = super.stage!!
val views: Views get() = stage.views val views: Views get() = stage.views
val type: KClass<out View> get() = this::class
var fps: Double = 60.0 var fps: Double = 60.0
val key get() = stage.views.key val key get() = stage.views.key
val Mouse get() = views.mouseV val Mouse get() = views.mouseV
val Screen get() = views.screenV val Screen get() = views.screenV
val audio get() = views.audioV val audio get() = views.audioV
var angle: Double
get() = rotationDegrees
set(value) = run { rotationDegrees = value }
suspend fun frame() { suspend fun frame() {
//delayFrame() //delayFrame()
delay((1.0 / fps).seconds) delay((1.0 / fps).seconds)
@@ -46,30 +40,19 @@ abstract class Process(parent: Container) : Container() {
abstract suspend fun main() abstract suspend fun main()
private lateinit var job: Job private val job: Job = views.launchAsap {
var action = ::main
fun destroy() { while (true) {
removeFromParent() try {
} action()
break
open fun onDestroy() { } catch (e: ChangeActionException) {
} action = e.action
class ChangeActionException(val action: KSuspendFunction0<Unit>) : Exception()
init {
job = views.launchAsap {
var action = ::main
while (true) {
try {
action()
break
} catch (e: ChangeActionException) {
action = e.action
}
} }
} }
}
init {
addComponent(object : StageComponent { addComponent(object : StageComponent {
override val view: View = this@Process override val view: View = this@Process
@@ -85,12 +68,34 @@ abstract class Process(parent: Container) : Container() {
}) })
} }
fun collision(type: KClass<out View>): Boolean { fun destroy() {
return false removeFromParent()
} }
protected open fun onDestroy() {
}
class ChangeActionException(val action: KSuspendFunction0<Unit>) : Exception()
inline fun <reified T : View> collision(): T? = views.stage.findCollision<T>(this)
fun collision(matcher: (View) -> Boolean): View? = views.stage.findCollision(this, matcher)
} }
inline fun <reified T : View> 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 * Component with [added] and [removed] methods that are executed
* once the view is going to be displayed, and when the view has been removed * once the view is going to be displayed, and when the view has been removed

View File

@@ -37,17 +37,17 @@ class RandomLight(
alpha = 0.1 alpha = 0.1
while (true) { while (true) {
angle += inca rotationDegrees += inca
x = w2 - cos(angle) * w2 * excx + sx x = w2 - cos(rotationDegrees) * w2 * excx + sx
y = h2 - sin(angle) * h2 * excy + sy y = h2 - sin(rotationDegrees) * h2 * excy + sy
scale = 1 + (cos(angle) / 6) * incs scale = 1 + (cos(rotationDegrees) / 6) * incs
// Comprueba si una esfera de luz ha chocado con otra // Comprueba si una esfera de luz ha chocado con otra
// El sistema de colisión por defecto es inner circle // El sistema de colisión por defecto es inner circle
if (this.collision(this.type)) { if (this.collision<RandomLight>() != null) {
if (alpha <= 0.8) alpha += 0.01 alpha = (alpha + 0.01).coerceIn(0.1, 0.8)
} else { } else {
if (alpha >= 0.1) alpha -= 0.01 alpha = (alpha - 0.05).coerceIn(0.1, 0.8)
} }
frame() frame()