libktx / ktx

Kotlin extensions for the libGDX game framework
https://libktx.github.io/
Creative Commons Zero v1.0 Universal
1.35k stars 71 forks source link

Artemis-ODB module #437

Closed deviodesign closed 1 year ago

deviodesign commented 1 year ago

352

deviodesign commented 1 year ago

@czyzby You can review the pull request if you want. I made sure to clean things up.

About the component delegation: You could add convenience functions for EntityEdit and Entity objects. However, you generally don't use Entity directly in artemis. Also, you don't want to extend Int as mentioned in the Fleks discussion. I prefer to hear your opinion on the implementation, so I won't add it to this pull request.

An example of how it looks:

EntityEdit.positon by world.propertyFor<Position>()

val entityId = world.entity {
   position.x = 10f
}

// Or...
val entityEdit = world.edit(entityId)

entityEdit.position.x = 10f
class ComponentDelegate<T : Component>(private val mapper: ComponentMapper<T>) {
    operator fun getValue(thisRef: EntityEdit, property: KProperty<*>): T  {
        val entityId = thisRef.entityId

        return mapper.get(entityId) ?: mapper.create(entityId)
    }

    operator fun setValue(thisRef: EntityEdit, property: KProperty<*>, value: T) {
        thisRef.add(value)
    }
}

inline fun <reified T : Component> World.propertyFor(): ComponentDelegate<T> =
    ComponentDelegate(mapperFor())
czyzby commented 1 year ago

@deviodesign

You can review the pull request if you want. I made sure to clean things up.

Busy week, but it's on my to-do list. :) I did an initial code review and seen your recent commits, there aren't any major issues. It's just that before merging an entire new module I'd like to make sure that everything's OK, and I usually need some time for an extra commit to update the contributors list, documentation, etc. I plan on merging this by the end of the week.

Extending EntityEdit is what I was thinking about. That, or using an inline class to wrap the entity ID and add extensions there. The only thing I'm worried about is that it might be difficult to get around a static world instance to support the delegates. We can postpone this feature for now and merge this as-is, unless you have some idea how to approach this without globals.

czyzby commented 1 year ago

Took me way longer than it should have, but I'll look into finalizing and releasing the module today through a new stable release. Thanks again, @deviodesign!