← VirtualController
Authors:
Tags:
dependencies:
- https://github.com/korlibs/korge-virtualcontroller/tree/v1.0.2/korge-virtualcontroller##df0e840b8171bb3f5b8f7a86b77fbe39e725be16
Supports handling keyboard, real gamepad and virtual gamepad all in a simple interface.
val virtualController = virtualController(
sticks = listOf(
VirtualStickConfig(
left = Key.LEFT,
right = Key.RIGHT,
up = Key.UP,
down = Key.DOWN,
lx = GameButton.LX,
ly = GameButton.LY,
anchor = Anchor.BOTTOM_LEFT,
)
),
buttons = listOf(
VirtualButtonConfig(
key = Key.SPACE,
button = GameButton.BUTTON_SOUTH,
anchor = Anchor.BOTTOM_RIGHT,
),
VirtualButtonConfig(
key = Key.RETURN,
button = GameButton.BUTTON_NORTH,
anchor = Anchor.BOTTOM_RIGHT,
offset = Point(0f, -100f)
)
),
)
virtualController.apply {
down(GameButton.BUTTON_SOUTH) {
val isInGround = playerSpeed.y.isAlmostZero()
//if (isInGround) {
if (true) {
if (!jumping) {
jumping = true
updateState()
}
playerSpeed += Vector2(0, -5.5)
}
}
changed(GameButton.LX) {
if (it.new.absoluteValue < 0.01f) {
updated(right = it.new > 0f, up = true, scale = 1f)
}
}
addUpdater(60.hz) {
val lx = virtualController.lx
when {
lx < 0f -> {
updated(right = false, up = false, scale = lx.absoluteValue)
}
lx > 0f -> {
updated(right = true, up = false, scale = lx.absoluteValue)
}
}
}
}