Skip to content

Commit

Permalink
Simplify character Suspension
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Jan 17, 2025
1 parent 6dfbe0e commit 7c98a64
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ class Interact(
* Continue any suspended, clear any finished or start a new interaction
*/
private fun launch(event: Interaction<*>): Boolean {
if (character.suspension != null) {
character.resumeSuspension()
if (character.resumeSuspension()) {
return true
}
if (!event.launched && character.emit(event)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class Interaction<C : Character> : CancellableEvent(), SuspendableEvent
* interaction will have finished and there will be nothing to resume the suspension
*/
override suspend fun pause(ticks: Int) {
TickSuspension(ticks)
TickSuspension.start(character, ticks)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class AreaEntered<C : Character>(
override val size = 5

override suspend fun pause(ticks: Int) {
TickSuspension(ticks)
TickSuspension.start(character, ticks)
}

override fun parameter(dispatcher: EventDispatcher, index: Int): Any? = when (index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data class AreaExited<C : Character>(
override val size = 5

override suspend fun pause(ticks: Int) {
TickSuspension(ticks)
TickSuspension.start(character, ticks)
}

override fun parameter(dispatcher: EventDispatcher, index: Int): Any? = when (index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class Moved<C : Character>(
override val size = 4

override suspend fun pause(ticks: Int) {
TickSuspension(ticks)
TickSuspension.start(character, ticks)
}

override fun parameter(dispatcher: EventDispatcher, index: Int): Any? = when (index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ data class NPC(
override var softTimers: Timers = TimerSlot(this)
override var delay: Continuation<Unit>? = null
override var suspension: Suspension? = null
set(value) {
field?.cancel()
field = value
}
override var variables: Variables = Variables(this)
override val steps: Steps = Steps(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ class Player(
get() = client != null && viewport != null

override var suspension: Suspension? = null
set(value) {
field?.cancel()
field = value
}

override var delay: Continuation<Unit>? = null

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package world.gregs.voidps.engine.suspend

import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import world.gregs.voidps.engine.entity.character.Character

class PredicateSuspension(
private val predicate: () -> Boolean,
override val onCancel: (() -> Unit)?,
private val continuation: CancellableContinuation<Unit>
) : Suspension() {

var boolean: Boolean? = null

override fun ready(): Boolean {
return predicate.invoke()
}

override fun resume() {
super.resume()
continuation.resume(Unit)
}

companion object {
context(SuspendableContext<*>) suspend operator fun invoke(predicate: () -> Boolean) {
if (predicate.invoke()) {
return
}
suspend fun start(character: Character, predicate: () -> Boolean) {
val suspension = PredicateSuspension(predicate)
suspendCancellableCoroutine {
character.suspension = PredicateSuspension(predicate, onCancel, it)
suspension.continuation = it
character.suspension = suspension
}
character.suspension = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ fun Character.resumeSuspension(): Boolean {
}

suspend fun SuspendableContext<Player>.awaitDialogues(): Boolean {
PredicateSuspension { player.dialogue == null }
PredicateSuspension.start(character) { player.dialogue == null }
return true
}

suspend fun SuspendableContext<Player>.awaitInterfaces(): Boolean {
PredicateSuspension { player.menu == null }
PredicateSuspension.start(character) { player.menu == null }
return true
}

suspend fun SuspendableContext<*>.pauseForever() {
InfiniteSuspension()
}

private val logger = InlineLogger()

context(SuspendableContext<*>) suspend fun Character.playAnimation(id: String, override: Boolean = false, canInterrupt: Boolean = true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package world.gregs.voidps.engine.suspend

import kotlinx.coroutines.CancellableContinuation
import kotlin.coroutines.resume

abstract class Suspension {
protected abstract val onCancel: (() -> Unit)?
lateinit var continuation: CancellableContinuation<Unit>

abstract fun ready(): Boolean

open fun resume() {
}

open fun cancel() {
onCancel?.invoke()
continuation.resume(Unit)
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package world.gregs.voidps.engine.suspend

import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import world.gregs.voidps.engine.GameLoop
import kotlin.coroutines.resume
import world.gregs.voidps.engine.entity.character.Character

data class TickSuspension(
val tick: Int,
override val onCancel: (() -> Unit)?,
private val continuation: CancellableContinuation<Unit>
private val tick: Int,
) : Suspension() {

override fun ready(): Boolean {
return GameLoop.tick >= tick
}

override fun resume() {
super.resume()
continuation.resume(Unit)
}

companion object {
context(SuspendableContext<*>) suspend operator fun invoke(ticks: Int) {
suspend fun start(character: Character, ticks: Int) {
if (ticks <= 0) {
return
}
val suspension = TickSuspension(GameLoop.tick + ticks)
suspendCancellableCoroutine {
character.suspension = TickSuspension(GameLoop.tick + ticks, onCancel, it)
suspension.continuation = it
character.suspension = suspension
}
character.suspension = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ internal class InteractTest : KoinMock() {
if (operate) {
Events.handle<Player, NPCOption<Player>>("player_operate_npc", "*", "*") {
if (suspend) {
TickSuspension(2)
TickSuspension.start(character, 2)
}
operated = true
}
}
if (approach) {
Events.handle<Player, NPCOption<Player>>("player_approach_npc", "*", "*") {
if (suspend) {
TickSuspension(2)
TickSuspension.start(character, 2)
}
approached = true
}
Expand Down

0 comments on commit 7c98a64

Please sign in to comment.