bump pixlyzer, wip: real block breaking

This commit is contained in:
Bixilon 2021-05-22 22:41:37 +02:00
parent 3b26669b5b
commit bf66039ca0
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 74 additions and 32 deletions

View File

@ -45,6 +45,8 @@ data class BlockState(
val collisionShape: VoxelShape,
val occlusionShape: VoxelShape,
val outlineShape: VoxelShape,
val hardness: Float,
val requiresTool: Boolean,
) {
override fun hashCode(): Int {
@ -116,7 +118,7 @@ data class BlockState(
companion object {
fun deserialize(owner: Block, registries: Registries, data: JsonObject, models: Map<ResourceLocation, BlockModel>): BlockState {
fun deserialize(block: Block, registries: Registries, data: JsonObject, models: Map<ResourceLocation, BlockModel>): BlockState {
val properties = data["properties"]?.asJsonObject?.let {
getProperties(it)
} ?: mutableMapOf()
@ -149,7 +151,7 @@ data class BlockState(
}
}
val tintColor: RGBColor? = data["tint_color"]?.asInt?.let { TintColorCalculator.getJsonColor(it) } ?: owner.tintColor
val tintColor: RGBColor? = data["tint_color"]?.asInt?.let { TintColorCalculator.getJsonColor(it) } ?: block.tintColor
val material = registries.materialRegistry[ResourceLocation(data["material"].asString)]!!
@ -173,13 +175,13 @@ data class BlockState(
val occlusionShape = data["occlusion_shapes"]?.asShape() ?: VoxelShape.EMPTY
val outlineShape = data["outline_shape"]?.asShape() ?: VoxelShape.EMPTY
owner.renderOverride?.let {
block.renderOverride?.let {
renderers.clear()
renderers.addAll(it)
}
return BlockState(
block = owner,
block = block,
properties = properties.toMap(),
renderers = renderers,
tintColor = tintColor,
@ -187,6 +189,8 @@ data class BlockState(
collisionShape = collisionShape,
occlusionShape = occlusionShape,
outlineShape = outlineShape,
hardness = data["hardness"].asFloat,
requiresTool = data["requires_tool"]?.asBoolean ?: material.soft,
)
}

View File

@ -38,7 +38,6 @@ open class Block(
mappings: Registries,
data: JsonObject,
) : RegistryItem {
open val hardness: Float = data["hardness"]?.asFloat ?: 0.0f
open val explosionResistance: Float = data["explosion_resistance"]?.asFloat ?: 0.0f
open val tintColor: RGBColor? = data["tint_color"]?.asInt?.let { TintColorCalculator.getJsonColor(it) }
open val randomOffsetType: RandomOffsetTypes? = data["offset_type"]?.asString?.let { RandomOffsetTypes[it] }

View File

@ -28,6 +28,7 @@ data class Material(
val blockMotion: Boolean,
val flammable: Boolean,
val liquid: Boolean,
val soft: Boolean,
val solidBlocking: Boolean,
val replaceable: Boolean,
val solid: Boolean,
@ -46,6 +47,7 @@ data class Material(
blockMotion = data["blocks_motion"]?.asBoolean ?: false,
flammable = data["flammable"]?.asBoolean ?: false,
liquid = data["liquid"]?.asBoolean ?: false,
soft = data["is_soft"]?.asBoolean ?: false,
solidBlocking = data["solid_blocking"]?.asBoolean ?: false,
replaceable = data["replaceable"]?.asBoolean ?: false,
solid = data["solid"]?.asBoolean ?: false,

View File

@ -64,9 +64,9 @@ class RenderWindow(
val inputHandler = RenderWindowInputHandler(this)
var windowId = 0L
private var deltaFrameTime = 0L
private var deltaFrameTime = 0.0
private var lastFrame = 0L
private var lastFrame = 0.0
private val latch = CountUpAndDownLatch(1)
private var renderingState = RenderingStates.RUNNING
@ -312,7 +312,7 @@ class RenderWindow(
this.lastTickTimer = currentTickTime
}
val currentFrame = System.currentTimeMillis()
val currentFrame = glfwGetTime()
deltaFrameTime = currentFrame - lastFrame
lastFrame = currentFrame

View File

@ -33,7 +33,7 @@ class LeftClickHandler(
private var breakPosition: Vec3i? = null
private var breakBlockState: BlockState? = null
private var breakProgress: Float = -1.0f
private var breakProgress = -1.0
private var breakSelectedSlot: Int = -1
private var breakItemInHand: ItemStack? = null
@ -45,7 +45,7 @@ class LeftClickHandler(
private fun clearDigging() {
breakPosition = null
breakBlockState = null
breakProgress = -1.0f
breakProgress = -1.0
breakSelectedSlot = -1
breakItemInHand = null
@ -58,7 +58,16 @@ class LeftClickHandler(
}
}
private fun checkBreaking(isKeyDown: Boolean, deltaTime: Long): Boolean {
private fun swingArm() {
val currentTime = System.currentTimeMillis()
if (currentTime - lastSwing <= ProtocolDefinition.TICK_TIME) {
return
}
lastSwing = currentTime
connection.sendPacket(ArmSwingC2SP(Hands.MAIN_HAND))
}
private fun checkBreaking(isKeyDown: Boolean, deltaTime: Double): Boolean {
val currentTime = System.currentTimeMillis()
if (!isKeyDown) {
@ -97,7 +106,7 @@ class LeftClickHandler(
breakPosition = raycastHit.blockPosition
breakBlockState = raycastHit.blockState
breakProgress = 0.0f
breakProgress = 0.0
breakSelectedSlot = connection.player.selectedHotbarSlot
breakItemInHand = connection.player.inventory.getHotbarSlot()
@ -109,29 +118,62 @@ class LeftClickHandler(
connection.world.setBlockState(raycastHit.blockPosition, null)
}
if (currentTime - breakSent <= ProtocolDefinition.TICK_TIME) {
return true
}
breakSent = currentTime
val canStartBreaking = currentTime - breakSent >= ProtocolDefinition.TICK_TIME
val canInstantBreak = connection.player.baseAbilities.canInstantBreak || connection.player.entity.gamemode == Gamemodes.CREATIVE
if (canInstantBreak) {
if (!canStartBreaking) {
return true
}
// creative
if (currentTime - creativeLastHoldBreakTime <= ProtocolDefinition.TICK_TIME * 5) {
return true
}
connection.sendPacket(ArmSwingC2SP(Hands.MAIN_HAND))
swingArm()
startDigging()
finishDigging()
creativeLastHoldBreakTime = currentTime
breakSent = currentTime
return true
}
startDigging()
connection.sendPacket(ArmSwingC2SP(Hands.MAIN_HAND))
if (breakPosition == null && !canStartBreaking) {
return true
}
breakProgress += 0.05f
breakSent = currentTime
startDigging()
swingArm()
var speedMultiplier = 1.0f
var damage = speedMultiplier / raycastHit.blockState.hardness
damage /= if (raycastHit.blockState.requiresTool) {
100
} else {
30
}
when {
damage > 1.0f -> {
breakProgress = 1.0
}
damage <= 0.0f -> {
breakProgress = 0.0
}
else -> {
val ticks = 1.0f / damage
val seconds = (ticks / ProtocolDefinition.TICKS_PER_SECOND)
val progress = ((1.0f / seconds) * deltaTime)
breakProgress += progress
}
}
if (breakProgress >= 1.0f) {
finishDigging()
@ -143,8 +185,7 @@ class LeftClickHandler(
renderWindow.inputHandler.registerCheckCallback(KeyBindingsNames.DESTROY_BLOCK)
}
fun draw(deltaTime: Long) {
val currentTime = System.currentTimeMillis()
fun draw(deltaTime: Double) {
val isKeyDown = renderWindow.inputHandler.isKeyBindingDown(KeyBindingsNames.DESTROY_BLOCK)
// ToDo: Entity attacking
val consumed = checkBreaking(isKeyDown, deltaTime)
@ -155,10 +196,6 @@ class LeftClickHandler(
if (consumed) {
return
}
if (currentTime - lastSwing <= ProtocolDefinition.TICK_TIME) {
return
}
connection.sendPacket(ArmSwingC2SP(Hands.MAIN_HAND))
lastSwing = currentTime
swingArm()
}
}

View File

@ -117,7 +117,7 @@ class RightClickHandler(
}
fun draw(deltaTime: Long) {
fun draw(deltaTime: Double) {
// ToDo: Entity interaction, shield/sword blocking, etc
checkInteraction(renderWindow.inputHandler.isKeyBindingDown(KeyBindingsNames.BLOCK_INTERACT))
}

View File

@ -211,7 +211,7 @@ class Camera(
sendPositionToServer()
}
fun draw(deltaTime: Long) {
fun draw(deltaTime: Double) {
if (!currentPositionSent || !currentRotationSent) {
recalculateViewProjectionMatrix()
sendPositionToServer()
@ -224,7 +224,7 @@ class Camera(
flyingSpeed
} else {
walkingSpeed
} * (deltaTime / 1000.0)
} * deltaTime
val movementFront = Vec3(cameraFront)
if (!Minosoft.getConfig().config.game.camera.noCipMovement) {
movementFront.y = 0.0f

View File

@ -276,7 +276,7 @@ class RenderWindowInputHandler(
return false
}
fun draw(delta: Long) {
fun draw(delta: Double) {
camera.draw(delta)
leftClickHandler.draw(delta)
rightClickHandler.draw(delta)

File diff suppressed because one or more lines are too long