From 29bacd8ce3b7925839d5f9de9f649fa1990a8128 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 20 Oct 2021 13:07:59 +0200 Subject: [PATCH] hud: hotbar air --- .../minosoft/config/StaticConfiguration.kt | 2 +- .../hud/elements/hotbar/HotbarAirElement.kt | 94 +++++++++++++++++++ .../gui/hud/elements/hotbar/HotbarElement.kt | 2 + .../protocol/packets/s2c/play/JoinGameS2CP.kt | 3 +- .../assets/minosoft/mapping/atlas.json | 14 +++ 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarAirElement.kt diff --git a/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.kt b/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.kt index f83896062..a9e3f2e6f 100644 --- a/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.kt +++ b/src/main/java/de/bixilon/minosoft/config/StaticConfiguration.kt @@ -22,5 +22,5 @@ object StaticConfiguration { @Deprecated(message = "Just for hud development purposes") - const val HUD_ONLY = false + const val HUD_ONLY = true } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarAirElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarAirElement.kt new file mode 100644 index 000000000..631009227 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarAirElement.kt @@ -0,0 +1,94 @@ +/* + * Minosoft + * Copyright (C) 2021 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.hotbar + +import de.bixilon.minosoft.data.registries.fluid.DefaultFluids +import de.bixilon.minosoft.gui.rendering.gui.elements.Element +import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.ImageElement +import de.bixilon.minosoft.gui.rendering.gui.hud.HUDRenderer +import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer +import de.bixilon.minosoft.gui.rendering.util.vec.Vec2Util.EMPTY +import glm_.vec2.Vec2i + +class HotbarAirElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { + private val water = hudRenderer.connection.registries.fluidRegistry[DefaultFluids.WATER]!! + private val airBubble = hudRenderer.atlasManager["minecraft:air_bubble"]!! + private val poppingAirBubble = hudRenderer.atlasManager["minecraft:popping_air_bubble"]!! + + init { + forceSilentApply() + } + + private var bubbles = 0 + private var lastBubblePopping = false + + override fun forceRender(offset: Vec2i, z: Int, consumer: GUIVertexConsumer): Int { + if (bubbles <= 0) { + return 0 + } + + for (i in bubbles - 1 downTo 0) { + var atlasElement = airBubble + if (i == 0 && lastBubblePopping) { + atlasElement = poppingAirBubble + } + + val image = ImageElement(hudRenderer, atlasElement) + + image.render(offset + Vec2i(i * BUBBLE_SIZE.x, 0), z, consumer) + } + + return 1 + } + + override fun silentApply(): Boolean { + val player = hudRenderer.connection.player + + val submergedFluid = player.submergedFluid + + var bubbles = 0 + + if (submergedFluid == water) { + bubbles = 10 // ToDo: Get air and check time, etc + } + + + if (this.bubbles == bubbles) { + return false + } + + this.bubbles = bubbles + + forceSilentApply() + + return true + } + + override fun forceSilentApply() { + size = if (bubbles <= 0.0f) { // ToDo: This notifies the parent, should we really notify it in silentApply? + Vec2i.EMPTY + } else { + Vec2i(BUBBLE_SIZE.x * bubbles, BUBBLE_SIZE.y) + } + cacheUpToDate = false + } + + override fun tick() { + apply() + } + + companion object { + private val BUBBLE_SIZE = Vec2i(8, 9) + } +} 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 f55a72d14..9bdbfbac6 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 @@ -31,6 +31,7 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { val health = HotbarHealthElement(hudRenderer) val hunger = HotbarHungerElement(hudRenderer) val protection = HotbarProtectionElement(hudRenderer) + val air = HotbarAirElement(hudRenderer) private val topLeft = RowLayout(hudRenderer, HorizontalAlignments.LEFT, 1) // contains health, protection, etc private val topRight = RowLayout(hudRenderer, HorizontalAlignments.RIGHT, 1) // contains hunger, air @@ -59,6 +60,7 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { topLeft += protection topLeft += health + topRight += air topRight += hunger forceSilentApply() diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/JoinGameS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/JoinGameS2CP.kt index 51aec361a..5fdd93353 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/JoinGameS2CP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/JoinGameS2CP.kt @@ -83,7 +83,8 @@ class JoinGameS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() { gamemode = Gamemodes[(gamemodeRaw and (0x8.inv()))] } else { isHardcore = buffer.readBoolean() - gamemode = Gamemodes[buffer.readUnsignedByte()] + buffer.readUnsignedByte() + gamemode = Gamemodes.SURVIVAL } if (buffer.versionId < ProtocolVersions.V_1_9_1) { diff --git a/src/main/resources/assets/minosoft/mapping/atlas.json b/src/main/resources/assets/minosoft/mapping/atlas.json index 07c2fbb0c..ec4df0541 100644 --- a/src/main/resources/assets/minosoft/mapping/atlas.json +++ b/src/main/resources/assets/minosoft/mapping/atlas.json @@ -456,5 +456,19 @@ "start": [34, 9], "end": [43, 18] } + }, + "minecraft:air_bubble": { + "0": { + "texture": "minecraft:textures/gui/icons.png", + "start": [16, 18], + "end": [25, 27] + } + }, + "minecraft:popping_air_bubble": { + "0": { + "texture": "minecraft:textures/gui/icons.png", + "start": [25, 18], + "end": [34, 27] + } } }