-
-
Save TheDome0/b175aabc450b7739871249d6c141a316 to your computer and use it in GitHub Desktop.
KorGE setup for super smooth player movement by keys or mouse-click
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var destination: Point = player.pos.copy() | |
| private val playerSpeed = 6.0 | |
| private val step = 20 | |
| init { | |
| addUpdater { | |
| handeKeyboardControls() | |
| } | |
| addFixedUpdater(25.milliseconds) { | |
| if (destination.distanceTo(player.pos) > playerSpeed) { | |
| moveToDestination() | |
| } | |
| } | |
| onClick { | |
| destination.copyFrom(it.currentPosStage) | |
| } | |
| } | |
| private fun handeKeyboardControls() { | |
| if(anyMoveKeyIsPressed()){ | |
| resetDestination() | |
| } | |
| if (Key.UP.isPressed() || Key.W.isPressed()) { | |
| moveUp() | |
| } | |
| if (Key.DOWN.isPressed() || Key.S.isPressed()) { | |
| moveDown() | |
| } | |
| if (Key.LEFT.isPressed() || Key.A.isPressed()) { | |
| moveLeft() | |
| } | |
| if (Key.RIGHT.isPressed() || Key.D.isPressed()) { | |
| moveRight() | |
| } | |
| } | |
| fun anyMoveKeyIsPressed() = (arrowKeys() + wasdKeys()).any { it.isPressed() } | |
| private fun arrowKeys() = listOf(Key.UP, Key.DOWN, Key.LEFT, Key.RIGHT) | |
| private fun wasdKeys() = listOf(Key.W, Key.A, Key.S, Key.S) | |
| private fun Key.isPressed() = stage?.input?.keys?.get(this) ?: false | |
| private fun moveLeft() { | |
| destination.x -= step | |
| } | |
| private fun moveRight() { | |
| destination.x += step | |
| } | |
| private fun moveDown() { | |
| destination.y += step | |
| } | |
| private fun moveUp() { | |
| destination.y -= step | |
| } | |
| private fun resetDestination() { | |
| destination.copyFrom(player.pos) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment