From 72f11d51e6b69998416577cb0abd564993fff4df Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 16 Oct 2021 17:42:06 +0200 Subject: [PATCH] hotbar polling, fix half hart display --- .../gui/hud/elements/hotbar/HotbarElement.kt | 14 +++++++++++--- .../gui/hud/elements/hotbar/HotbarHUDElement.kt | 5 ----- .../gui/hud/elements/hotbar/HotbarHealthElement.kt | 11 +++++++++-- .../gui/hud/elements/hotbar/HotbarHungerElement.kt | 4 ++-- .../hud/elements/hotbar/HotbarProtectionElement.kt | 4 ++++ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt index fd96aa9a1..27d213e92 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt @@ -56,8 +56,6 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { } override fun forceRender(offset: Vec2i, z: Int, consumer: GUIVertexConsumer): Int { - // ToDo: Do not apply every frame - forceSilentApply() var maxZ = 0 val topMaxSize = topLeft.size.max(topRight.size) @@ -78,7 +76,17 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { element.silentApply() } - size = base.size + Vec2i(0, max(topLeft.size.y, topRight.size.y)) + Vec2i(0, experience.size.y) + _size = base.size + Vec2i(0, max(topLeft.size.y, topRight.size.y)) + Vec2i(0, experience.size.y) + } + + override fun silentApply(): Boolean { + forceSilentApply() + return true + } + + override fun onChildChange(child: Element) { + silentApply() // ToDo: Check + parent?.onChildChange(this) } override fun tick() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHUDElement.kt index 489f54034..b0b54421a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHUDElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHUDElement.kt @@ -18,7 +18,6 @@ import de.bixilon.minosoft.gui.rendering.gui.hud.HUDRenderer import de.bixilon.minosoft.gui.rendering.gui.hud.elements.HUDBuilder import de.bixilon.minosoft.gui.rendering.gui.hud.elements.HUDElement import de.bixilon.minosoft.modding.event.events.ExperienceChangeEvent -import de.bixilon.minosoft.modding.event.events.UpdateHealthEvent import de.bixilon.minosoft.modding.event.invoker.CallbackEventInvoker import de.bixilon.minosoft.util.KUtil.toResourceLocation import glm_.vec2.Vec2i @@ -39,10 +38,6 @@ class HotbarHUDElement(hudRenderer: HUDRenderer) : HUDElement(hud layout = HotbarElement(hudRenderer) layout.prefMaxSize = Vec2i(-1, -1) - connection.registerEvent(CallbackEventInvoker.of { - layout.health.apply() - layout.hunger.apply() - }) connection.registerEvent(CallbackEventInvoker.of { layout.experience.apply() }) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHealthElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHealthElement.kt index 559194782..2f3c6e6d6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHealthElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHealthElement.kt @@ -161,7 +161,7 @@ class HotbarHealthElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { var healthLeft = totalHealth var heart = 0 - while (healthLeft > 0.5f) { + while (healthLeft >= 0.5f) { val row = heart / HEARTS_PER_ROW val column = heart % HEARTS_PER_ROW @@ -232,7 +232,10 @@ class HotbarHealthElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { val wither = witherStatusEffect?.let { player.activeStatusEffects[it] != null } ?: false val frozen = player.ticksFrozen > 0 - val health = player.healthCondition.hp + var health = player.healthCondition.hp + if (health > 0.0f && health < 0.5f) { + health = 0.5f + } val absorptionsAmount = player.playerAbsorptionHearts // ToDo: This is (probably) calculated as effect instance val maxHealth = player.getAttributeValue(DefaultStatusEffectAttributeNames.GENERIC_MAX_HEALTH).toFloat() @@ -254,6 +257,10 @@ class HotbarHealthElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { return true } + override fun tick() { + apply() + } + companion object { private const val HP_PER_ROW = 20 private const val HEARTS_PER_ROW = HP_PER_ROW / 2 diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHungerElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHungerElement.kt index fbacf55ad..1c9477547 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHungerElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarHungerElement.kt @@ -131,11 +131,11 @@ class HotbarHungerElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { } override fun tick() { - super.tick() - val healthCondition = hudRenderer.connection.player.healthCondition animate = healthCondition.saturation <= 0.0f && ticks++ % (healthCondition.hunger * 3 + 1) == 0 + + apply() } override fun silentApply(): Boolean { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt index 03bc2ead6..b3ee0fd71 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt @@ -79,6 +79,10 @@ class HotbarProtectionElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { cacheUpToDate = false } + override fun tick() { + apply() + } + companion object { private val ARMOR_SIZE = Vec2i(8, 9) private val SIZE = Vec2i(10 * ARMOR_SIZE.x, ARMOR_SIZE.y)