mirror of
https://github.com/jlengrand/korge-samples.git
synced 2026-03-10 08:31:18 +00:00
Some cleanups
This commit is contained in:
@@ -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<BmpSlice>,
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<out View> 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<Unit>) : 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<out View>): Boolean {
|
||||
return false
|
||||
fun destroy() {
|
||||
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
|
||||
* once the view is going to be displayed, and when the view has been removed
|
||||
|
||||
@@ -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<RandomLight>() != 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()
|
||||
|
||||
Reference in New Issue
Block a user