From 3a1ece26e88107075c997df175286a1b6f41231c Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 1 Oct 2021 20:15:59 +0200 Subject: [PATCH] hud: protection --- .../minosoft/data/entities/entities/Entity.kt | 17 ++++ .../gui/hud/elements/hotbar/HotbarElement.kt | 7 +- .../elements/hotbar/HotbarHealthElement.kt | 2 +- .../hotbar/HotbarProtectionElement.kt | 77 +++++++++++++++++++ .../assets/minosoft/mapping/atlas.json | 21 +++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt diff --git a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt index 1cbe2df6a..467dd623e 100644 --- a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt +++ b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt @@ -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.fluid.FlowableFluid 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.text.ChatComponent 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 { private val BELOW_POSITION_MINUS = Vec3(0, 0.20000000298023224f, 0) } 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 64ee2b2c2..78a8b0cc0 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 @@ -30,6 +30,7 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { private val experience = HotbarExperienceBarElement(hudRenderer) private val health = HotbarHealthElement(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 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 init { - topLeft += health topLeft.parent = this + topRight.parent = this + + topLeft += protection + topLeft += health topRight += hunger - topRight.parent = this silentApply() } 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 530d1fe98..9e35f7915 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 @@ -146,7 +146,7 @@ class HotbarHealthElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { private var rows = 0 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) { val row = heart / HEARTS_PER_ROW val column = heart % HEARTS_PER_ROW 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 new file mode 100644 index 000000000..e5a1014d3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt @@ -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 . + * + * 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) + } +} diff --git a/src/main/resources/assets/minosoft/mapping/atlas.json b/src/main/resources/assets/minosoft/mapping/atlas.json index 016b3aa0c..07c2fbb0c 100644 --- a/src/main/resources/assets/minosoft/mapping/atlas.json +++ b/src/main/resources/assets/minosoft/mapping/atlas.json @@ -435,5 +435,26 @@ "start": [0, 89], "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] + } } }