Skip to content

Commit

Permalink
Simplify Tick and Predicate suspension into one
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Jan 17, 2025
1 parent 7c98a64 commit 57a8746
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import world.gregs.voidps.engine.entity.character.Character
import world.gregs.voidps.engine.event.CancellableEvent
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.engine.suspend.SuspendableContext
import world.gregs.voidps.engine.suspend.TickSuspension
import world.gregs.voidps.engine.suspend.Suspension

abstract class Interaction<C : Character> : CancellableEvent(), SuspendableEvent, SuspendableContext<C> {
var approach = false
Expand All @@ -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.start(character, ticks)
Suspension.start(character, ticks)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.engine.suspend.SuspendableContext
import world.gregs.voidps.engine.suspend.TickSuspension
import world.gregs.voidps.engine.suspend.Suspension
import world.gregs.voidps.type.Area

data class AreaEntered<C : Character>(
Expand All @@ -22,7 +22,7 @@ data class AreaEntered<C : Character>(
override val size = 5

override suspend fun pause(ticks: Int) {
TickSuspension.start(character, ticks)
Suspension.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 @@ -7,7 +7,7 @@ import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.engine.suspend.SuspendableContext
import world.gregs.voidps.engine.suspend.TickSuspension
import world.gregs.voidps.engine.suspend.Suspension
import world.gregs.voidps.type.Area

data class AreaExited<C : Character>(
Expand All @@ -21,7 +21,7 @@ data class AreaExited<C : Character>(
override val size = 5

override suspend fun pause(ticks: Int) {
TickSuspension.start(character, ticks)
Suspension.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 @@ -8,7 +8,7 @@ import world.gregs.voidps.engine.event.EventDispatcher
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.event.SuspendableEvent
import world.gregs.voidps.engine.suspend.SuspendableContext
import world.gregs.voidps.engine.suspend.TickSuspension
import world.gregs.voidps.engine.suspend.Suspension
import world.gregs.voidps.type.Tile

/**
Expand All @@ -24,7 +24,7 @@ data class Moved<C : Character>(
override val size = 4

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

override fun parameter(dispatcher: EventDispatcher, index: Int): Any? = when (index) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ fun Character.resumeSuspension(): Boolean {
}

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

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

Expand Down
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 }
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import world.gregs.voidps.engine.entity.character.player.equip.BodyParts
import world.gregs.voidps.engine.event.Events
import world.gregs.voidps.engine.map.collision.Collisions
import world.gregs.voidps.engine.script.KoinMock
import world.gregs.voidps.engine.suspend.TickSuspension
import world.gregs.voidps.engine.suspend.Suspension
import world.gregs.voidps.network.login.protocol.visual.NPCVisuals
import world.gregs.voidps.network.login.protocol.visual.PlayerVisuals
import world.gregs.voidps.type.Tile
Expand Down Expand Up @@ -96,15 +96,15 @@ internal class InteractTest : KoinMock() {
if (operate) {
Events.handle<Player, NPCOption<Player>>("player_operate_npc", "*", "*") {
if (suspend) {
TickSuspension.start(character, 2)
Suspension.start(character, 2)
}
operated = true
}
}
if (approach) {
Events.handle<Player, NPCOption<Player>>("player_approach_npc", "*", "*") {
if (suspend) {
TickSuspension.start(character, 2)
Suspension.start(character, 2)
}
approached = true
}
Expand Down

0 comments on commit 57a8746

Please sign in to comment.