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, Component>() /** * Add a component to this entity. */ fun add(component: T): Entity { components[component::class] = component return this } /** * Remove a component from this entity. */ fun remove(type: KClass): Entity { components.remove(type) return this } /** * Get a component by type. */ @Suppress("UNCHECKED_CAST") fun get(type: KClass): T? { return components[type] as? T } /** * Check if entity has a component. */ fun has(type: KClass): Boolean { return components.containsKey(type) } /** * Check if entity has all specified components. */ fun hasAll(vararg types: KClass): Boolean { return types.all { components.containsKey(it) } } /** * Get all component types on this entity. */ fun componentTypes(): Set> { 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 Entity.get(): T? = get(T::class) inline fun Entity.has(): Boolean = has(T::class) inline fun Entity.remove(): Entity = remove(T::class)