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

130 lines
5.6 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.particles;
2020-01-28 18:08:56 +01:00
import com.mojang.blaze3d.vertex.IVertexBuilder;
2018-10-13 20:35:18 +02:00
import de.ellpeck.naturesaura.NaturesAura;
2021-12-06 14:38:12 +01:00
import net.minecraft.client.multiplayer.ClientLevel;
2020-01-21 23:02:39 +01:00
import net.minecraft.client.particle.IParticleRenderType;
2018-10-13 20:35:18 +02:00
import net.minecraft.client.particle.Particle;
2020-01-21 23:02:39 +01:00
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.entity.Entity;
2018-10-13 20:35:18 +02:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.ReuseableStream;
2021-12-04 19:17:21 +01:00
import net.minecraft.util.math.Mth;
import net.minecraft.util.math.shapes.ISelectionContext;
2020-09-22 03:17:02 +02:00
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;
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
import java.util.stream.Stream;
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;
2020-01-21 23:02:39 +01:00
this.maxAge = maxAge;
2018-10-13 20:35:18 +02:00
this.canCollide = collision;
this.particleGravity = gravity;
2018-10-14 14:27:18 +02:00
this.fade = fade;
this.depth = depth;
2018-10-13 20:35:18 +02:00
this.motionX = motionX;
this.motionY = motionY;
this.motionZ = motionZ;
2020-01-28 18:08:56 +01:00
float r = (color >> 16 & 255) / 255F * (1F - this.rand.nextFloat() * 0.25F);
float g = (color >> 8 & 255) / 255F * (1F - this.rand.nextFloat() * 0.25F);
float b = (color & 255) / 255F * (1F - this.rand.nextFloat() * 0.25F);
2020-01-21 23:02:39 +01:00
this.setColor(r, g, b);
2018-10-13 20:35:18 +02:00
2019-01-28 20:19:08 +01:00
this.particleAlpha = 1F;
this.particleScale = 0F;
2018-10-13 20:35:18 +02:00
}
@Override
2020-01-21 23:02:39 +01:00
public void tick() {
2018-10-14 14:27:18 +02:00
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
2018-10-13 20:35:18 +02:00
2020-01-21 23:02:39 +01:00
this.age++;
if (this.age > this.maxAge) {
2018-10-14 14:27:18 +02:00
this.setExpired();
} else {
this.motionY -= 0.04D * (double) this.particleGravity;
this.move(this.motionX, this.motionY, this.motionZ);
2020-01-21 23:02:39 +01:00
float lifeRatio = (float) this.age / (float) this.maxAge;
2019-01-29 19:55:38 +01:00
if (this.fade && lifeRatio > 0.75F)
this.particleAlpha = 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) {
double lastY = y;
if (this.canCollide && (x != 0 || y != 0 || z != 0)) {
2021-12-04 15:40:09 +01:00
Vector3d motion = Entity.collideBoundingBoxHeuristically(null, new Vector3d(x, y, z), this.getBoundingBox(), this.level, ISelectionContext.dummy(), new ReuseableStream<>(Stream.empty()));
x = motion.x;
y = motion.y;
z = motion.z;
}
if (x != 0 || y != 0 || z != 0) {
this.setBoundingBox(this.getBoundingBox().offset(x, y, z));
this.resetPositionToBB();
}
this.onGround = lastY != y && lastY < 0;
if (this.onGround) {
this.motionX = 0;
this.motionZ = 0;
}
}
@Override
2020-01-28 18:08:56 +01:00
public void renderParticle(IVertexBuilder buffer, ActiveRenderInfo renderInfo, float partialTicks) {
2020-09-22 03:17:02 +02:00
Vector3d vec3d = renderInfo.getProjectedView();
2021-12-04 19:17:21 +01:00
float f = (float) (Mth.lerp(partialTicks, this.prevPosX, this.posX) - vec3d.getX());
float f1 = (float) (Mth.lerp(partialTicks, this.prevPosY, this.posY) - vec3d.getY());
float f2 = (float) (Mth.lerp(partialTicks, this.prevPosZ, this.posZ) - vec3d.getZ());
2020-01-28 18:08:56 +01:00
Quaternion quaternion = renderInfo.getRotation();
Vector3f[] 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)};
float f4 = 0.1F * this.particleScale;
for (int i = 0; i < 4; ++i) {
Vector3f vector3f = avector3f[i];
vector3f.transform(quaternion);
vector3f.mul(f4);
vector3f.add(f, f1, f2);
}
int j = this.getBrightnessForRender(partialTicks);
buffer.pos(avector3f[0].getX(), avector3f[0].getY(), avector3f[0].getZ()).tex(0, 1).color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j).endVertex();
buffer.pos(avector3f[1].getX(), avector3f[1].getY(), avector3f[1].getZ()).tex(1, 1).color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j).endVertex();
buffer.pos(avector3f[2].getX(), avector3f[2].getY(), avector3f[2].getZ()).tex(1, 0).color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j).endVertex();
buffer.pos(avector3f[3].getX(), avector3f[3].getY(), avector3f[3].getZ()).tex(0, 0).color(this.particleRed, this.particleGreen, this.particleBlue, this.particleAlpha).lightmap(j).endVertex();
2018-10-13 20:35:18 +02:00
}
2020-01-21 23:02:39 +01:00
@Override
public IParticleRenderType 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
public int getBrightnessForRender(float f) {
return 15 << 20 | 15 << 4;
}
}