Skip to content

Commit

Permalink
Fragmentation grenade 'wave' particle
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaHelios committed Jan 10, 2025
1 parent 01dba31 commit be9d6f3
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.pswg.api.GalaxiesClientAddon;
import dev.pswg.particles.FragmentationGrenadeSparkParticle;
import dev.pswg.particles.ExplosionSmokeParticle;
import dev.pswg.particles.FragmentationGrenadeWaveParticle;
import dev.pswg.renderer.FragmentationGrenadeEntityRenderer;
import dev.pswg.renderer.ThermalDetonatorEntityRenderer;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
Expand All @@ -24,6 +25,7 @@ public void onGalaxiesClientReady()

ParticleFactoryRegistry.getInstance().register(Gadgets.EXPLOSION_SMOKE_PARTICLE, ExplosionSmokeParticle.Factory::new);
ParticleFactoryRegistry.getInstance().register(Gadgets.FRAGMENTATION_GRENADE_SPARK, FragmentationGrenadeSparkParticle.Factory::new);
ParticleFactoryRegistry.getInstance().register(Gadgets.FRAGMENTATION_GRENADE_WAVE, FragmentationGrenadeWaveParticle.Factory::new);

Gadgets.LOGGER.info("Client module initialized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ protected void configure(RegistryWrapper.WrapperLookup wrapperLookup)
.add(Blocks.FERN)
.add(Blocks.LARGE_FERN)
.add(Blocks.BROWN_MUSHROOM)
.add(Blocks.RED_MUSHROOM);
.add(Blocks.RED_MUSHROOM)
.add(Blocks.DEAD_BUSH)
.add(Blocks.SHORT_GRASS)
.add(Blocks.TALL_GRASS);

getOrCreateTagBuilder(Gadgets.DETONATES_GRENADE)
.add(Blocks.REDSTONE_BLOCK)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package dev.pswg.particles;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.particle.*;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.SimpleParticleType;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;

@Environment(value = EnvType.CLIENT)
public class FragmentationGrenadeWaveParticle extends SpriteBillboardParticle
{
private float scaleX = 1;
private float scaleY = 1;

protected FragmentationGrenadeWaveParticle(ClientWorld clientWorld, double x, double y, double z, double vX, double vY, double vZ, SpriteProvider spriteProvider)
{
super(clientWorld, x, y, z);
setBoundingBoxSpacing(0.25f, 0.25f);
this.collidesWithWorld = false;
velocityX = vX;
velocityY = vY + (double)(random.nextFloat() / 500.0f);
velocityZ = vZ;
this.maxAge = 20;
this.scale = 1;
this.setSprite(spriteProvider);
this.setColor(Random.create().nextBetween(0, 2) / 2f + 0.98f, Random.create().nextBetween(0, 2) / 2f + 0.98f, 1);
}

private void updateShape(float age)
{
if (age <= 10)
{
this.alpha = 1;

this.scaleX = 2 * (1 + 2 * (float)Math.max(4 * Math.pow(age / 10 - 0.5f, 3), 0));
this.scaleY = 2 * (1 - (float)Math.pow((age - 4) / 6, 2));
}
else
{
this.scaleX = this.scaleY = 1;
this.scale = (float)Math.pow(age / 10, 6);
this.alpha = (float)(Math.max(1 - (Math.pow(age / 20, 3) * 2), 0.001f));
}
}

@Override
public ParticleTextureSheet getType()
{
return ParticleTextureSheet.PARTICLE_SHEET_TRANSLUCENT;
}

@Override
public void render(VertexConsumer vertexConsumer, Camera camera, float tickDelta)
{
updateShape(this.age + tickDelta);

Vec3d vec3d = camera.getPos();
float f = (float)(MathHelper.lerp((double)tickDelta, this.prevPosX, this.x) - vec3d.getX());
float g = (float)(MathHelper.lerp((double)tickDelta, this.prevPosY, this.y) - vec3d.getY());
float h = (float)(MathHelper.lerp((double)tickDelta, this.prevPosZ, this.z) - vec3d.getZ());

Quaternionf quaternionf = new Quaternionf();
this.getRotator().setRotation(quaternionf, camera, tickDelta);
if (this.angle != 0.0F)
{
quaternionf.rotateZ(MathHelper.lerp(tickDelta, this.prevAngle, this.angle));
}

Vector3f[] corners = 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 size = this.getSize(tickDelta);

for (int j = 0; j < 4; ++j)
{
Vector3f vector3f = corners[j];
vector3f.mul(scaleX, scaleY, 1);
vector3f.rotate(new Quaternionf(camera.getRotation().rotateZ((float)Math.toRadians(180d))));
vector3f.mul(size);
vector3f.add(f, g, h);
}

float k = this.getMinU();
float l = this.getMaxU();
float m = this.getMinV();
float n = this.getMaxV();
int o = this.getBrightness(tickDelta);
vertexConsumer.vertex(corners[0].x(), corners[0].y(), corners[0].z())
.texture(l, n)
.color(this.red, this.green, this.blue, this.alpha)
.light(o);
vertexConsumer.vertex(corners[1].x(), corners[1].y(), corners[1].z())
.texture(l, m)
.color(this.red, this.green, this.blue, this.alpha)
.light(o);
vertexConsumer.vertex(corners[2].x(), corners[2].y(), corners[2].z())
.texture(k, m)
.color(this.red, this.green, this.blue, this.alpha)
.light(o);
vertexConsumer.vertex(corners[3].x(), corners[3].y(), corners[3].z())
.texture(k, n)
.color(this.red, this.green, this.blue, this.alpha)
.light(o);
}

@Environment(value = EnvType.CLIENT)
public static class Factory implements ParticleFactory<SimpleParticleType>
{
private final SpriteProvider spriteProvider;

public Factory(SpriteProvider spriteProvider)
{
this.spriteProvider = spriteProvider;
}

@Nullable
@Override
public Particle createParticle(SimpleParticleType parameters, ClientWorld world, double x, double y, double z, double velocityX, double velocityY, double velocityZ)
{
FragmentationGrenadeWaveParticle fragmentationGrenadeParticle = new FragmentationGrenadeWaveParticle(world, x, y, z, velocityX, velocityY, velocityZ, spriteProvider);
fragmentationGrenadeParticle.setSprite(spriteProvider);
return fragmentationGrenadeParticle;
}
}
}
1 change: 1 addition & 0 deletions projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static Identifier id(String path)

public static final SimpleParticleType EXPLOSION_SMOKE_PARTICLE = Registry.register(Registries.PARTICLE_TYPE, id("explosion_smoke"), FabricParticleTypes.simple());
public static final SimpleParticleType FRAGMENTATION_GRENADE_SPARK = Registry.register(Registries.PARTICLE_TYPE, id("fragmentation_grenade_spark"), FabricParticleTypes.simple());
public static final SimpleParticleType FRAGMENTATION_GRENADE_WAVE = Registry.register(Registries.PARTICLE_TYPE, id("fragmentation_grenade_wave"), FabricParticleTypes.simple());

public static final SoundEvent ARM = registerSound(id("shared.arm"));
public static final SoundEvent DISARM = registerSound(id("shared.disarm"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -37,6 +38,7 @@ public FragmentationGrenadeEntity(EntityType<FragmentationGrenadeEntity> type, W
setExplosionPower(4f);
}


@Override
public GrenadeItem getItem()
{
Expand All @@ -54,8 +56,7 @@ public void explode()
{
if (getWorld() instanceof ServerWorld serverWorld)
{
//for (ServerPlayerEntity serverPlayerEntity : serverWorld.getPlayers())
// serverWorld.spawnParticles(serverPlayerEntity, SwgParticleTypes.FRAGMENTATION_GRENADE, true, getX(), getY(), getZ(), 1, 0, 0, 0, 0);
serverWorld.spawnParticles(Gadgets.FRAGMENTATION_GRENADE_WAVE, false, true, getX(), getY() + 0.05d, getZ(), 1, 0, 0, 0, 0);
var passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBoolean(true);
passedData.writeInt(getId());
Expand Down Expand Up @@ -120,7 +121,7 @@ public void tick()

if (EXPLOSION_TICK == 7)
{
for (int i = 0; i < Random.create().nextBetween(50, 80); i++)
for (int i = 0; i < Random.create().nextBetween(70, 100); i++)
{
double vx = getWorld().random.nextGaussian() * 0.5;
double vz = getWorld().random.nextGaussian() * 0.5;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"textures": [
"pswg_gadgets:fragmentation_grenade_wave"
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit be9d6f3

Please sign in to comment.