hud: protection

This commit is contained in:
Bixilon 2021-10-01 20:15:59 +02:00
parent 9adc958120
commit 3a1ece26e8
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 121 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import de.bixilon.minosoft.data.registries.enchantment.Enchantment
import de.bixilon.minosoft.data.registries.entities.EntityType import de.bixilon.minosoft.data.registries.entities.EntityType
import de.bixilon.minosoft.data.registries.fluid.FlowableFluid import de.bixilon.minosoft.data.registries.fluid.FlowableFluid
import de.bixilon.minosoft.data.registries.fluid.Fluid import de.bixilon.minosoft.data.registries.fluid.Fluid
import de.bixilon.minosoft.data.registries.items.armor.ArmorItem
import de.bixilon.minosoft.data.registries.particle.data.BlockParticleData import de.bixilon.minosoft.data.registries.particle.data.BlockParticleData
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.gui.rendering.input.camera.EntityPositionInfo import de.bixilon.minosoft.gui.rendering.input.camera.EntityPositionInfo
@ -585,6 +586,22 @@ abstract class Entity(
} }
} }
val protectionLevel: Float
get() {
var protectionLevel = 0.0f
for (equipment in equipment.toSynchronizedMap().values) {
val item = equipment.item
if (item is ArmorItem) {
// could also be a pumpkin or just trash
protectionLevel += item.protection
}
}
return protectionLevel
}
companion object { companion object {
private val BELOW_POSITION_MINUS = Vec3(0, 0.20000000298023224f, 0) private val BELOW_POSITION_MINUS = Vec3(0, 0.20000000298023224f, 0)
} }

View File

@ -30,6 +30,7 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) {
private val experience = HotbarExperienceBarElement(hudRenderer) private val experience = HotbarExperienceBarElement(hudRenderer)
private val health = HotbarHealthElement(hudRenderer) private val health = HotbarHealthElement(hudRenderer)
private val hunger = HotbarHungerElement(hudRenderer) private val hunger = HotbarHungerElement(hudRenderer)
private val protection = HotbarProtectionElement(hudRenderer)
private val topLeft = RowLayout(hudRenderer, HorizontalAlignments.LEFT, 1) // contains health, armor, etc private val topLeft = RowLayout(hudRenderer, HorizontalAlignments.LEFT, 1) // contains health, armor, etc
private val topRight = RowLayout(hudRenderer, HorizontalAlignments.RIGHT, 1) // contains hunger, air private val topRight = RowLayout(hudRenderer, HorizontalAlignments.RIGHT, 1) // contains hunger, air
@ -43,11 +44,13 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) {
override var cacheEnabled: Boolean = false // ToDo: Cache correctly override var cacheEnabled: Boolean = false // ToDo: Cache correctly
init { init {
topLeft += health
topLeft.parent = this topLeft.parent = this
topRight.parent = this
topLeft += protection
topLeft += health
topRight += hunger topRight += hunger
topRight.parent = this
silentApply() silentApply()
} }

View File

@ -146,7 +146,7 @@ class HotbarHealthElement(hudRenderer: HUDRenderer) : Element(hudRenderer) {
private var rows = 0 private var rows = 0
override fun forceRender(offset: Vec2i, z: Int, consumer: GUIVertexConsumer): Int { override fun forceRender(offset: Vec2i, z: Int, consumer: GUIVertexConsumer): Int {
// ToDo: Damage animation, regeneration, caching // ToDo: Damage animation, regeneration, caching, stacking
for (heart in 0 until totalMaxHearts) { for (heart in 0 until totalMaxHearts) {
val row = heart / HEARTS_PER_ROW val row = heart / HEARTS_PER_ROW
val column = heart % HEARTS_PER_ROW val column = heart % HEARTS_PER_ROW

View File

@ -0,0 +1,77 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*
* 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.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 HotbarProtectionElement(hudRenderer: HUDRenderer) : Element(hudRenderer) {
private val emptyProtection = hudRenderer.atlasManager["minecraft:empty_protection"]!!
private val halfProtection = hudRenderer.atlasManager["minecraft:half_protection"]!!
private val fullProtection = hudRenderer.atlasManager["minecraft:full_protection"]!!
init {
silentApply()
}
private var protection = 0.0f
override fun forceRender(offset: Vec2i, z: Int, consumer: GUIVertexConsumer): Int {
if (protection <= 0.0f) {
return 0
}
var protectionLeft = protection
for (i in 0 until 10) {
val atlasElement = when {
protectionLeft <= 0.0f -> emptyProtection
protectionLeft < 1.0f -> halfProtection
else -> fullProtection
}
val image = ImageElement(hudRenderer, atlasElement)
image.render(offset + Vec2i(i * ARMOR_SIZE.x, 0), z, consumer)
protectionLeft -= 1.0f
}
return 1
}
override fun silentApply() {
val protection = hudRenderer.connection.player.protectionLevel // ToDo: Check for equipment change
if (this.protection == protection) {
return
}
size = if (protection <= 0.0f) {
Vec2i.EMPTY
} else {
SIZE
}
cacheUpToDate = false
}
companion object {
private val ARMOR_SIZE = Vec2i(8, 9)
private val SIZE = Vec2i(10 * ARMOR_SIZE.x, ARMOR_SIZE.y)
}
}

View File

@ -435,5 +435,26 @@
"start": [0, 89], "start": [0, 89],
"end": [182, 94] "end": [182, 94]
} }
},
"minecraft:empty_protection": {
"0": {
"texture": "minecraft:textures/gui/icons.png",
"start": [16, 9],
"end": [25, 18]
}
},
"minecraft:half_protection": {
"0": {
"texture": "minecraft:textures/gui/icons.png",
"start": [25, 9],
"end": [34, 18]
}
},
"minecraft:full_protection": {
"0": {
"texture": "minecraft:textures/gui/icons.png",
"start": [34, 9],
"end": [43, 18]
}
} }
} }