From ae4da3d0b39a3a57b2f40f0555af054e62609f52 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 18 Jan 2023 15:39:16 +0100 Subject: [PATCH] wawla: break block indicator --- .../elements/wawla/block/BlockWawlaElement.kt | 4 +- .../wawla/block/WawlaBreakProgressElement.kt | 58 +++++++++++++++++++ .../input/interaction/BlockBreakStatus.kt | 38 ++++++++++++ .../interaction/BreakInteractionHandler.kt | 14 ++++- .../interaction/InteractInteractionHandler.kt | 5 +- 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/WawlaBreakProgressElement.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BlockBreakStatus.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/BlockWawlaElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/BlockWawlaElement.kt index 2f8a8876f..e2975c607 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/BlockWawlaElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/BlockWawlaElement.kt @@ -25,15 +25,17 @@ import de.bixilon.minosoft.gui.rendering.gui.elements.text.TextElement import de.bixilon.minosoft.gui.rendering.gui.hud.elements.wawla.WawlaElement import de.bixilon.minosoft.gui.rendering.gui.hud.elements.wawla.WawlaHUDElement -class BlockWawlaElement(wawla: WawlaHUDElement, private val target: BlockTarget) : WawlaElement(wawla) { +class BlockWawlaElement(wawla: WawlaHUDElement, val target: BlockTarget) : WawlaElement(wawla) { override val elements: List = listOf( createName(), createAdditionalInformation(), createIdentifierElement(target.blockState.block), createMod(), + WawlaBreakProgressElement(this), ) init { + parent = wawla forceSilentApply() } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/WawlaBreakProgressElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/WawlaBreakProgressElement.kt new file mode 100644 index 000000000..d01fc5c1e --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/wawla/block/WawlaBreakProgressElement.kt @@ -0,0 +1,58 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.gui.hud.elements.wawla.block + +import de.bixilon.kotlinglm.vec2.Vec2i +import de.bixilon.minosoft.data.text.formatting.color.ChatColors +import de.bixilon.minosoft.gui.rendering.gui.elements.Element +import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.ColorElement +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions +import de.bixilon.minosoft.gui.rendering.input.interaction.BlockBreakStatus +import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY + +class WawlaBreakProgressElement(block: BlockWawlaElement) : Element(block.guiRenderer) { + private val `break` = context.inputHandler.interactionManager.`break` + private val status = `break`.status + private val progress = if (status != null) `break`.breakProgress else null + + init { + parent = block + forceSilentApply() + } + + override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) { + if (progress == null) { + return + } + val maxWidth = parent?.size?.x ?: 0 + if (status == BlockBreakStatus.USELESS) { + ColorElement(guiRenderer, Vec2i(maxWidth, size.y), color = ChatColors.RED).forceRender(offset, consumer, options) + return + } + val width = (progress * (maxWidth - 1)).toInt() + 1 // bar is always 1 pixel wide + + val color = when (status) { + BlockBreakStatus.INEFFECTIVE -> ChatColors.RED + BlockBreakStatus.SLOW -> ChatColors.YELLOW + else -> ChatColors.GREEN + } + + ColorElement(guiRenderer, Vec2i(width, size.y), color).render(offset, consumer, options) + } + + override fun forceSilentApply() { + this.size = if (progress == null) Vec2i.EMPTY else Vec2i(-1, 3) + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BlockBreakStatus.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BlockBreakStatus.kt new file mode 100644 index 000000000..04857c227 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BlockBreakStatus.kt @@ -0,0 +1,38 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.input.interaction + +enum class BlockBreakStatus { + /** + * Block can not be broken + */ + USELESS, + + /** + * Block can only be broken with tool + */ + INEFFECTIVE, + + /** + * Breaking can be faster with the correct tool + */ + SLOW, + + /** + * Current tool is effective + */ + EFFECTIVE, + ; + +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BreakInteractionHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BreakInteractionHandler.kt index 3482d7bb1..984234a53 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BreakInteractionHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/BreakInteractionHandler.kt @@ -48,8 +48,8 @@ class BreakInteractionHandler( private var breakBlockState: BlockState? = null var breakProgress = Double.NEGATIVE_INFINITY private set - val breakingBlock: Boolean - get() = breakPosition != null + var status: BlockBreakStatus? = null + private set private var breakSelectedSlot: Int = -1 @@ -63,6 +63,7 @@ class BreakInteractionHandler( breakPosition = null breakBlockState = null breakProgress = Double.NEGATIVE_INFINITY + status = null breakSelectedSlot = -1 } @@ -238,6 +239,15 @@ class BreakInteractionHandler( } } + // TODO: properly set slow status if block drops without tool + this.status = when { + damage <= 0.0f -> BlockBreakStatus.USELESS + speedMultiplier <= 1.0f -> BlockBreakStatus.SLOW + isBestTool -> BlockBreakStatus.EFFECTIVE + target.blockState.requiresTool -> BlockBreakStatus.INEFFECTIVE + else -> BlockBreakStatus.SLOW + } + if (breakProgress >= 1.0f) { finishDigging() } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/InteractInteractionHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/InteractInteractionHandler.kt index e8f96c723..0318aebb3 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/InteractInteractionHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/interaction/InteractInteractionHandler.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -14,7 +14,6 @@ package de.bixilon.minosoft.gui.rendering.input.interaction import de.bixilon.kotlinglm.vec3.Vec3 -import de.bixilon.kutil.time.TimeUtil import de.bixilon.kutil.time.TimeUtil.millis import de.bixilon.minosoft.config.key.KeyActions import de.bixilon.minosoft.config.key.KeyBinding @@ -167,7 +166,7 @@ class InteractInteractionHandler( } fun useItem() { - if (interactionManager.`break`.breakingBlock) { + if (interactionManager.`break`.status != null) { return }