mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 10:55:01 -04:00
improve block breaking
This commit is contained in:
parent
b8ff40ee0a
commit
f0686f7e0f
@ -26,6 +26,7 @@ import de.bixilon.minosoft.protocol.packets.c2s.play.ArmSwingC2SP
|
|||||||
import de.bixilon.minosoft.protocol.packets.c2s.play.BlockBreakC2SP
|
import de.bixilon.minosoft.protocol.packets.c2s.play.BlockBreakC2SP
|
||||||
import de.bixilon.minosoft.protocol.packets.c2s.play.BlockPlaceC2SP
|
import de.bixilon.minosoft.protocol.packets.c2s.play.BlockPlaceC2SP
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
|
import glm_.vec3.Vec3i
|
||||||
|
|
||||||
class InteractionHandler(
|
class InteractionHandler(
|
||||||
val renderWindow: RenderWindow,
|
val renderWindow: RenderWindow,
|
||||||
@ -34,22 +35,54 @@ class InteractionHandler(
|
|||||||
private var lastInteraction = 0L
|
private var lastInteraction = 0L
|
||||||
private var lastInteractionSent = 0L
|
private var lastInteractionSent = 0L
|
||||||
|
|
||||||
|
private var lastBreak = 0L
|
||||||
|
private var lastBreakSent = 0L
|
||||||
|
private var currentlyBreakingBlock: Vec3i? = null
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
renderWindow.inputHandler.registerCheckCallback(KeyBindingsNames.BLOCK_INTERACT)
|
renderWindow.inputHandler.registerCheckCallback(KeyBindingsNames.BLOCK_INTERACT)
|
||||||
renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.DESTROY_BLOCK) {
|
renderWindow.inputHandler.registerCheckCallback(KeyBindingsNames.DESTROY_BLOCK)
|
||||||
if (!it) {
|
}
|
||||||
return@registerKeyCallback
|
|
||||||
}
|
private fun checkBreaking(isKeyDown: Boolean) {
|
||||||
val raycastHit = renderWindow.inputHandler.camera.getTargetBlock()
|
val currentTime = System.currentTimeMillis()
|
||||||
raycastHit?.let {
|
if (!isKeyDown) {
|
||||||
if (raycastHit.distance > RenderConstants.MAX_BLOCK_OUTLINE_RAYCAST_DISTANCE) {
|
lastBreak = 0L
|
||||||
return@let
|
return
|
||||||
}
|
}
|
||||||
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.START_DIGGING, raycastHit.blockPosition, Directions.UP))
|
if (currentTime - lastBreak < ProtocolDefinition.TICK_TIME * 5) {
|
||||||
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.FINISHED_DIGGING, raycastHit.blockPosition, Directions.UP))
|
return
|
||||||
connection.world.setBlock(raycastHit.blockPosition, null)
|
}
|
||||||
|
val raycastHit = renderWindow.inputHandler.camera.getTargetBlock()
|
||||||
|
|
||||||
|
fun cancel() {
|
||||||
|
currentlyBreakingBlock?.let {
|
||||||
|
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.CANCELLED_DIGGING, currentlyBreakingBlock, Directions.UP)) // ToDo
|
||||||
|
currentlyBreakingBlock = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (raycastHit?.blockPosition != currentlyBreakingBlock) {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
if ((raycastHit?.distance ?: Float.MAX_VALUE) > RenderConstants.MAX_BLOCK_OUTLINE_RAYCAST_DISTANCE) {
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
raycastHit ?: return
|
||||||
|
|
||||||
|
if (currentTime - lastBreakSent < ProtocolDefinition.TICK_TIME) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
currentlyBreakingBlock = raycastHit.blockPosition
|
||||||
|
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.START_DIGGING, raycastHit.blockPosition, raycastHit.hitDirection))
|
||||||
|
connection.sendPacket(BlockBreakC2SP(BlockBreakC2SP.BreakType.FINISHED_DIGGING, raycastHit.blockPosition, raycastHit.hitDirection))
|
||||||
|
connection.world.setBlock(raycastHit.blockPosition, null)
|
||||||
|
currentlyBreakingBlock = null
|
||||||
|
|
||||||
|
lastBreak = currentTime
|
||||||
|
lastBreakSent = currentTime
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkInteraction(isKeyDown: Boolean) {
|
private fun checkInteraction(isKeyDown: Boolean) {
|
||||||
@ -139,5 +172,6 @@ class InteractionHandler(
|
|||||||
|
|
||||||
fun draw() {
|
fun draw() {
|
||||||
checkInteraction(renderWindow.inputHandler.isKeyBindingDown(KeyBindingsNames.BLOCK_INTERACT))
|
checkInteraction(renderWindow.inputHandler.isKeyBindingDown(KeyBindingsNames.BLOCK_INTERACT))
|
||||||
|
checkBreaking(renderWindow.inputHandler.isKeyBindingDown(KeyBindingsNames.DESTROY_BLOCK))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user