From 16acebab64ddb84d9914a0dd3b3223ec88e6f6cd Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Mon, 29 Apr 2024 23:08:08 -0400 Subject: [PATCH] feat(prefabs): Add using option to EventBind, backed by ReEmitEvent --- .../com/mineinabyss/geary/prefabs/Prefabs.kt | 1 + .../configuration/components/EntityObservers.kt | 15 +++++++++++++-- .../configuration/components/ReEmitEvent.kt | 7 +++++++ ...EntityObservers.kt => ParseEntityObservers.kt} | 4 +++- .../configuration/systems/ParseReEmitEvent.kt | 13 +++++++++++++ .../com/mineinabyss/geary/datatypes/Entity.kt | 3 +++ 6 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/ReEmitEvent.kt rename addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/{BindEntityObservers.kt => ParseEntityObservers.kt} (84%) create mode 100644 addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseReEmitEvent.kt diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/Prefabs.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/Prefabs.kt index 506a8001..11e01ca9 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/Prefabs.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/Prefabs.kt @@ -34,6 +34,7 @@ interface Prefabs { createTrackPrefabsByKeyListener() createCopyToInstancesSystem() bindEntityObservers() + reEmitEvent() } geary.pipeline.runOnOrAfter(GearyPhase.INIT_ENTITIES) { loader.loadOrUpdatePrefabs() diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt index 964ef854..6e67756c 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt @@ -1,8 +1,11 @@ package com.mineinabyss.geary.prefabs.configuration.components +import com.mineinabyss.geary.datatypes.ComponentId +import com.mineinabyss.geary.helpers.componentId import com.mineinabyss.geary.serialization.serializers.InnerSerializer import com.mineinabyss.geary.serialization.serializers.SerializedComponents import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient import kotlinx.serialization.builtins.ListSerializer @Serializable(with = EntityObservers.Serializer::class) @@ -18,6 +21,14 @@ class EntityObservers(val observers: List) { @Serializable class EventBind( val event: SerializableComponentId, + val using: SerializableComponentId? = null, val involving: List = listOf(), - val emit: SerializedComponents -) + private val emit: List +) { + class CachedEvent(val componentId: ComponentId, val data: Any?) + + @Transient + val emitEvents = emit.map { + if (using != null) ReEmitEvent(SerializableComponentId(using.id), it) else it + }.map { CachedEvent(componentId(it::class), it) } +} diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/ReEmitEvent.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/ReEmitEvent.kt new file mode 100644 index 00000000..5e0acc7a --- /dev/null +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/ReEmitEvent.kt @@ -0,0 +1,7 @@ +package com.mineinabyss.geary.prefabs.configuration.components + +data class ReEmitEvent( + val findByRelationKind: SerializableComponentId, + val data: Any?, +) + diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/BindEntityObservers.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseEntityObservers.kt similarity index 84% rename from addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/BindEntityObservers.kt rename to addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseEntityObservers.kt index 54764a29..cd9055f4 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/BindEntityObservers.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseEntityObservers.kt @@ -13,7 +13,9 @@ fun GearyModule.bindEntityObservers() = observe() .exec { (observers) -> observers.observers.forEach { observer -> entity.observe(observer.event.id).involving(EntityType(observer.involving.map { it.id })).exec { - observer.emit.forEach { event -> entity.emit(event) } + observer.emitEvents.forEach { event -> + entity.emit(event = event.componentId, data = event.data) + } } } entity.remove() diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseReEmitEvent.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseReEmitEvent.kt new file mode 100644 index 00000000..01eac958 --- /dev/null +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/ParseReEmitEvent.kt @@ -0,0 +1,13 @@ +package com.mineinabyss.geary.prefabs.configuration.systems + +import com.mineinabyss.geary.helpers.toGeary +import com.mineinabyss.geary.modules.GearyModule +import com.mineinabyss.geary.prefabs.configuration.components.ReEmitEvent +import com.mineinabyss.geary.systems.builders.observeWithData + +fun GearyModule.reEmitEvent() = observeWithData().exec { + entity.getRelationsByKind(event.findByRelationKind.id).forEach { relation -> + val entity = relation.target.toGeary() + if (entity.exists()) entity.emit(event = event.findByRelationKind.id, data = event.data) + } +} diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt index c0004ba0..d9e7ba41 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt @@ -210,6 +210,9 @@ value class Entity(val id: EntityId) { componentIdWithNullable() ) as List> + fun getRelationsByKind(kind: ComponentId): List = + getRelations(kind, geary.components.any) + /** Queries for relations using the same format as [AccessorOperations.getRelations]. */ inline fun getRelations(): List = getRelations(componentIdWithNullable(), componentIdWithNullable())