aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-04-18 20:10:16 +0200
committerLinnea Gräf <nea@nea.moe>2024-04-18 20:23:44 +0200
commitc264ca9e8f9f2b0aed457753c43bb4e25edb0ef1 (patch)
treef0eac8c4902bca00f9e44bc4d0e9b6eb73e7144d /src/main/kotlin/moe/nea/firmament/util
parentfbe94315a45082bdf50d0a95583725352e3f20ec (diff)
downloadFirmament-c264ca9e8f9f2b0aed457753c43bb4e25edb0ef1.tar.gz
Firmament-c264ca9e8f9f2b0aed457753c43bb4e25edb0ef1.tar.bz2
Firmament-c264ca9e8f9f2b0aed457753c43bb4e25edb0ef1.zip
Add pickaxe ability timer
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt7
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/render/LerpUtils.kt23
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/render/RenderCircleProgress.kt99
3 files changed, 128 insertions, 1 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt
index d226da0..80628ae 100644
--- a/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt
+++ b/src/main/kotlin/moe/nea/firmament/util/item/NbtItemData.kt
@@ -1,5 +1,6 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
@@ -17,7 +18,11 @@ fun textFromNbt() {
val ItemStack.loreAccordingToNbt
get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).getList(ItemStack.LORE_KEY, NbtElement.STRING_TYPE.toInt())
- .map { lazy(LazyThreadSafetyMode.NONE) { Text.Serialization.fromJson((it as NbtString).asString()) } }
+ .map {
+ lazy(LazyThreadSafetyMode.NONE) {
+ Text.Serialization.fromJson((it as NbtString).asString())
+ }
+ }
val ItemStack.displayNameAccordingToNbt
get() = getOrCreateSubNbt(ItemStack.DISPLAY_KEY).let {
diff --git a/src/main/kotlin/moe/nea/firmament/util/render/LerpUtils.kt b/src/main/kotlin/moe/nea/firmament/util/render/LerpUtils.kt
new file mode 100644
index 0000000..a979f8d
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/render/LerpUtils.kt
@@ -0,0 +1,23 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.util.render
+
+val pi = Math.PI
+val tau = Math.PI * 2
+fun lerpAngle(a: Float, b: Float, progress: Float): Float {
+ // TODO: there is at least 10 mods to many in here lol
+ val shortestAngle = ((((b.mod(tau) - a.mod(tau)).mod(tau)) + tau + pi).mod(tau)) - pi
+ return ((a + (shortestAngle) * progress).mod(tau)).toFloat()
+}
+
+fun lerp(a: Float, b: Float, progress: Float): Float {
+ return a + (b - a) * progress
+}
+
+fun ilerp(a: Float, b: Float, value: Float): Float {
+ return (value - a) / (b - a)
+}
diff --git a/src/main/kotlin/moe/nea/firmament/util/render/RenderCircleProgress.kt b/src/main/kotlin/moe/nea/firmament/util/render/RenderCircleProgress.kt
new file mode 100644
index 0000000..a8245ca
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/render/RenderCircleProgress.kt
@@ -0,0 +1,99 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.util.render
+
+import com.mojang.blaze3d.systems.RenderSystem
+import org.joml.Matrix4f
+import org.joml.Vector2f
+import kotlin.math.atan2
+import kotlin.math.tan
+import net.minecraft.client.gui.DrawContext
+import net.minecraft.client.render.BufferRenderer
+import net.minecraft.client.render.GameRenderer
+import net.minecraft.client.render.Tessellator
+import net.minecraft.client.render.VertexFormat.DrawMode
+import net.minecraft.client.render.VertexFormats
+import net.minecraft.util.Identifier
+
+object RenderCircleProgress {
+
+ fun renderCircle(
+ drawContext: DrawContext,
+ texture: Identifier,
+ progress: Float,
+ u1: Float,
+ u2: Float,
+ v1: Float,
+ v2: Float,
+ ) {
+ RenderSystem.setShaderTexture(0, texture)
+ RenderSystem.setShader { GameRenderer.getPositionColorTexProgram() }
+ RenderSystem.enableBlend()
+ val matrix: Matrix4f = drawContext.matrices.peek().positionMatrix
+ val bufferBuilder = Tessellator.getInstance().buffer
+ bufferBuilder.begin(DrawMode.TRIANGLES, VertexFormats.POSITION_COLOR_TEXTURE)
+ bufferBuilder.fixedColor(255, 255, 255, 255)
+
+ val corners = listOf(
+ Vector2f(0F, -1F),
+ Vector2f(1F, -1F),
+ Vector2f(1F, 0F),
+ Vector2f(1F, 1F),
+ Vector2f(0F, 1F),
+ Vector2f(-1F, 1F),
+ Vector2f(-1F, 0F),
+ Vector2f(-1F, -1F),
+ )
+
+ for (i in (0 until 8)) {
+ if (progress < i / 8F) {
+ break
+ }
+ val second = corners[(i + 1) % 8]
+ val first = corners[i]
+ if (progress <= (i + 1) / 8F) {
+ val internalProgress = 1 - (progress - i / 8F) * 8F
+ val angle = lerpAngle(
+ atan2(second.y, second.x),
+ atan2(first.y, first.x),
+ internalProgress
+ )
+ if (angle < tau / 8 || angle >= tau * 7 / 8) {
+ second.set(1F, tan(angle))
+ } else if (angle < tau * 3 / 8) {
+ second.set(1 / tan(angle), 1F)
+ } else if (angle < tau * 5 / 8) {
+ second.set(-1F, -tan(angle))
+ } else {
+ second.set(-1 / tan(angle), -1F)
+ }
+ }
+
+ fun ilerp(f: Float): Float =
+ ilerp(-1f, 1f, f)
+
+ bufferBuilder
+ .vertex(matrix, second.x, second.y, 0F)
+ .texture(lerp(u1, u2, ilerp(second.x)), lerp(v1, v2, ilerp(second.y)))
+ .next()
+ bufferBuilder
+ .vertex(matrix, first.x, first.y, 0F)
+ .texture(lerp(u1, u2, ilerp(first.x)), lerp(v1, v2, ilerp(first.y)))
+ .next()
+ bufferBuilder
+ .vertex(matrix, 0F, 0F, 0F)
+ .texture(lerp(u1, u2, ilerp(0F)), lerp(v1, v2, ilerp(0F)))
+ .next()
+ }
+ bufferBuilder.unfixColor()
+ BufferRenderer.drawWithGlobalProgram(bufferBuilder.end())
+ RenderSystem.disableBlend()
+ }
+
+
+
+}