From d5836151d1a828d0c0de418741b10c7b80784564 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Sat, 2 Nov 2024 21:12:17 -0400 Subject: [PATCH] chore: Add helper function for creating prefabs directly with the prefabs addon --- .../mineinabyss/geary/prefabs/PrefabLoader.kt | 19 ++++++++++--------- .../mineinabyss/geary/prefabs/PrefabsDSL.kt | 6 ++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt index 2ff81403..99953644 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt @@ -5,7 +5,6 @@ import com.mineinabyss.geary.components.relations.NoInherit import com.mineinabyss.geary.datatypes.Entity import com.mineinabyss.geary.datatypes.GearyEntity import com.mineinabyss.geary.helpers.entity -import com.mineinabyss.geary.helpers.fastForEach import com.mineinabyss.geary.modules.Geary import com.mineinabyss.geary.prefabs.configuration.components.CopyToInstances import com.mineinabyss.geary.prefabs.configuration.components.InheritPrefabs @@ -30,14 +29,6 @@ class PrefabLoader( ) { private val needsInherit = world.cache(::NeedsInherit) - fun markAsPrefab(entity: GearyEntity, key: PrefabKey) { - entity.set(Prefab()) - entity.set(key) - entity.addRelation() - entity.addRelation() - entity.addRelation() - } - fun loadOrUpdatePrefabs() { val results = mutableListOf() sources.paths.forEach { prefabsPath -> @@ -155,4 +146,14 @@ class PrefabLoader( class NeedsInherit(world: Geary) : Query(world) { val inheritPrefabs by get() } + + companion object { + fun markAsPrefab(entity: GearyEntity, key: PrefabKey) { + entity.set(Prefab()) + entity.set(key) + entity.addRelation() + entity.addRelation() + entity.addRelation() + } + } } diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSL.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSL.kt index 43e40a89..f84c7fc3 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSL.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabsDSL.kt @@ -2,6 +2,7 @@ package com.mineinabyss.geary.prefabs import com.mineinabyss.geary.addons.Namespaced import com.mineinabyss.geary.addons.dsl.GearyDSL +import com.mineinabyss.geary.datatypes.GearyEntity import kotlinx.io.files.Path import kotlinx.io.files.SystemFileSystem @@ -10,6 +11,11 @@ class PrefabsDSL( internal val prefabsBuilder: PrefabSources, internal val namespaced: Namespaced, ) { + fun create(vararg prefabs: Pair) { + prefabs.forEach { (name, entity) -> + PrefabLoader.markAsPrefab(entity, PrefabKey.of(namespaced.namespace, name)) + } + } /** Loads prefab entities from all files inside a [directory][from], into a given [namespace] */ fun fromFiles(