diff options
Diffstat (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt')
| -rw-r--r-- | composeApp/src/commonMain/kotlin/coffee/liz/ecs/Entity.kt | 70 |
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) |
