我的游戏自定义输入适配器不是 运行 渲染系统

My game custom input adapter is not running rendering system

我正在使用 Ashley 和 Guicer 通过 LibGDX 和 Kotlin 制作 ECS 游戏。一切正常,除了当我单击时,实体生成时没有纹理。当我在主文件中调用 addEntity 时,纹理已加载,但在 MyInputAdapter 中纹理只是不加载。

这里是 MyInputAdapter class:

class MyInputAdapter @Inject constructor(private val camera: OrthographicCamera,
                                     private val engine: Engine,
                                     private val world: World) :
    InputAdapter() {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
    val worldCords = camera.unproject(Vector3(screenX.toFloat(), screenY.toFloat(), 0f))

    engine.addEntity(Entity().apply {
        add(TextureRegionComponent(TextureRegion(Main.img)))
        add(TransformComponent(Vector2(worldCords.x, worldCords.y), 50f, 0.2f))

        val body = world.createBody(BodyDef().apply {
            type = BodyDef.BodyType.DynamicBody
        })
        body.createFixture(PolygonShape().apply {
            setAsBox(1f, 1f)
        }, 1.0f)
        body.setTransform(transform.position, 0f)
        add(PhysicsComponent(body))
    })

    return true
  }
}

这里是 Main 中的适配器注册 class:

 override fun create() {
    batch = SpriteBatch()
    img = Texture("badlogic.jpg")
    injector = Guice.createInjector(GameModule(this))
    injector.getInstance(Systems::class.java).list.map { injector.getInstance(it) }.forEach { system ->
        engine.addSystem(system)
    }

    createEntities()

    Gdx.input.inputProcessor = injector.getInstance(MyInputAdapter::class.java)
}

TextureRegionComponent 在输入适配器中被忽略,但它不在 Main class:

private fun createEntities() {
    val world = injector.getInstance(World::class.java)
    engine.addEntity(Entity().apply {
        add(TextureRegionComponent(TextureRegion(img)))
        add(TransformComponent(Vector2(5F, 5F)))

        val body = world.createBody(BodyDef().apply {
            type = BodyDef.BodyType.DynamicBody
        })
        body.createFixture(PolygonShape().apply {
            setAsBox(img.width.pixelToMeter / 2f, img.height.pixelToMeter / 2f)
        }, 1.0f)
        body.setTransform(transform.position, 0f)
        add(PhysicsComponent(body))
    })
    ...
}

这是一些图片: example

解决方案

所以我现在想通了。问题是我没有在 GameModule class

中正确设置活页夹
class GameModule(private val main: Main) : Module {
  override fun configure(binder: Binder) {
    binder.requireAtInjectOnConstructors() //<-- this was missing
    binder.requireExactBindingAnnotations() //<-- and this too
    binder.bind(SpriteBatch::class.java).toInstance(main.batch)
  }
  ...
}