Skip to content

Commit

Permalink
feat(prefabs): Add using option to EventBind, backed by ReEmitEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Apr 30, 2024
1 parent 42cdd76 commit 16aceba
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface Prefabs {
createTrackPrefabsByKeyListener()
createCopyToInstancesSystem()
bindEntityObservers()
reEmitEvent()
}
geary.pipeline.runOnOrAfter(GearyPhase.INIT_ENTITIES) {
loader.loadOrUpdatePrefabs()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -18,6 +21,14 @@ class EntityObservers(val observers: List<EventBind>) {
@Serializable
class EventBind(
val event: SerializableComponentId,
val using: SerializableComponentId? = null,
val involving: List<SerializableComponentId> = listOf(),
val emit: SerializedComponents
)
private val emit: List<SerializedComponents>
) {
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) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mineinabyss.geary.prefabs.configuration.components

data class ReEmitEvent(
val findByRelationKind: SerializableComponentId,
val data: Any?,
)

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ fun GearyModule.bindEntityObservers() = observe<OnSet>()
.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<EntityObservers>()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReEmitEvent>().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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ value class Entity(val id: EntityId) {
componentIdWithNullable<T>()
) as List<RelationWithData<K, T>>

fun getRelationsByKind(kind: ComponentId): List<Relation> =
getRelations(kind, geary.components.any)

/** Queries for relations using the same format as [AccessorOperations.getRelations]. */
inline fun <reified K : Component?, reified T : Component?> getRelations(): List<Relation> =
getRelations(componentIdWithNullable<K>(), componentIdWithNullable<T>())
Expand Down

0 comments on commit 16aceba

Please sign in to comment.