-
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.
feat(observers): Entity scoped observers via a relation on entity
feat(prefabs): Defining entity observers via config
- Loading branch information
Showing
21 changed files
with
238 additions
and
59 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
23 changes: 23 additions & 0 deletions
23
...mmonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mineinabyss.geary.prefabs.configuration.components | ||
|
||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import com.mineinabyss.geary.serialization.serializers.SerializedComponents | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
|
||
@Serializable(with = EntityObservers.Serializer::class) | ||
class EntityObservers(val observers: List<EventBind>) { | ||
class Serializer : InnerSerializer<List<EventBind>, EntityObservers>( | ||
serialName = "geary:observe", | ||
inner = ListSerializer(EventBind.serializer()), | ||
inverseTransform = { it.observers }, | ||
transform = ::EntityObservers | ||
) | ||
} | ||
|
||
@Serializable | ||
class EventBind( | ||
val event: SerializableComponentId, | ||
val involving: List<SerializableComponentId> = listOf(), | ||
val emit: SerializedComponents | ||
) |
34 changes: 34 additions & 0 deletions
34
.../kotlin/com/mineinabyss/geary/prefabs/configuration/components/SerializableComponentId.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,34 @@ | ||
package com.mineinabyss.geary.prefabs.configuration.components | ||
|
||
import com.mineinabyss.geary.datatypes.ComponentId | ||
import com.mineinabyss.geary.helpers.componentId | ||
import com.mineinabyss.geary.serialization.serializableComponents | ||
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
@Serializable(with = SerializableComponentId.Serializer::class) | ||
class SerializableComponentId(val id: ComponentId) { | ||
object Serializer : KSerializer<SerializableComponentId> { | ||
override val descriptor = PrimitiveSerialDescriptor("EventComponent", PrimitiveKind.STRING) | ||
|
||
private val polymorphicListAsMapSerializer = PolymorphicListAsMapSerializer.ofComponents() | ||
|
||
override fun deserialize(decoder: Decoder): SerializableComponentId { | ||
val type = decoder.decodeString() | ||
val namespaces = polymorphicListAsMapSerializer | ||
.getParentConfig(decoder.serializersModule)?.namespaces | ||
?: emptyList() | ||
val typeComponentId = componentId(serializableComponents.serializers.getClassFor(type, namespaces)) | ||
return SerializableComponentId(typeComponentId) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: SerializableComponentId) { | ||
TODO() | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...monMain/kotlin/com/mineinabyss/geary/prefabs/configuration/systems/BindEntityObservers.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,21 @@ | ||
package com.mineinabyss.geary.prefabs.configuration.systems | ||
|
||
import com.mineinabyss.geary.datatypes.EntityType | ||
import com.mineinabyss.geary.modules.GearyModule | ||
import com.mineinabyss.geary.observers.entity.observe | ||
import com.mineinabyss.geary.observers.events.OnSet | ||
import com.mineinabyss.geary.prefabs.configuration.components.EntityObservers | ||
import com.mineinabyss.geary.systems.builders.observe | ||
import com.mineinabyss.geary.systems.query.query | ||
|
||
fun GearyModule.bindEntityObservers() = observe<OnSet>() | ||
.involving(query<EntityObservers>()) | ||
.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) } | ||
} | ||
} | ||
entity.remove<EntityObservers>() | ||
} | ||
|
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
13 changes: 13 additions & 0 deletions
13
...src/commonMain/kotlin/com/mineinabyss/geary/serialization/dsl/WithCommonComponentNames.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,13 @@ | ||
package com.mineinabyss.geary.serialization.dsl | ||
|
||
import com.mineinabyss.geary.observers.events.* | ||
|
||
fun SerializableComponentsDSL.withCommonComponentNames() { | ||
namedComponent<OnAdd>("geary:on_add") | ||
namedComponent<OnSet>("geary:on_set") | ||
namedComponent<OnFirstSet>("geary:on_first_set") | ||
namedComponent<OnRemove>("geary:on_remove") | ||
namedComponent<OnUpdate>("geary:on_update") | ||
namedComponent<OnEntityRemoved>("geary:on_entity_removed") | ||
namedComponent<OnExtend>("geary:on_extend") | ||
} |
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
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
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
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
Oops, something went wrong.