-
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: Revamped action system as a new addon
- Loading branch information
Showing
19 changed files
with
249 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
id(idofrontLibs.plugins.mia.kotlin.multiplatform.get().pluginId) | ||
id(idofrontLibs.plugins.mia.publication.get().pluginId) | ||
alias(idofrontLibs.plugins.kotlinx.serialization) | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
implementation(project(":geary-core")) | ||
implementation(project(":geary-serialization")) | ||
|
||
implementation(libs.uuid) | ||
implementation(idofrontLibs.idofront.di) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/Action.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,5 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
interface Action { | ||
fun ActionGroupContext.execute() | ||
} |
15 changes: 15 additions & 0 deletions
15
addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroup.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,15 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
class ActionGroup( | ||
val actions: List<Action>, | ||
) { | ||
fun execute(context: ActionGroupContext) { | ||
actions.forEach { | ||
try { | ||
with(it) { context.execute() } | ||
} catch (e: ActionsCancelledException) { | ||
return | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionGroupContext.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,10 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
import com.mineinabyss.geary.actions.expressions.Expression | ||
import com.mineinabyss.geary.datatypes.GearyEntity | ||
|
||
class ActionGroupContext( | ||
var entity: GearyEntity, | ||
) { | ||
fun <T> eval(expression: Expression<T>): T = expression.evaluate(this) | ||
} |
3 changes: 3 additions & 0 deletions
3
...-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/ActionsCancelledException.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,3 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
class ActionsCancelledException : Exception() |
5 changes: 5 additions & 0 deletions
5
addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/Condition.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,5 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
interface Condition { | ||
fun ActionGroupContext.execute(): Boolean | ||
} |
18 changes: 18 additions & 0 deletions
18
addons/geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/GearyActions.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,18 @@ | ||
package com.mineinabyss.geary.actions | ||
|
||
import com.mineinabyss.geary.actions.event_binds.bindEntityObservers | ||
import com.mineinabyss.geary.addons.GearyPhase | ||
import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault | ||
import com.mineinabyss.geary.modules.geary | ||
|
||
class GearyActions { | ||
companion object : GearyAddonWithDefault<GearyActions> { | ||
override fun default() = GearyActions() | ||
|
||
override fun GearyActions.install() { | ||
geary.run { | ||
bindEntityObservers() | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/BecomeAction.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,25 @@ | ||
package com.mineinabyss.geary.actions.actions | ||
|
||
import com.mineinabyss.geary.actions.Action | ||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.actions.expressions.EntityExpression | ||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable(with = BecomeAction.Serializer::class) | ||
@SerialName("geary:become") | ||
class BecomeAction( | ||
val become: EntityExpression, | ||
) : Action { | ||
override fun ActionGroupContext.execute() { | ||
entity = become.evaluate(this) | ||
} | ||
|
||
object Serializer : InnerSerializer<EntityExpression, BecomeAction>( | ||
serialName = "geary:become", | ||
inner = EntityExpression.serializer(), | ||
inverseTransform = { it.become }, | ||
transform = ::BecomeAction | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
...ry-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EmitEventAction.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.actions.actions | ||
|
||
import com.mineinabyss.geary.actions.Action | ||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.datatypes.ComponentId | ||
import com.mineinabyss.geary.helpers.componentId | ||
|
||
class EmitEventAction( | ||
val eventId: ComponentId, | ||
val data: Any?, | ||
): Action { | ||
override fun ActionGroupContext.execute() { | ||
entity.emit(event = eventId, data = data) | ||
} | ||
|
||
companion object { | ||
fun from(data: Any) = EmitEventAction(componentId(data::class), data) | ||
|
||
fun wrapIfNotAction(data: Any) = if(data is Action) data else from(data) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...geary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/actions/EnsureAction.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,38 @@ | ||
package com.mineinabyss.geary.actions.actions | ||
|
||
import com.mineinabyss.geary.actions.Action | ||
import com.mineinabyss.geary.actions.ActionsCancelledException | ||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.actions.event_binds.EventBind | ||
import com.mineinabyss.geary.helpers.componentId | ||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer | ||
import com.mineinabyss.geary.serialization.serializers.SerializedComponents | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.Transient | ||
|
||
@Serializable(with = EnsureAction.Serializer::class) | ||
class EnsureAction( | ||
val conditions: SerializedComponents, | ||
) : Action { | ||
@Transient | ||
private val flat = conditions.map { EventBind.CachedEvent(componentId(it::class), it) } | ||
|
||
override fun ActionGroupContext.execute() { | ||
flat.forEach { | ||
when (val condition = it.data) { | ||
is Conwdition -> with(condition) { | ||
if(!execute()) throw ActionsCancelledException() | ||
} | ||
else -> entity.emit(it.componentId, it.data) //TODO use geary condition system if we get one | ||
} | ||
} | ||
} | ||
|
||
object Serializer: InnerSerializer<SerializedComponents, EnsureAction>( | ||
serialName = "geary:ensure", | ||
inner = PolymorphicListAsMapSerializer.ofComponents(), | ||
inverseTransform = { it.conditions }, | ||
transform = { EnsureAction(it) } | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.mineinabyss.geary.actions.event_binds | ||
|
||
import com.mineinabyss.geary.serialization.serializers.InnerSerializer | ||
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer | ||
import com.mineinabyss.geary.serialization.serializers.SerializableComponentId | ||
import com.mineinabyss.geary.serialization.serializers.SerializedComponents | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.builtins.MapSerializer | ||
|
||
@Serializable(with = EntityObservers.Serializer::class) | ||
class EntityObservers(val observers: List<EventBind>) { | ||
class Serializer : InnerSerializer<Map<SerializableComponentId, List<SerializedComponents>>, EntityObservers>( | ||
serialName = "geary:observe", | ||
inner = MapSerializer( | ||
SerializableComponentId.serializer(), | ||
ListSerializer(PolymorphicListAsMapSerializer.ofComponents()) | ||
), | ||
inverseTransform = { it.observers.associate { it.event to it.emit } }, | ||
transform = { EntityObservers(it.map { (event, emit) -> EventBind(event, emit = emit) }) } | ||
) | ||
} | ||
|
||
|
10 changes: 10 additions & 0 deletions
10
...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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mineinabyss.geary.actions.event_binds | ||
|
||
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<SerializedComponents>, | ||
) |
27 changes: 27 additions & 0 deletions
27
...s/src/commonMain/kotlin/com/mineinabyss/geary/actions/event_binds/ParseEntityObservers.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,27 @@ | ||
package com.mineinabyss.geary.actions.event_binds | ||
|
||
import com.mineinabyss.geary.actions.ActionGroup | ||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.actions.actions.EmitEventAction | ||
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.systems.builders.observe | ||
import com.mineinabyss.geary.systems.query.query | ||
|
||
fun GearyModule.bindEntityObservers() = observe<OnSet>() | ||
.involving(query<EntityObservers>()) | ||
.exec { (observers) -> | ||
observers.observers.forEach { observer -> | ||
val actionGroup = ActionGroup( | ||
actions = observer.emit.flatten().map { EmitEventAction.wrapIfNotAction(it) } | ||
) | ||
entity.observe(observer.event.id).involving(EntityType(observer.involving.map { it.id })).exec { | ||
val context = ActionGroupContext(entity) | ||
actionGroup.execute(context) | ||
} | ||
} | ||
entity.remove<EntityObservers>() | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
...tions/src/commonMain/kotlin/com/mineinabyss/geary/actions/expressions/EntityExpression.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,18 @@ | ||
package com.mineinabyss.geary.actions.expressions | ||
|
||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import com.mineinabyss.geary.datatypes.GearyEntity | ||
import com.mineinabyss.geary.helpers.parent | ||
import kotlinx.serialization.Serializable | ||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
@Serializable | ||
value class EntityExpression( | ||
val expression: String, | ||
) /*: Expression<GearyEntity>()*/ { | ||
fun evaluate(context: ActionGroupContext): GearyEntity { | ||
return if (expression == "parent") context.entity.parent!! | ||
else TODO() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ary-actions/src/commonMain/kotlin/com/mineinabyss/geary/actions/expressions/Expression.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,9 @@ | ||
package com.mineinabyss.geary.actions.expressions | ||
|
||
import com.mineinabyss.geary.actions.ActionGroupContext | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
abstract class Expression<T> { | ||
abstract fun evaluate(context: ActionGroupContext): T | ||
} |
119 changes: 0 additions & 119 deletions
119
...mmonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...c/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/ReEmitEvent.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
Oops, something went wrong.