Skip to content

Commit

Permalink
Extremely basic spark particles
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jun 21, 2023
1 parent d616b24 commit b9f4023
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.fusionflux.portalcubed.PortalCubedConfig;
import com.fusionflux.portalcubed.blocks.LaserCatcherBlock;
import com.fusionflux.portalcubed.blocks.LaserEmitterBlock;
import com.fusionflux.portalcubed.blocks.LaserRelayBlock;
import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.entity.CorePhysicsEntity;
import com.fusionflux.portalcubed.entity.RedirectionCubeEntity;
import com.fusionflux.portalcubed.particle.PortalCubedParticleTypes;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import com.fusionflux.portalcubed.util.AdvancedEntityRaycast;
import com.fusionflux.portalcubed.util.GeneralUtil;
Expand All @@ -20,6 +22,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -156,6 +159,14 @@ public void tick(Level level, BlockPos pos, BlockState state) {
return;
}

if (hitState != null && !(hitState.getBlock() instanceof LaserRelayBlock)) {
final Vec3 finalPos = multiSegments.get(multiSegments.size() - 1).finalRay().end();
((ServerLevel)level).sendParticles(
PortalCubedParticleTypes.ENERGY_SPARK,
finalPos.x, finalPos.y, finalPos.z, 5, 0, 0, 0, 0.01
);
}

