Files
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

164 lines
3.4 KiB
Plaintext

FlowFieldRunner runner = new FlowFieldRunner();
void setup()
{
//size(1800, 1000);
size(600, 600);
sample00();
}
void draw()
{
runner.update();
}
void keyPressed() {
println(" key " + key + " pressed");
if (key == 's')
{
String fn = "captures/" + runner.sampleName + ".png";
save(fn);
}
}
void sample00() {
background(color(219, 237, 243));
NoiseGenerator ngen = new NoiseGenerator(0.01, 0.5);
ngen.setNoiseOctaveParam(3, 0.3);
runner.sampleName = "sample00";
runner.addLayer(new ParticlesLayer(10000,600, color(40, 49, 73), ngen) {
public void draw(Particle p)
{
strokeWeight(1);
stroke(p.c, 5);
line(p.prev.x, p.prev.y, p.pos.x, p.pos.y);
}
});
}
class FlowFieldRunner
{
ArrayList<ParticlesLayer> layers;
ParticlesLayer currentLayer;
int currentLayerIndex = -1;
boolean start = true;
String sampleName = "<none>";
FlowFieldRunner()
{
layers = new ArrayList<ParticlesLayer>();
}
void addLayer(ParticlesLayer pl)
{
if (currentLayerIndex == -1)
{
currentLayerIndex = 0;
currentLayer = pl;
}
layers.add(pl);
}
void update()
{
if (start)
{
this.start = false;
println("Layer " + (currentLayerIndex + 1) + " started");
currentLayer.init();
}
if (currentLayer.dead)
{
if (currentLayerIndex < layers.size() - 1)
{
println("Layer " + (currentLayerIndex + 1) + " done");
currentLayerIndex++;
println("Layer " + (currentLayerIndex + 1) + " started");
currentLayer = layers.get(currentLayerIndex);
currentLayer.init();
//currentLayer.update();
}
else {
println("Layer " + (currentLayerIndex + 1) + " done");
noLoop();
println("All layers drawn :-)");
}
}
else {
// updatecurrent layer
currentLayer.update();
}
}
}
class ParticlesLayer {
boolean dead = false;
ArrayList<Particle> particles;
int deadCount;
int aliveCount;
int lifeTime;
NoiseGenerator noiseGenerator;
color c;
ParticlesLayer(int count, int lifeTime, color c, NoiseGenerator noiseGenerator)
{
this.deadCount = 0;
this.aliveCount = count;
this.noiseGenerator = noiseGenerator;
this.lifeTime = lifeTime;
this.c = c;
particles = new ArrayList<Particle>();
for( int x = 0; x < count; x++)
{
particles.add( new Particle( new PVector(-100+random(width+200), -100+random(height+200)) , this.lifeTime, this.c, this.noiseGenerator));
}
}
void init()
{
this.noiseGenerator.init();
}
void update()
{
if(dead) return;
for( int x = 0; x < aliveCount; x++)
{
Particle p = particles.get(x);
if( !p.dead)
{
p.update();
this.draw(p);
if( p.dead)
{
deadCount++;
}
}
}
if( deadCount == aliveCount)
{
//println("All particles of this layer are dead");
dead = true;
}
noiseGenerator.update();
}
void draw( Particle p)
{
// to be override to customize drawing
}
}