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 [Component]. */ fun add(component: T): Entity { components[component::class] = component return this } /** * Remove [Component]. */ fun remove(type: KClass): Entity { components.remove(type) return this } /** * Get [Component] */ @Suppress("UNCHECKED_CAST") fun get(type: KClass): T = components[type] as T /** * Has [Component] */ fun has(type: KClass): Boolean = components.containsKey(type) /** * [Component]s of this entity. */ fun componentTypes(): Set> = 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)" }