Entity owner = EntityType.MARKER.create(level);
assert owner != null;
alreadyHit.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fusionflux.portalcubed.client.gui.FaithPlateScreen;
import com.fusionflux.portalcubed.client.gui.VelocityHelperScreen;
import com.fusionflux.portalcubed.client.packet.PortalCubedClientPackets;
import com.fusionflux.portalcubed.client.particle.PortalCubedParticleFactories;
import com.fusionflux.portalcubed.client.particle.PortalCubedParticleProviders;
import com.fusionflux.portalcubed.client.render.PortalHud;
import com.fusionflux.portalcubed.client.render.block.EmissiveSpriteRegistry;
import com.fusionflux.portalcubed.client.render.block.entity.*;
Expand Down Expand Up @@ -158,7 +158,7 @@ public void onInitializeClient(ModContainer mod) {
registerEmissiveModels(mod);
PortalCubedClientPackets.registerPackets();
PortalCubedKeyBindings.register();
PortalCubedParticleFactories.register();
PortalCubedParticleProviders.register();

HudRenderCallback.EVENT.register(PortalHud::renderPortalRight);
HudRenderCallback.EVENT.register(PortalHud::renderPortalLeft);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public ParticleRenderType getRenderType() {
}

@ClientOnly
public static class Factory implements ParticleProvider<DecalParticleOption> {
public static class Provider implements ParticleProvider<DecalParticleOption> {
private final FabricSpriteProvider spriteProvider;

private List<TextureAtlasSprite> cacheKey;
private final Map<ResourceLocation, TextureAtlasSprite> spriteCache = new HashMap<>();

public Factory(FabricSpriteProvider spriteProvider) {
public Provider(FabricSpriteProvider spriteProvider) {
this.spriteProvider = spriteProvider;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.fusionflux.portalcubed.client.particle;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleRenderType;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.loader.api.minecraft.ClientOnly;

@ClientOnly
public class EnergySparkParticle extends Particle {
private double xoo, yoo, zoo;

public EnergySparkParticle(
ClientLevel clientLevel,
double x, double y, double z,
double xSpeed, double ySpeed, double zSpeed
) {
super(clientLevel, x, y, z, xSpeed, ySpeed, zSpeed);
xoo = x;
yoo = y;
zoo = z;
friction = 1f;
gravity = 0.8f;
lifetime *= 4;
}

@Override
public void render(VertexConsumer buffer, Camera renderInfo, float partialTicks) {
final PoseStack poseStack = new PoseStack();
final Vec3 position = renderInfo.getPosition();
poseStack.translate(-position.x, -position.y, -position.z);
final MultiBufferSource.BufferSource bufferSource = Minecraft.getInstance().renderBuffers().bufferSource();
final VertexConsumer vertexConsumer = bufferSource.getBuffer(RenderType.lines());
render0(poseStack, vertexConsumer, partialTicks);
bufferSource.endBatch();
}

private void render0(PoseStack poseStack, VertexConsumer vertexConsumer, float partialTicks) {
final float x1 = (float)Mth.lerp(partialTicks, xoo, xo);
final float y1 = (float)Mth.lerp(partialTicks, yoo, yo);
final float z1 = (float)Mth.lerp(partialTicks, zoo, zo);

final float x2 = (float)Mth.lerp(partialTicks, xo, x);
final float y2 = (float)Mth.lerp(partialTicks, yo, y);
final float z2 = (float)Mth.lerp(partialTicks, zo, z);

final float nx = x2 - x1;
final float ny = y2 - y1;
final float nz = z2 - z1;

final PoseStack.Pose matrix = poseStack.last();
vertexConsumer
.vertex(matrix.pose(), x1, y1, z1)
.color(242 / 255f, 177 / 255f, 46 / 255f, 1f)
.normal(matrix.normal(), nx, ny, nz)
.endVertex();
vertexConsumer
.vertex(matrix.pose(), x2, y2, z2)
.color(97 / 255f, 67 / 255f, 6 / 255f, 1f)
.normal(matrix.normal(), nx, ny, nz)
.endVertex();
}

@NotNull
@Override
public ParticleRenderType getRenderType() {
return ParticleRenderType.CUSTOM;
}

@Override
public void tick() {
xoo = xo;
yoo = yo;
zoo = zo;
super.tick();
if (onGround) {
yd = -yd * 0.75;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import org.quiltmc.loader.api.minecraft.ClientOnly;

@ClientOnly
public class PortalCubedParticleFactories {
public class PortalCubedParticleProviders {
public static void register() {
final ParticleFactoryRegistry registry = ParticleFactoryRegistry.getInstance();
registry.register(PortalCubedParticleTypes.DECAL, DecalParticle.Factory::new);
registry.register(PortalCubedParticleTypes.DECAL, DecalParticle.Provider::new);
registry.register(
PortalCubedParticleTypes.ENERGY_SPARK,
sprites -> (type, level, x, y, z, xSpeed, ySpeed, zSpeed) ->
new EnergySparkParticle(level, x, y, z, xSpeed, ySpeed, zSpeed)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fusionflux.portalcubed.items.PortalCubedItems;
import com.fusionflux.portalcubed.listeners.WentThroughPortalListener;
import com.fusionflux.portalcubed.particle.DecalParticleOption;
import com.fusionflux.portalcubed.particle.PortalCubedParticleTypes;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -114,19 +115,23 @@ public void tick() {
if (bouncedDir != null) {
setDeltaMovement(vel);
level.playSound(null, this, PortalCubedSounds.PELLET_BOUNCE_EVENT, SoundSource.HOSTILE, 0.4f, 1f);
if (level instanceof ServerLevel serverWorld) {
final Vec3 spawnPos = serverWorld.clip(new ClipContext(
if (level instanceof ServerLevel serverLevel) {
final Vec3 spawnPos = serverLevel.clip(new ClipContext(
position(),
position().add(vel.with(bouncedDir.getAxis(), -vel.get(bouncedDir.getAxis()))),
ClipContext.Block.COLLIDER,
ClipContext.Fluid.NONE,
this
)).getLocation().add(Vec3.atLowerCornerOf(bouncedDir.getNormal()).scale(0.01));
serverWorld.sendParticles(
serverLevel.sendParticles(
new DecalParticleOption(DecalParticleOption.SCORCH, bouncedDir),
spawnPos.x, spawnPos.y, spawnPos.z,
0, 0, 0, 0, 0
);
serverLevel.sendParticles(
PortalCubedParticleTypes.ENERGY_SPARK,
getX(), getY(), getZ(), 25, 0.1, 0.1, 0.1, 1
);
}
}
if ((tickCount - 1) % 34 == 0) {
Expand Down Expand Up @@ -177,6 +182,12 @@ private void bounceOrKill(LivingEntity entity) {

private void kill(@Nullable LivingEntity entity) {
level.playSound(null, position().x, position().y, position().z, PortalCubedSounds.PELLET_EXPLODE_EVENT, SoundSource.HOSTILE, 0.8f, 1f);
if (level instanceof ServerLevel serverLevel) {
serverLevel.sendParticles(
PortalCubedParticleTypes.ENERGY_SPARK,
getX(), getY(), getZ(), 100, 0.1, 0.1, 0.1, 3.5
);
}
if (entity != null) {
entity.hurt(pcSources(level).vaporization(), PortalCubedConfig.pelletDamage);
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/fusionflux/portalcubed/entity/TurretEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.compat.rayon.RayonIntegration;
import com.fusionflux.portalcubed.particle.DecalParticleOption;
import com.fusionflux.portalcubed.particle.PortalCubedParticleTypes;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import com.fusionflux.portalcubed.util.GeneralUtil;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -104,16 +105,16 @@ public void setPitchSpeed(float pitchSpeed) {
getEntityData().set(PITCH_SPEED, pitchSpeed);
}

public static void makeBulletHole(ServerLevel world, BlockHitResult hit, SoundSource soundCategory) {
final BlockState block = world.getBlockState(hit.getBlockPos());
public static void makeBulletHole(ServerLevel level, BlockHitResult hit, SoundSource soundCategory) {
final BlockState block = level.getBlockState(hit.getBlockPos());
final Vec3 pos = hit.getLocation().add(Vec3.atLowerCornerOf(hit.getDirection().getNormal()).scale(0.01));
final SoundEvent soundEffect;
final ResourceLocation particleTexture;
boolean multiplyTexture = true;
if (block.is(PortalCubedBlocks.BULLET_HOLE_CONCRETE)) {
soundEffect = PortalCubedSounds.BULLET_CONCRETE_EVENT;
particleTexture = DecalParticleOption.BULLET_HOLE_CONCRETE;
world.sendParticles(
level.sendParticles(
new BlockParticleOption(ParticleTypes.BLOCK, block),
pos.x, pos.y, pos.z, 3, 0.1, 0.1, 0.1, 1
);
Expand All @@ -124,15 +125,19 @@ public static void makeBulletHole(ServerLevel world, BlockHitResult hit, SoundSo
} else if (block.is(PortalCubedBlocks.BULLET_HOLE_METAL)) {
soundEffect = PortalCubedSounds.BULLET_METAL_EVENT;
particleTexture = DecalParticleOption.BULLET_HOLE_METAL;
level.sendParticles(
PortalCubedParticleTypes.ENERGY_SPARK,
pos.x, pos.y, pos.z, 50, 0.1, 0.1, 0.1, 2
);
} else {
soundEffect = null;
particleTexture = null;
}
if (soundEffect != null) {
world.playSound(null, pos.x, pos.y, pos.z, soundEffect, soundCategory, 0.3f, 1f);
level.playSound(null, pos.x, pos.y, pos.z, soundEffect, soundCategory, 0.3f, 1f);
}
if (particleTexture != null) {
world.sendParticles(
level.sendParticles(
new DecalParticleOption(particleTexture, hit.getDirection(), multiplyTexture),
pos.x, pos.y, pos.z, 0, 0, 0, 0, 0
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.fusionflux.portalcubed.particle;

import com.mojang.serialization.Codec;
import net.fabricmc.fabric.api.particle.v1.FabricParticleTypes;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.particles.SimpleParticleType;
import net.minecraft.core.registries.BuiltInRegistries;
import org.jetbrains.annotations.NotNull;

Expand All @@ -15,6 +17,11 @@ public class PortalCubedParticleTypes {
public static final ParticleType<DecalParticleOption> DECAL = register(
"decal", false, DecalParticleOption.PARAMETERS_FACTORY, DecalParticleOption::codec
);
public static final SimpleParticleType ENERGY_SPARK = register("energy_spark", false);

private static SimpleParticleType register(String key, boolean overrideLimiter) {
return Registry.register(BuiltInRegistries.PARTICLE_TYPE, id(key), FabricParticleTypes.simple(overrideLimiter));
}

@SuppressWarnings("deprecation")
private static <T extends ParticleOptions> ParticleType<T> register(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"textures": [
"portalcubed:energy_spark"
]
}
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 b9f4023

Please sign in to comment.