Skip to content

Commit

Permalink
projection
Browse files Browse the repository at this point in the history
  • Loading branch information
RadonCoding committed Nov 3, 2023
1 parent ed19828 commit a50b2b0
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package radon.jujutsu_kaisen.ability.projection_sorcery;

import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.Nullable;
import radon.jujutsu_kaisen.JujutsuKaisen;
import radon.jujutsu_kaisen.ability.JJKAbilities;
import radon.jujutsu_kaisen.ability.MenuType;
import radon.jujutsu_kaisen.ability.base.Ability;
import radon.jujutsu_kaisen.capability.data.ISorcererData;
import radon.jujutsu_kaisen.capability.data.SorcererDataHandler;
import radon.jujutsu_kaisen.client.particle.MirageParticle;
import radon.jujutsu_kaisen.client.particle.ProjectionParticle;
import radon.jujutsu_kaisen.effect.JJKEffects;
import radon.jujutsu_kaisen.entity.effect.ProjectionFrameEntity;
import radon.jujutsu_kaisen.network.PacketHandler;
Expand Down Expand Up @@ -63,15 +57,15 @@ public void run(LivingEntity owner) {

Vec3 start = owner.getEyePosition();
Vec3 look = owner.getLookAngle();
Vec3 end = start.add(look.scale(this.getCharge(owner) * 2));
Vec3 end = start.add(look.scale(this.getCharge(owner) * 4));
HitResult result = HelperMethods.getHitResult(owner, start, end);

Vec3 pos = result.getType() == HitResult.Type.MISS ? end : result.getLocation();
Vec3 pos = result.getType() == HitResult.Type.MISS ? end : result.getLocation().add(0.0D, owner.getBbHeight(), 0.0D);

Vec3 frame = pos.subtract(0.0D, owner.getBbHeight(), 0.0D);
cap.addFrame(frame);

owner.level().addParticle(new MirageParticle.MirageParticleOptions(owner.getId()), frame.x(), frame.y(), frame.z(),
owner.level().addParticle(new ProjectionParticle.ProjectionParticleOptions(owner.getId()), frame.x(), frame.y(), frame.z(),
0.0D, 0.0D, 0.0D);
}

Expand All @@ -95,6 +89,11 @@ public void onStart(LivingEntity owner) {

}

private static boolean isGrounded(Level level, BlockPos pos) {
BlockHitResult hit = level.clip(new ClipContext(pos.getCenter(), pos.below(8).getCenter(), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, null));
return hit.getType() == HitResult.Type.BLOCK;
}

@Override
public void onRelease(LivingEntity owner) {
ISorcererData cap = owner.getCapability(SorcererDataHandler.INSTANCE).resolve().orElseThrow();
Expand All @@ -107,14 +106,17 @@ public void onRelease(LivingEntity owner) {
int delay = 0;

AtomicBoolean cancelled = new AtomicBoolean();
AtomicReference<Vec3> previous = new AtomicReference<>();

for (Vec3 frame : cap.getFrames()) {
cap.delayTickEvent(() -> {
if (cancelled.get()) return;

owner.walkAnimation.setSpeed(24.0F);
owner.walkAnimation.setSpeed(2.0F);

if (frame.distanceTo(owner.position()) >= 24.0D) {
boolean isOnGround = isGrounded(owner.level(), owner.blockPosition()) || (previous.get() != null && isGrounded(owner.level(), BlockPos.containing(previous.get())));

if ((!isOnGround && !owner.level().getBlockState(BlockPos.containing(frame)).canOcclude()) || frame.distanceTo(owner.position()) >= 24.0D) {
cancelled.set(true);

owner.level().addFreshEntity(new ProjectionFrameEntity(owner, owner, Ability.getPower(JJKAbilities.TWENTY_FOUR_FRAME_RULE.get(), owner)));
Expand All @@ -127,6 +129,8 @@ public void onRelease(LivingEntity owner) {
}
owner.teleportTo(frame.x(), frame.y(), frame.z());
cap.removeFrame(frame);

previous.set(frame);
}, delay++);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public static void onRegisterParticleProviders(RegisterParticleProvidersEvent ev
event.registerSpriteSet(JJKParticles.TRAVEL.get(), TravelParticle.Provider::new);
event.registerSpriteSet(JJKParticles.CURSED_ENERGY.get(), CursedEnergyParticle.Provider::new);
event.registerSpriteSet(JJKParticles.MIRAGE.get(), MirageParticle.Provider::new);
event.registerSpriteSet(JJKParticles.PROJECTION.get(), ProjectionParticle.Provider::new);
event.registerSpriteSet(JJKParticles.LIGHTNING.get(), LightningParticle.Provider::new);
event.registerSpriteSet(JJKParticles.CURSED_SPEECH.get(), CursedSpeechParticle.Provider::new);
event.registerSpriteSet(JJKParticles.BLOOD.get(), BloodParticle.Provider::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,4 @@ public void end(Tesselator tesselator) {
tesselator.end();
}
};

public static ParticleRenderType BLUR = new ParticleRenderType() {
@Override
public void begin(BufferBuilder buffer, @NotNull TextureManager manager) {
RenderSystem.depthMask(true);
RenderSystem.setShaderTexture(0, TextureAtlas.LOCATION_PARTICLES);
//manager.getTexture(TextureAtlas.LOCATION_PARTICLES).setBlurMipmap(true, false);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.PARTICLE);
}

@Override
public void end(Tesselator tesselator) {
tesselator.end();
//Minecraft.getInstance().textureManager.getTexture(TextureAtlas.LOCATION_PARTICLES).restoreLastBlurMipmap();
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public Codec<MirageParticle.MirageParticleOptions> codec() {
return null;
}
});
public static RegistryObject<ParticleType<ProjectionParticle.ProjectionParticleOptions>> PROJECTION = PARTICLES.register("projection", () ->
new ParticleType<>(false, ProjectionParticle.ProjectionParticleOptions.DESERIALIZER) {
@Override
public Codec<ProjectionParticle.ProjectionParticleOptions> codec() {
return null;
}
});
public static RegistryObject<SimpleParticleType> CURSED_SPEECH = PARTICLES.register("cursed_speech", () ->
new SimpleParticleType(true));
public static RegistryObject<SimpleParticleType> BLOOD = PARTICLES.register("blood", () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import net.minecraft.client.particle.*;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import radon.jujutsu_kaisen.client.MixinData;
Expand Down Expand Up @@ -73,7 +75,7 @@ public void tick() {

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

@Override
Expand Down Expand Up @@ -107,10 +109,16 @@ public void render(@NotNull VertexConsumer pBuffer, @NotNull Camera pRenderInfo,
this.entity.setYRot(this.yRot);
this.entity.yRotO = this.yRot0;

RenderSystem.depthMask(false);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);

EntityRenderDispatcher manager = Minecraft.getInstance().getEntityRenderDispatcher();
manager.render(this.entity, this.x - pRenderInfo.getPosition().x(), this.y - pRenderInfo.getPosition().y(), this.z - pRenderInfo.getPosition().z(), 0.0F, pPartialTicks, stack, buffer, 15728880);
EntityRenderer<? super Entity> renderer = manager.getRenderer(this.entity);

Vec3 offset = renderer.getRenderOffset(this.entity, pPartialTicks);
stack.translate((this.x - pRenderInfo.getPosition().x()) + offset.x(), (this.y - pRenderInfo.getPosition().y()) + offset.y(), (this.z - pRenderInfo.getPosition().z()) + offset.z());

renderer.render(this.entity, 0.0F, pPartialTicks, stack, buffer, 15728880);

RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package radon.jujutsu_kaisen.client.particle;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.*;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import radon.jujutsu_kaisen.client.MixinData;

import java.util.Locale;

public class ProjectionParticle<T extends ProjectionParticle.ProjectionParticleOptions> extends TextureSheetParticle {
private final int entityId;

@Nullable
private Entity entity;

private float yRot;
private float yRot0;
private float yHeadRot;
private float yHeadRot0;

private float position;

protected ProjectionParticle(ClientLevel pLevel, double pX, double pY, double pZ, double pXSpeed, double pYSpeed, double pZSpeed, T options) {
super(pLevel, pX, pY, pZ);

this.lifetime = 10;

this.xd = pXSpeed;
this.yd = pYSpeed;
this.zd = pZSpeed;

this.entityId = options.entityId();
}

@Override
public void tick() {
super.tick();

this.alpha = 1.0F - ((float) this.age / this.lifetime);

if (this.entity == null) {
this.entity = this.level.getEntity(this.entityId);

if (this.entity == null) return;

this.yRot = this.entity.getYRot();
this.yRot0 = this.entity.yRotO;

if (this.entity instanceof LivingEntity living) {
this.yHeadRot = living.yHeadRot;
this.yHeadRot0 = living.yHeadRotO;

this.position = living.walkAnimation.position();
}
}
}

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

@Override
public void render(@NotNull VertexConsumer pBuffer, @NotNull Camera pRenderInfo, float pPartialTicks) {
if (this.entity != null) {
PoseStack stack = new PoseStack();

MultiBufferSource.BufferSource buffer = Minecraft.getInstance().renderBuffers().bufferSource();

float yRot = this.entity.getYRot();
float yRotO = this.entity.yRotO;

float yHeadRot = 0.0F;
float yHeadRotO = 0.0F;

boolean invisible = this.entity.isInvisible();

MixinData.isCustomWalkAnimationPosition = true;

this.entity.setInvisible(false);

if (this.entity instanceof LivingEntity living) {
yHeadRot = living.yHeadRot;
yHeadRotO = living.yHeadRotO;
living.yHeadRot = this.yHeadRot;
living.yHeadRotO = this.yHeadRot0;

MixinData.walkAnimationPosition = this.position;
}

this.entity.setYRot(this.yRot);
this.entity.yRotO = this.yRot0;

RenderSystem.depthMask(false);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.alpha);

EntityRenderDispatcher manager = Minecraft.getInstance().getEntityRenderDispatcher();
EntityRenderer<? super Entity> renderer = manager.getRenderer(this.entity);

Vec3 offset = renderer.getRenderOffset(this.entity, pPartialTicks);
stack.translate((this.x - pRenderInfo.getPosition().x()) + offset.x(), (this.y - pRenderInfo.getPosition().y()) + offset.y(), (this.z - pRenderInfo.getPosition().z()) + offset.z());

renderer.render(this.entity, 0.0F, pPartialTicks, stack, buffer, 15728880);

RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);

this.entity.yRotO = yRotO;
this.entity.setYRot(yRot);

if (this.entity instanceof LivingEntity living) {
living.yHeadRotO = yHeadRotO;
living.yHeadRot = yHeadRot;
}

this.entity.setInvisible(invisible);

MixinData.isCustomWalkAnimationPosition = false;

buffer.endBatch();
}
}

public record ProjectionParticleOptions(int entityId) implements ParticleOptions {
public static Deserializer<ProjectionParticleOptions> DESERIALIZER = new Deserializer<>() {
public @NotNull ProjectionParticle.ProjectionParticleOptions fromCommand(@NotNull ParticleType<ProjectionParticleOptions> type, @NotNull StringReader reader) throws CommandSyntaxException {
return new ProjectionParticleOptions(reader.readInt());
}

public @NotNull ProjectionParticle.ProjectionParticleOptions fromNetwork(@NotNull ParticleType<ProjectionParticleOptions> type, @NotNull FriendlyByteBuf buf) {
return new ProjectionParticleOptions(buf.readInt());
}
};

@Override
public @NotNull ParticleType<?> getType() {
return JJKParticles.PROJECTION.get();
}

@Override
public void writeToNetwork(FriendlyByteBuf buf) {
buf.writeInt(this.entityId);
}

@Override
public @NotNull String writeToString() {
return String.format(Locale.ROOT, "%s %d", BuiltInRegistries.PARTICLE_TYPE.getKey(this.getType()), this.entityId);
}
}

public static class Provider implements ParticleProvider<ProjectionParticleOptions> {
public Provider(SpriteSet ignored) {}

public Particle createParticle(@NotNull ProjectionParticle.ProjectionParticleOptions pType, @NotNull ClientLevel pLevel, double pX, double pY, double pZ,
double pXSpeed, double pYSpeed, double pZSpeed) {
return new ProjectionParticle<>(pLevel, pX, pY, pZ, pXSpeed, pYSpeed, pZSpeed, pType);
}
}
}

0 comments on commit a50b2b0

Please sign in to comment.