summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-10-23 21:59:37 -0700
committerElizabeth Hunt <me@liz.coffee>2025-10-24 20:00:58 -0700
commit64f825465de9fa30c4dfe2707067efdb96110db8 (patch)
tree5241385e316e2f4ceede5018603103d71be75202 /composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt
downloadabstraction-engine-kt-64f825465de9fa30c4dfe2707067efdb96110db8.tar.gz
abstraction-engine-kt-64f825465de9fa30c4dfe2707067efdb96110db8.zip
Init
Diffstat (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt')
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt70
1 files changed, 70 insertions, 0 deletions
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt
new file mode 100644
index 0000000..2ceac47
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt
@@ -0,0 +1,70 @@
+package coffee.liz.ecs
+
+import kotlin.reflect.KClass
+
+/**
+ * An entity is a unique identifier with a collection of components.
+ */
+class Entity(val id: Int) {
+ private val components = mutableMapOf<KClass<out Component>, Component>()
+
+ /**
+ * Add a component to this entity.
+ */
+ fun <T : Component> add(component: T): Entity {
+ components[component::class] = component
+ return this
+ }
+
+ /**
+ * Remove a component from this entity.
+ */
+ fun <T : Component> remove(type: KClass<T>): Entity {
+ components.remove(type)
+ return this
+ }
+
+ /**
+ * Get a component by type.
+ */
+ @Suppress("UNCHECKED_CAST")
+ fun <T : Component> get(type: KClass<T>): T? {
+ return components[type] as? T
+ }
+
+ /**
+ * Check if entity has a component.
+ */
+ fun <T : Component> has(type: KClass<T>): Boolean {
+ return components.containsKey(type)
+ }
+
+ /**
+ * Check if entity has all specified components.
+ */
+ fun hasAll(vararg types: KClass<out Component>): Boolean {
+ return types.all { components.containsKey(it) }
+ }
+
+ /**
+ * Get all component types on this entity.
+ */
+ fun componentTypes(): Set<KClass<out Component>> {
+ return components.keys.toSet()
+ }
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (other !is Entity) return false
+ return id == other.id
+ }
+
+ override fun hashCode(): Int = id
+
+ override fun toString(): String = "Entity($id)"
+}
+
+// Convenience extensions for cleaner syntax
+inline fun <reified T : Component> Entity.get(): T? = get(T::class)
+inline fun <reified T : Component> Entity.has(): Boolean = has(T::class)
+inline fun <reified T : Component> Entity.remove(): Entity = remove(T::class)