-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify Tick and Predicate suspension into one
- Loading branch information
Showing
9 changed files
with
40 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
engine/src/main/kotlin/world/gregs/voidps/engine/suspend/PredicateSuspension.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 27 additions & 4 deletions
31
engine/src/main/kotlin/world/gregs/voidps/engine/suspend/Suspension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
package world.gregs.voidps.engine.suspend | ||
|
||
import kotlinx.coroutines.CancellableContinuation | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import world.gregs.voidps.engine.GameLoop | ||
import world.gregs.voidps.engine.entity.character.Character | ||
import kotlin.coroutines.resume | ||
|
||
abstract class Suspension { | ||
lateinit var continuation: CancellableContinuation<Unit> | ||
data class Suspension( | ||
private val predicate: () -> Boolean | ||
) { | ||
private lateinit var continuation: CancellableContinuation<Unit> | ||
|
||
abstract fun ready(): Boolean | ||
fun ready(): Boolean { | ||
return predicate.invoke() | ||
} | ||
|
||
open fun resume() { | ||
fun resume() { | ||
continuation.resume(Unit) | ||
} | ||
|
||
companion object { | ||
suspend fun start(character: Character, predicate: () -> Boolean) { | ||
val suspension = Suspension(predicate) | ||
suspendCancellableCoroutine { | ||
suspension.continuation = it | ||
character.suspension = suspension | ||
} | ||
character.suspension = null | ||
} | ||
|
||
suspend fun start(character: Character, ticks: Int) { | ||
val tick = GameLoop.tick + ticks | ||
start(character) { GameLoop.tick >= tick } | ||
} | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
engine/src/main/kotlin/world/gregs/voidps/engine/suspend/TickSuspension.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters