Reimplement particle connections

This commit is contained in:
Sollace 2020-04-24 22:40:02 +02:00
parent f202e1881f
commit fd74bb47eb
6 changed files with 77 additions and 53 deletions

View file

@ -16,9 +16,10 @@ import net.minecraft.world.World;
import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.client.render.model.SphereModel;
import com.minelittlepony.unicopia.magic.Caster; 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 red;
protected float green; protected float green;
@ -57,7 +58,7 @@ public class SphereParticle extends Particle implements AttachableParticle {
} }
@Override @Override
public void attachTo(Caster<?> caster) { public void attach(Caster<?> caster) {
setMaxAge(50000); setMaxAge(50000);
this.caster = caster; this.caster = caster;
} }
@ -68,16 +69,11 @@ public class SphereParticle extends Particle implements AttachableParticle {
radius = (float)value; radius = (float)value;
} }
if (key == 1) { if (key == 1) {
red = (int)value/255F; int tint = (int)value;
} red = Color.r(tint);
if (key == 2) { green = Color.g(tint);
green = (int)value/255F; blue = Color.b(tint);
} alpha = Color.a(tint);
if (key == 3) {
blue = (int)value/255F;
}
if (key == 4) {
alpha = (float)value;
} }
} }

View file

@ -9,7 +9,7 @@ import com.minelittlepony.unicopia.magic.Affinity;
import com.minelittlepony.unicopia.magic.AttachedMagicEffect; import com.minelittlepony.unicopia.magic.AttachedMagicEffect;
import com.minelittlepony.unicopia.magic.Caster; import com.minelittlepony.unicopia.magic.Caster;
import com.minelittlepony.unicopia.particles.MagicParticleEffect; 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.particles.UParticles;
import com.minelittlepony.unicopia.util.projectile.ProjectileUtil; import com.minelittlepony.unicopia.util.projectile.ProjectileUtil;
import com.minelittlepony.unicopia.util.shape.Sphere; 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 { public class ShieldSpell extends AbstractSpell.RangedAreaSpell implements AttachedMagicEffect {
private final ParticleConnection particlEffect = new ParticleConnection(); private final ParticleHandle particlEffect = new ParticleHandle();
@Override @Override
public String getName() { public String getName() {
@ -52,10 +52,13 @@ public class ShieldSpell extends AbstractSpell.RangedAreaSpell implements Attach
source.addParticle(new MagicParticleEffect(getTint()), pos, Vec3d.ZERO); source.addParticle(new MagicParticleEffect(getTint()), pos, Vec3d.ZERO);
}); });
particlEffect.ifMissing(source, () -> { particlEffect.ifAbsent(source, spawner -> {
source.addParticle(UParticles.SPHERE, source.getOriginVector(), Vec3d.ZERO); spawner.addParticle(UParticles.SPHERE, source.getOriginVector(), Vec3d.ZERO);
return null; // XXX: Attachables }).ifPresent(p -> {
}).ifPresent(p -> p.setAttribute(0, radius)); // 1, getTint(), 10 p.attach(source);
p.setAttribute(0, radius);
p.setAttribute(1, getTint());
});
} }
@Override @Override

View file

@ -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<AttachableParticle> particleEffect = Optional.empty();
public Optional<AttachableParticle> ifMissing(Caster<?> source, Supplier<AttachableParticle> 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);
}
}

View file

@ -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<Attachment> particleEffect = Optional.empty();
public Optional<Attachment> ifAbsent(ParticleSource source, Consumer<ParticleSpawner> 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);
}
}

View file

@ -9,7 +9,7 @@ import net.minecraft.particle.ParticleEffect;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
public interface ParticleSource { public interface ParticleSource extends ParticleSpawner {
/** /**
* gets the minecraft world * gets the minecraft world
@ -35,6 +35,7 @@ public interface ParticleSource {
.forEach(particleSpawner); .forEach(particleSpawner);
} }
@Override
default void addParticle(ParticleEffect effect, Vec3d position, Vec3d velocity) { default void addParticle(ParticleEffect effect, Vec3d position, Vec3d velocity) {
getWorld().addParticle(effect, position.x, position.y, position.z, velocity.x, velocity.y, velocity.z); getWorld().addParticle(effect, position.x, position.y, position.z, velocity.x, velocity.y, velocity.z);
} }

View file

@ -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);
}