From fd74bb47ebc5e93857aa3815ee338ec419ef9e65 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 24 Apr 2020 22:40:02 +0200 Subject: [PATCH] Reimplement particle connections --- .../client/particle/SphereParticle.java | 22 ++++----- .../unicopia/magic/spell/ShieldSpell.java | 15 +++--- .../particles/ParticleConnection.java | 33 ------------- .../unicopia/particles/ParticleHandle.java | 49 +++++++++++++++++++ .../unicopia/particles/ParticleSource.java | 3 +- .../unicopia/particles/ParticleSpawner.java | 8 +++ 6 files changed, 77 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/com/minelittlepony/unicopia/particles/ParticleConnection.java create mode 100644 src/main/java/com/minelittlepony/unicopia/particles/ParticleHandle.java create mode 100644 src/main/java/com/minelittlepony/unicopia/particles/ParticleSpawner.java diff --git a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java b/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java index 890f0be4..2c267871 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java +++ b/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java @@ -16,9 +16,10 @@ import net.minecraft.world.World; import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.magic.Caster; -import com.minelittlepony.unicopia.particles.ParticleConnection.AttachableParticle; +import com.minelittlepony.unicopia.particles.ParticleHandle.Attachment; +import com.minelittlepony.util.Color; -public class SphereParticle extends Particle implements AttachableParticle { +public class SphereParticle extends Particle implements Attachment { protected float red; protected float green; @@ -57,7 +58,7 @@ public class SphereParticle extends Particle implements AttachableParticle { } @Override - public void attachTo(Caster caster) { + public void attach(Caster caster) { setMaxAge(50000); this.caster = caster; } @@ -68,16 +69,11 @@ public class SphereParticle extends Particle implements AttachableParticle { radius = (float)value; } if (key == 1) { - red = (int)value/255F; - } - if (key == 2) { - green = (int)value/255F; - } - if (key == 3) { - blue = (int)value/255F; - } - if (key == 4) { - alpha = (float)value; + int tint = (int)value; + red = Color.r(tint); + green = Color.g(tint); + blue = Color.b(tint); + alpha = Color.a(tint); } } diff --git a/src/main/java/com/minelittlepony/unicopia/magic/spell/ShieldSpell.java b/src/main/java/com/minelittlepony/unicopia/magic/spell/ShieldSpell.java index 9dc76338..34a963ae 100644 --- a/src/main/java/com/minelittlepony/unicopia/magic/spell/ShieldSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/magic/spell/ShieldSpell.java @@ -9,7 +9,7 @@ import com.minelittlepony.unicopia.magic.Affinity; import com.minelittlepony.unicopia.magic.AttachedMagicEffect; import com.minelittlepony.unicopia.magic.Caster; import com.minelittlepony.unicopia.particles.MagicParticleEffect; -import com.minelittlepony.unicopia.particles.ParticleConnection; +import com.minelittlepony.unicopia.particles.ParticleHandle; import com.minelittlepony.unicopia.particles.UParticles; import com.minelittlepony.unicopia.util.projectile.ProjectileUtil; import com.minelittlepony.unicopia.util.shape.Sphere; @@ -22,7 +22,7 @@ import net.minecraft.util.math.Vec3d; public class ShieldSpell extends AbstractSpell.RangedAreaSpell implements AttachedMagicEffect { - private final ParticleConnection particlEffect = new ParticleConnection(); + private final ParticleHandle particlEffect = new ParticleHandle(); @Override public String getName() { @@ -52,10 +52,13 @@ public class ShieldSpell extends AbstractSpell.RangedAreaSpell implements Attach source.addParticle(new MagicParticleEffect(getTint()), pos, Vec3d.ZERO); }); - particlEffect.ifMissing(source, () -> { - source.addParticle(UParticles.SPHERE, source.getOriginVector(), Vec3d.ZERO); - return null; // XXX: Attachables - }).ifPresent(p -> p.setAttribute(0, radius)); // 1, getTint(), 10 + particlEffect.ifAbsent(source, spawner -> { + spawner.addParticle(UParticles.SPHERE, source.getOriginVector(), Vec3d.ZERO); + }).ifPresent(p -> { + p.attach(source); + p.setAttribute(0, radius); + p.setAttribute(1, getTint()); + }); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/particles/ParticleConnection.java b/src/main/java/com/minelittlepony/unicopia/particles/ParticleConnection.java deleted file mode 100644 index 1ed57c0c..00000000 --- a/src/main/java/com/minelittlepony/unicopia/particles/ParticleConnection.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.minelittlepony.unicopia.particles; - -import java.util.Optional; -import java.util.function.Supplier; - -import com.minelittlepony.unicopia.magic.Caster; - -/** - * A connection class for updating and persisting an attached particle effect. - */ -public class ParticleConnection { - - private Optional particleEffect = Optional.empty(); - - public Optional ifMissing(Caster source, Supplier constructor) { - particleEffect.filter(AttachableParticle::isStillAlive).orElseGet(() -> { - particleEffect = Optional.ofNullable(constructor.get()); - particleEffect.ifPresent(p -> p.attachTo(source)); - return null; - }); - - return particleEffect; - } - - public interface AttachableParticle { - - boolean isStillAlive(); - - void attachTo(Caster caster); - - void setAttribute(int key, Object value); - } -} diff --git a/src/main/java/com/minelittlepony/unicopia/particles/ParticleHandle.java b/src/main/java/com/minelittlepony/unicopia/particles/ParticleHandle.java new file mode 100644 index 00000000..fbec074a --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/particles/ParticleHandle.java @@ -0,0 +1,49 @@ +package com.minelittlepony.unicopia.particles; + +import java.util.Optional; +import java.util.function.Consumer; +import com.minelittlepony.unicopia.magic.Caster; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.particle.Particle; +import net.minecraft.particle.ParticleEffect; +import net.minecraft.util.math.Vec3d; + +/** + * A connection class for updating and persisting an attached particle effect. + */ +public class ParticleHandle { + + private Optional particleEffect = Optional.empty(); + + public Optional ifAbsent(ParticleSource source, Consumer constructor) { + particleEffect.filter(Attachment::isStillAlive).orElseGet(() -> { + if (source.getWorld().isClient) { + constructor.accept(this::addParticle); + } + return null; + }); + + return particleEffect; + } + + @Environment(EnvType.CLIENT) + private void addParticle(ParticleEffect effect, Vec3d pos, Vec3d vel) { + Particle p = MinecraftClient.getInstance().particleManager.addParticle(effect, pos.x, pos.y, pos.z, vel.x, vel.y, vel.z); + + if (p instanceof Attachment) { + particleEffect = Optional.ofNullable((Attachment)p); + } + } + + public interface Attachment { + + boolean isStillAlive(); + + void attach(Caster caster); + + void setAttribute(int key, Object value); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/particles/ParticleSource.java b/src/main/java/com/minelittlepony/unicopia/particles/ParticleSource.java index fc7c8765..365eee6b 100644 --- a/src/main/java/com/minelittlepony/unicopia/particles/ParticleSource.java +++ b/src/main/java/com/minelittlepony/unicopia/particles/ParticleSource.java @@ -9,7 +9,7 @@ import net.minecraft.particle.ParticleEffect; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -public interface ParticleSource { +public interface ParticleSource extends ParticleSpawner { /** * gets the minecraft world @@ -35,6 +35,7 @@ public interface ParticleSource { .forEach(particleSpawner); } + @Override default void addParticle(ParticleEffect effect, Vec3d position, Vec3d velocity) { getWorld().addParticle(effect, position.x, position.y, position.z, velocity.x, velocity.y, velocity.z); } diff --git a/src/main/java/com/minelittlepony/unicopia/particles/ParticleSpawner.java b/src/main/java/com/minelittlepony/unicopia/particles/ParticleSpawner.java new file mode 100644 index 00000000..a6e1c6ea --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/particles/ParticleSpawner.java @@ -0,0 +1,8 @@ +package com.minelittlepony.unicopia.particles; + +import net.minecraft.particle.ParticleEffect; +import net.minecraft.util.math.Vec3d; + +public interface ParticleSpawner { + void addParticle(ParticleEffect effect, Vec3d position, Vec3d velocity); +}