-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
44 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
42 changes: 5 additions & 37 deletions
42
...ctions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EntityObservers.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
5 changes: 2 additions & 3 deletions
5
...eary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/EventBind.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,11 +1,10 @@ | ||
package com.mineinabyss.geary.actions.event_binds | ||
|
||
import com.mineinabyss.geary.actions.ActionEntry | ||
import com.mineinabyss.geary.actions.ActionGroup | ||
import com.mineinabyss.geary.serialization.serializers.SerializableComponentId | ||
import com.mineinabyss.geary.serialization.serializers.SerializedComponents | ||
|
||
class EventBind( | ||
val event: SerializableComponentId, | ||
val involving: List<SerializableComponentId> = listOf(), | ||
val emit: List<ActionEntry>, | ||
val actionGroup: ActionGroup, | ||
) |
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
59 changes: 59 additions & 0 deletions
59
.../geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/Passive.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.mineinabyss.geary.actions.event_binds; | ||
|
||
import com.mineinabyss.geary.actions.ActionGroup | ||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.actions.serializers.DurationSerializer | ||
import com.mineinabyss.geary.helpers.entity | ||
import com.mineinabyss.geary.helpers.fastForEach | ||
import com.mineinabyss.geary.modules.GearyModule | ||
import com.mineinabyss.geary.observers.events.OnSet | ||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import com.mineinabyss.geary.serialization.serializers.SerializableComponentId | ||
import com.mineinabyss.geary.systems.builders.observe | ||
import com.mineinabyss.geary.systems.builders.system | ||
import com.mineinabyss.geary.systems.query.query | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@Serializable | ||
class SystemBind( | ||
val match: List<SerializableComponentId>, | ||
val every: @Serializable(with = DurationSerializer::class) Duration = 1.seconds, | ||
val run: ActionGroup, | ||
) | ||
|
||
@Serializable(with = Passive.Serializer::class) | ||
class Passive( | ||
val systems: List<SystemBind>, | ||
) { | ||
object Serializer : InnerSerializer<List<SystemBind>, Passive>( | ||
serialName = "geary:passive", | ||
inner = ListSerializer(SystemBind.serializer()), | ||
inverseTransform = Passive::systems, | ||
transform = ::Passive | ||
) | ||
} | ||
|
||
fun GearyModule.parsePassive() = observe<OnSet>() | ||
.involving(query<Passive>()) | ||
.exec { (passive) -> | ||
passive.systems.forEach { systemBind -> | ||
val systemMatchingId = entity().id | ||
entity.add(systemMatchingId) | ||
system(query { | ||
has(systemMatchingId) | ||
has(systemBind.match.map { it.id }) | ||
}).every(systemBind.every).execOnAll { | ||
entities().fastForEach { entity -> | ||
runCatching { | ||
val context = ActionGroupContext(entity) | ||
systemBind.run.execute(context) | ||
}.onFailure { it.printStackTrace() } | ||
} | ||
} | ||
} | ||
entity.remove<Passive>() | ||
} | ||
|
41 changes: 41 additions & 0 deletions
41
...ons/src/commonMain/kotlin/com/mineinabyss/geary/actions/serializers/DurationSerializer.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.mineinabyss.geary.actions.serializers | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.days | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal object DurationSerializer : KSerializer<Duration> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Time", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: Duration) = | ||
encoder.encodeString(value.toString()) | ||
|
||
override fun deserialize(decoder: Decoder): Duration { | ||
val string = decoder.decodeString() | ||
return Duration.parseOrNull(string) ?: fromString(decoder.decodeString()) ?: error("Not a valid duration: $string") | ||
} | ||
|
||
private fun fromString(string: String): Duration? { | ||
val splitAt = string.indexOfFirst { it.isLetter() }.takeIf { it > 0 } ?: string.length | ||
val value = string.take(splitAt).toDouble() | ||
return when (string.drop(splitAt)) { | ||
"ms" -> value.milliseconds | ||
"s" -> value.seconds | ||
"m" -> value.minutes | ||
"h" -> value.hours | ||
"d" -> value.days | ||
"w" -> value.days * 7 | ||
"mo" -> value.days * 31 | ||
else -> null | ||
} | ||
} | ||
} |