NaturesAura/src/main/java/de/ellpeck/naturesaura/particles/ParticleMagic.java

128 lines
5 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.particles;
2021-12-08 00:31:29 +01:00
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
2018-10-13 20:35:18 +02:00
import de.ellpeck.naturesaura.NaturesAura;
2021-12-08 00:31:29 +01:00
import net.minecraft.client.Camera;
2021-12-06 14:38:12 +01:00
import net.minecraft.client.multiplayer.ClientLevel;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.particle.Particle;
2021-12-08 00:31:29 +01:00
import net.minecraft.client.particle.ParticleRenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2018-10-13 20:35:18 +02:00
2021-12-08 00:31:29 +01:00
import java.util.Collections;
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2018-10-13 20:35:18 +02:00
public class ParticleMagic extends Particle {
2019-01-30 15:29:14 +01:00
public static final ResourceLocation TEXTURE = new ResourceLocation(NaturesAura.MOD_ID, "textures/particles/magic_round.png");
2018-10-13 20:35:18 +02:00
private final float desiredScale;
2018-10-14 14:27:18 +02:00
private final boolean fade;
private final boolean depth;
2020-01-21 23:02:39 +01:00
private float particleScale;
2018-10-13 20:35:18 +02:00
2021-12-04 15:40:09 +01:00
public ParticleMagic(ClientLevel level, double posX, double posY, double posZ, double motionX, double motionY, double motionZ, int color, float scale, int maxAge, float gravity, boolean collision, boolean fade, boolean depth) {
super(level, posX, posY, posZ);
2018-10-13 20:35:18 +02:00
this.desiredScale = scale;
2021-12-08 00:31:29 +01:00
this.lifetime = maxAge;
this.hasPhysics = collision;
this.gravity = gravity;
2018-10-14 14:27:18 +02:00
this.fade = fade;
this.depth = depth;
2018-10-13 20:35:18 +02:00
2021-12-08 00:31:29 +01:00
this.xd = motionX;
this.yd = motionY;
this.zd = motionZ;
2018-10-13 20:35:18 +02:00
2021-12-15 16:30:22 +01:00
var r = (color >> 16 & 255) / 255F * (1F - this.random.nextFloat() * 0.25F);
var g = (color >> 8 & 255) / 255F * (1F - this.random.nextFloat() * 0.25F);
var b = (color & 255) / 255F * (1F - this.random.nextFloat() * 0.25F);
2020-01-21 23:02:39 +01:00
this.setColor(r, g, b);
2018-10-13 20:35:18 +02:00
2021-12-08 00:31:29 +01:00
this.alpha = 1F;
2019-01-28 20:19:08 +01:00
this.particleScale = 0F;
2018-10-13 20:35:18 +02:00
}
@Override
2020-01-21 23:02:39 +01:00
public void tick() {
2021-12-08 00:31:29 +01:00
this.xo = this.x;
this.yo = this.y;
this.zo = this.z;
2018-10-13 20:35:18 +02:00
2020-01-21 23:02:39 +01:00
this.age++;
2021-12-08 00:31:29 +01:00
if (this.age > this.lifetime) {
this.remove();
2018-10-14 14:27:18 +02:00
} else {
2021-12-08 00:31:29 +01:00
this.yd -= 0.04D * (double) this.gravity;
this.move(this.xd, this.yd, this.zd);
2018-10-14 14:27:18 +02:00
2021-12-15 16:30:22 +01:00
var lifeRatio = (float) this.age / (float) this.lifetime;
2019-01-29 19:55:38 +01:00
if (this.fade && lifeRatio > 0.75F)
2021-12-08 00:31:29 +01:00
this.alpha = 1F - (lifeRatio - 0.75F) / 0.25F;
if (lifeRatio <= 0.25F)
this.particleScale = this.desiredScale * (lifeRatio / 0.25F);
else if (this.fade)
this.particleScale = this.desiredScale * (1F - (lifeRatio - 0.25F) / 0.75F);
2018-10-14 14:27:18 +02:00
}
2018-10-13 20:35:18 +02:00
}
@Override
public void move(double x, double y, double z) {
2021-12-15 16:30:22 +01:00
var lastY = y;
2021-12-08 00:31:29 +01:00
if (this.hasPhysics && (x != 0 || y != 0 || z != 0)) {
2021-12-15 16:30:22 +01:00
var motion = Entity.collideBoundingBox(null, new Vec3(x, y, z), this.getBoundingBox(), this.level, Collections.emptyList());
x = motion.x;
y = motion.y;
z = motion.z;
}
if (x != 0 || y != 0 || z != 0) {
2021-12-08 00:31:29 +01:00
this.setBoundingBox(this.getBoundingBox().move(x, y, z));
this.setLocationFromBoundingbox();
}
this.onGround = lastY != y && lastY < 0;
if (this.onGround) {
2021-12-08 00:31:29 +01:00
this.xd = 0;
this.zd = 0;
}
}
@Override
2021-12-08 00:31:29 +01:00
public void render(VertexConsumer buffer, Camera renderInfo, float partialTicks) {
2021-12-15 16:30:22 +01:00
var vec3d = renderInfo.getPosition();
var f = (float) (Mth.lerp(partialTicks, this.xo, this.x) - vec3d.x);
var f1 = (float) (Mth.lerp(partialTicks, this.yo, this.y) - vec3d.y);
var f2 = (float) (Mth.lerp(partialTicks, this.zo, this.z) - vec3d.z);
var quaternion = renderInfo.rotation();
var avector3f = new Vector3f[]{new Vector3f(-1.0F, -1.0F, 0.0F), new Vector3f(-1.0F, 1.0F, 0.0F), new Vector3f(1.0F, 1.0F, 0.0F), new Vector3f(1.0F, -1.0F, 0.0F)};
var f4 = 0.1F * this.particleScale;
for (var i = 0; i < 4; ++i) {
var vector3f = avector3f[i];
2020-01-28 18:08:56 +01:00
vector3f.transform(quaternion);
vector3f.mul(f4);
vector3f.add(f, f1, f2);
}
2021-12-15 16:30:22 +01:00
var j = this.getLightColor(partialTicks);
2021-12-08 00:31:29 +01:00
buffer.vertex(avector3f[0].x(), avector3f[0].y(), avector3f[0].z()).uv(0, 1).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
buffer.vertex(avector3f[1].x(), avector3f[1].y(), avector3f[1].z()).uv(1, 1).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
buffer.vertex(avector3f[2].x(), avector3f[2].y(), avector3f[2].z()).uv(1, 0).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
buffer.vertex(avector3f[3].x(), avector3f[3].y(), avector3f[3].z()).uv(0, 0).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
2018-10-13 20:35:18 +02:00
}
2020-01-21 23:02:39 +01:00
@Override
2021-12-08 00:31:29 +01:00
public ParticleRenderType getRenderType() {
return this.depth ? ParticleHandler.MAGIC : ParticleHandler.MAGIC_NO_DEPTH;
2020-01-21 23:02:39 +01:00
}
2018-10-13 20:35:18 +02:00
@Override
2021-12-08 00:31:29 +01:00
protected int getLightColor(float p_107249_) {
2018-10-13 20:35:18 +02:00
return 15 << 20 | 15 << 4;
}
}