Skip to content

Commit

Permalink
chore: Add helper function for creating prefabs directly with the pre…
Browse files Browse the repository at this point in the history
…fabs addon
  • Loading branch information
0ffz committed Nov 3, 2024
1 parent 0d5e7b3 commit d583615
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,14 +29,6 @@ class PrefabLoader(
) {
private val needsInherit = world.cache(::NeedsInherit)

fun markAsPrefab(entity: GearyEntity, key: PrefabKey) {
entity.set(Prefab())
entity.set<PrefabKey>(key)
entity.addRelation<NoInherit, Prefab>()
entity.addRelation<NoInherit, Uuid>()
entity.addRelation<NoInherit, CopyToInstances>()
}

fun loadOrUpdatePrefabs() {
val results = mutableListOf<String>()
sources.paths.forEach { prefabsPath ->
Expand Down Expand Up @@ -155,4 +146,14 @@ class PrefabLoader(
class NeedsInherit(world: Geary) : Query(world) {
val inheritPrefabs by get<InheritPrefabs>()
}

companion object {
fun markAsPrefab(entity: GearyEntity, key: PrefabKey) {
entity.set(Prefab())
entity.set<PrefabKey>(key)
entity.addRelation<NoInherit, Prefab>()
entity.addRelation<NoInherit, Uuid>()
entity.addRelation<NoInherit, CopyToInstances>()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -10,6 +11,11 @@ class PrefabsDSL(
internal val prefabsBuilder: PrefabSources,
internal val namespaced: Namespaced,
) {
fun create(vararg prefabs: Pair<String, GearyEntity>) {
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(
Expand Down

0 comments on commit d583615

Please sign in to comment.