mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 23:27:59 +01:00
Reimplement particle connections
This commit is contained in:
parent
f202e1881f
commit
fd74bb47eb
6 changed files with 77 additions and 53 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue