From 53d7789e15dc40f29ca755259ddbe2efd5794e65 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 26 May 2021 20:45:38 +0200 Subject: [PATCH] particle sprite animations (dependant on age) --- .../rendering/particle/ParticleRenderer.kt | 6 ++-- .../particle/types/ExplosionParticle.kt | 33 +++++++++++++++++++ .../gui/rendering/particle/types/Particle.kt | 23 ++++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/ExplosionParticle.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/particle/ParticleRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/ParticleRenderer.kt index 15673a6c4..c7e1e5e61 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/particle/ParticleRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/ParticleRenderer.kt @@ -19,7 +19,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.Renderer import de.bixilon.minosoft.gui.rendering.RendererBuilder import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent -import de.bixilon.minosoft.gui.rendering.particle.types.HappyVillagerParticle +import de.bixilon.minosoft.gui.rendering.particle.types.ExplosionParticle import de.bixilon.minosoft.gui.rendering.particle.types.Particle import de.bixilon.minosoft.gui.rendering.shader.Shader import de.bixilon.minosoft.gui.rendering.textures.Texture @@ -60,9 +60,9 @@ class ParticleRenderer( } val random = Random.Default - val type = connection.registries.particleTypeRegistry[HappyVillagerParticle.RESOURCE_LOCATION]!! + val type = connection.registries.particleTypeRegistry[ExplosionParticle.RESOURCE_LOCATION]!! for (i in 0 until 10000) { - val particle = HappyVillagerParticle(connection, Vec3(random.nextFloat(0.0f, 50.0f), random.nextFloat(6.0f, 50.0f), random.nextFloat(0.0f, 50.0f)), ParticleData(type), Random(random.nextLong())) + val particle = ExplosionParticle(connection, Vec3(random.nextFloat(0.0f, 50.0f), random.nextFloat(6.0f, 50.0f), random.nextFloat(0.0f, 50.0f)), ParticleData(type), Random(random.nextLong())) // particle.grow(0.5f, 20000L) // particle.velocity = Vec3(1f, 0.2f, 1f) // particle.friction = Vec3(0.1f, 0.1f, 0.1f) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/ExplosionParticle.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/ExplosionParticle.kt new file mode 100644 index 000000000..b24be1b35 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/ExplosionParticle.kt @@ -0,0 +1,33 @@ +/* + * 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.particle.types + +import de.bixilon.minosoft.data.mappings.ResourceLocation +import de.bixilon.minosoft.data.mappings.particle.data.ParticleData +import de.bixilon.minosoft.gui.rendering.particle.ParticleFactory +import de.bixilon.minosoft.protocol.network.connection.PlayConnection +import de.bixilon.minosoft.util.KUtil.asResourceLocation +import glm_.vec3.Vec3 +import kotlin.random.Random + +class ExplosionParticle(connection: PlayConnection, position: Vec3, data: ParticleData, random: Random) : Particle(connection, position, data, random) { + + companion object : ParticleFactory { + override val RESOURCE_LOCATION: ResourceLocation = "minecraft:explosion".asResourceLocation() + + override fun build(connection: PlayConnection, position: Vec3, data: ParticleData, random: Random): ExplosionParticle { + return ExplosionParticle(connection, position, data, random) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/Particle.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/Particle.kt index 105eb89c4..e6b90037c 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/Particle.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/particle/types/Particle.kt @@ -25,7 +25,7 @@ import kotlin.math.abs import kotlin.random.Random abstract class Particle(protected val connection: PlayConnection, protected val position: Vec3, protected val data: ParticleData, protected val random: Random) { - protected val texture = connection.rendering!!.renderWindow.textures.allTextures[data.type.textures.random()]!! + protected var texture = connection.rendering!!.renderWindow.textures.allTextures[data.type.textures.first()]!! protected var scale: Float = 0.1f protected var color: RGBColor = ChatColors.WHITE @@ -39,7 +39,7 @@ abstract class Particle(protected val connection: PlayConnection, protected val var dead = false var age: Int = 0 protected set - var maxAge: Int = 100000 + random.nextInt(0, 10000) + var maxAge: Int = 10000 + random.nextInt(0, 10000) // moving var friction = Vec3.EMPTY @@ -107,7 +107,7 @@ abstract class Particle(protected val connection: PlayConnection, protected val hoverMaxY - position.y } val totalDistance = hoverMaxY - hoverMinY - val yVelocity = 1 / (totalDistance / distanceToMiddle) + val yFriction = 1 / (totalDistance / distanceToMiddle) when { @@ -120,11 +120,24 @@ abstract class Particle(protected val connection: PlayConnection, protected val velocity.y = -1.0f } else -> { - friction.y = yVelocity + friction.y = yFriction } } } + private fun checkSpriteTexture() { + val totalTextures = data.type.textures.size + if (totalTextures <= 1) { + return + } + // calculate next texture + val nextTextureResourceLocation = data.type.textures[age / ((maxAge / totalTextures) + 1)] + if (texture.resourceLocation == nextTextureResourceLocation) { + return + } + texture = connection.rendering!!.renderWindow.textures.allTextures[nextTextureResourceLocation]!! + } + open fun tick() { check(!dead) { "Cannot tick dead particle!" } val currentTime = System.currentTimeMillis() @@ -147,6 +160,8 @@ abstract class Particle(protected val connection: PlayConnection, protected val move(deltaTime) hover(deltaTime) + checkSpriteTexture() + lastTickTime = currentTime }