Call onTransitionEnd() after active
[SugarCubes.git] / DanHorwitz.pde
CommitLineData
a41f334c
MS
1//----------------------------------------------------------------------------------------------------------------------------------
2public class Pong extends DPat {
e5308763 3 SinLFO x,y,z,dx,dy,dz;
a41f334c
MS
4 float cRad; DParam pSize;
5 Pick pChoose;
6 xyz v = new xyz(), vMir = new xyz();
7
8 Pong(GLucose glucose) {
9 super(glucose);
10 cRad = mMax.x/10;
11 addModulator(dx = new SinLFO(6000, 500, 30000 )).trigger();
12 addModulator(dy = new SinLFO(3000, 500, 22472 )).trigger();
13 addModulator(dz = new SinLFO(1000, 500, 18420 )).trigger();
14 addModulator(x = new SinLFO(cRad, mMax.x - cRad, 0)).trigger(); x.modulateDurationBy(dx);
15 addModulator(y = new SinLFO(cRad, mMax.y - cRad, 0)).trigger(); y.modulateDurationBy(dy);
16 addModulator(z = new SinLFO(cRad, mMax.z - cRad, 0)).trigger(); z.modulateDurationBy(dz);
17 pSize = addParam ("Size" , 0.4 );
18 pChoose = addPick ("Animiation" , 0, 2, new String[] {"Pong", "Ball", "Cone"} );
19 }
20
21 void StartRun(double deltaMs) { cRad = mMax.x*pSize.Val()/6; }
22 color CalcPoint(xyz p) {
23 v.set(x.getValuef(), y.getValuef(), z.getValuef());
24 v.z=0;p.z=0;// ignore z dimension
25 switch(pChoose.Cur()) {
26 case 0: vMir.set(mMax); vMir.subtract(p);
e5308763 27 return lx.hsb(lxh(),100,c1c(1 - min(v.distance(p), v.distance(vMir))*.5/cRad)); // balls
28 case 1: return lx.hsb(lxh(),100,c1c(1 - v.distance(p)*.5/cRad)); // ball
a41f334c 29 case 2: vMir.set(mMax.x/2,0,mMax.z/2);
e5308763 30 return lx.hsb(lxh(),100,c1c(1 - CalcCone(p,v,vMir) * max(.02,.45-pSize.Val()))); // spot
a41f334c 31 }
fb83f322 32 return lx.hsb(0,0,0);
a41f334c
MS
33 }
34}
35//----------------------------------------------------------------------------------------------------------------------------------
36public class NDat {
e5308763 37 float xz, yz, zz, hue, speed, angle, den;
a41f334c
MS
38 float xoff,yoff,zoff;
39 float sinAngle, cosAngle;
40 boolean isActive;
41 NDat () { isActive=false; }
42 boolean Active() { return isActive; }
e5308763 43 void set (float _hue, float _xz, float _yz, float _zz, float _den, float _speed, float _angle) {
a41f334c 44 isActive = true;
e5308763 45 hue=_hue; xz=_xz; yz=_yz; zz =_zz; den=_den; speed=_speed; angle=_angle;
a41f334c
MS
46 xoff = random(100e3); yoff = random(100e3); zoff = random(100e3);
47 }
48}
49
50public class Noise extends DPat
51{
52 int CurAnim, iSymm;
53 int XSym=1,YSym=2,RadSym=3;
54 float zTime , zTheta=0, zSin, zCos, rtime, ttime, transAdd;
e5308763 55 DParam pSpeed , pDensity, pSharp;
a41f334c
MS
56 Pick pChoose, pSymm;
57 int _ND = 4;
58 NDat N[] = new NDat[_ND];
59
60 Noise(GLucose glucose) {
61 super(glucose);
62 pSpeed = addParam("Fast" , .55);
63 pDensity = addParam("Dens" , .5);
e5308763 64 pSharp = addParam("Shrp" , 0);
a41f334c
MS
65 pSymm = addPick("Symmetry" , 0, 3, new String[] {"None", "X", "Y", "Radial"} );
66 pChoose = addPick("Animation", 6, 7, new String[] {"Drip", "Cloud", "Rain", "Fire", "Machine", "Spark","VWave", "Wave"} );
67 for (int i=0; i<_ND; i++) N[i] = new NDat();
68 }
69
70 void StartPattern() { zTime = random(500); zTheta=0; rtime = 0; ttime = 0; transAdd=0; }
71 void StartRun(double deltaMs) {
72 zTime += deltaMs*(pSpeed.Val()-.5)*.002 ;
73 zTheta += deltaMs*(pSpin .Val()-.5)*.01 ;
74 rtime += deltaMs;
75 iSymm = pSymm.Cur();
76 transAdd = 1*(1 - constrain(rtime - ttime,0,1000)/1000);
77 zSin = sin(zTheta);
78 zCos = cos(zTheta);
79
80 if (pChoose.Cur() != CurAnim) {
81 CurAnim = pChoose.Cur(); ttime = rtime;
82 pSpin .reset(); zTheta = 0;
83 pDensity .reset(); pSpeed .reset();
84 for (int i=0; i<_ND; i++) { N[i].isActive = false; }
85
86 switch(CurAnim) {
e5308763 87 // hue xz yz zz den mph angle
88 case 0: N[0].set(0 ,75 ,75 ,150,45 ,3 ,0 ); pSharp.set(1 ); break; // drip
89 case 1: N[0].set(0 ,100,100,200,45 ,3 ,180); pSharp.set(0 ); break; // clouds
90 case 2: N[0].set(0 ,2 ,400,2 ,20 ,3 ,0 ); pSharp.set(.5); break; // rain
91 case 3: N[0].set(40 ,100,100,200,10 ,1 ,180);
92 N[1].set(0 ,100,100,200,10 ,5 ,180); pSharp.set(0 ); break; // fire 1
93 case 4: N[0].set(0 ,40 ,40 ,40 ,15 ,2.5,180);
94 N[1].set(20 ,40 ,40 ,40 ,15 ,4 ,0 );
95 N[2].set(40 ,40 ,40 ,40 ,15 ,2 ,90 );
96 N[3].set(60 ,40 ,40 ,40 ,15 ,3 ,-90); pSharp.set(.5); break; // machine
97 case 5: N[0].set(0 ,400,100,2 ,15 ,3 ,90 );
98 N[1].set(20 ,400,100,2 ,15 ,2.5,0 );
99 N[2].set(40 ,100,100,2 ,15 ,2 ,180);
100 N[3].set(60 ,100,100,2 ,15 ,1.5,270); pSharp.set(.5); break; // spark
a41f334c 101 }
a41f334c
MS
102 }
103
104 for (int i=0; i<_ND; i++) if (N[i].Active()) {
105 N[i].sinAngle = sin(radians(N[i].angle));
106 N[i].cosAngle = cos(radians(N[i].angle));
107 }
108 }
109
110 color CalcPoint(xyz P) {
111 color c = 0;
112 P.RotateZ(mCtr, zSin, zCos);
e5308763 113
a41f334c
MS
114 if (CurAnim == 6 || CurAnim == 7) {
115 P.setNorm();
e5308763 116 return lx.hsb(lxh(),100, 100 * (
a41f334c
MS
117 constrain(1-50*(1-pDensity.Val())*abs(P.y-sin(zTime*10 + P.x*(300))*.5 - .5),0,1) +
118 (CurAnim == 7 ? constrain(1-50*(1-pDensity.Val())*abs(P.x-sin(zTime*10 + P.y*(300))*.5 - .5),0,1) : 0))
119 );
120 }
e5308763 121
a41f334c
MS
122 if (iSymm == XSym && P.x > mMax.x/2) P.x = mMax.x-P.x;
123 if (iSymm == YSym && P.y > mMax.y/2) P.y = mMax.y-P.y;
124
125 for (int i=0;i<_ND; i++) if (N[i].Active()) {
126 NDat n = N[i];
127 float zx = zTime * n.speed * n.sinAngle,
128 zy = zTime * n.speed * n.cosAngle;
e5308763 129
a41f334c
MS
130 float b = (iSymm==RadSym ? noise(zTime*n.speed+n.xoff-Dist(P,mCtr)/n.xz)
131 : noise(P.x/n.xz+zx+n.xoff,P.y/n.yz+zy+n.yoff,P.z/n.zz+n.zoff))
132 *1.8;
133
134 b += n.den/100 -.4 + pDensity.Val() -1;
135 b += transAdd;
e5308763 136 c = blendColor(c,lx.hsb(lxh()+n.hue,100,c1c(b)),ADD);
a41f334c
MS
137 }
138 return c;
139 }
140}
141//----------------------------------------------------------------------------------------------------------------------------------
142public class Play extends DPat
143{
144 public class rAngle {
145 float prvA, dstA, c;
146 float prvR, dstR, r;
147 float _cos, _sin, x, y;
148 float fixAngle (float a, float b) { return a<b ?
149 (abs(a-b) > abs(a+2*PI-b) ? a : a+2*PI) :
150 (abs(a-b) > abs(a-2*PI-b) ? a : a-2*PI) ; }
151 float getX(float r) { return mCtr.x + _cos*r; }
152 float getY(float r) { return mCtr.y + _sin*r; }
153 void move() { c = interp(t,prvA,dstA);
154 r = interp(t,prvR,dstR);
155 _cos = cos(c); _sin = sin(c);
156 x = getX(r); y = getY(r); }
157 void set() { prvA = dstA; dstA = random(2*PI); prvA = fixAngle(prvA, dstA);
158 prvR = dstR; dstR = random(mCtr.y); }
159 }
160
161 int nBeats = 0;
162 DParam pAmp, pRadius, pBounce;
163
164 float t,amp,rad,bnc;
165 float zTheta=0;
166 ArrayList<rWave> waves = new ArrayList<rWave>(10);
167
e5308763 168 rAngle a1 = new rAngle(), a2 = new rAngle(),
169 a3 = new rAngle(), a4 = new rAngle();
170 xyz cPrev = new xyz(), cRand = new xyz(),
171 cMid = new xyz(), V = new xyz(),
172 Theta = new xyz(), TSin = new xyz(),
173 TCos = new xyz(), cMidNorm = new xyz(),
a41f334c
MS
174 Pn = new xyz();
175 float LastBeat=3, LastMeasure=3;
176 int CurRandTempo = 1, CurRandTPat = 1;
177
178 Pick pTimePattern, pTempoMult, pShape;
179 int RandCube;
180
181 Play(GLucose glucose) {
182 super(glucose);
183 pRadius = addParam("Rad" , .1 );
184 pBounce = addParam("Bnc" , .2 );
185 pAmp = addParam("Amp" , .2 );
41f26e8f 186 pTempoMult = addPick ("TMult" , 5 , 5 , new String[] {"1x", "2x", "4x", "8x", "16x", "Rand" } );
187 pTimePattern= addPick ("TPat" , 7 , 7 , new String[] {"Bounce", "Sin", "Roll", "Quant", "Accel", "Deccel", "Slide", "Rand"} );
188 pShape = addPick ("Shape" , 7 , 15 , new String[] {"Line", "Tap", "V", "RandV",
a41f334c
MS
189 "Pyramid", "Wings", "W2", "Clock",
190 "Triangle", "Quad", "Sphere", "Cone",
191 "Noise", "Wave", "?", "?"} );
192 }
193
194 public class rWave {
195 float v0, a0, x0, t,damp,a;
196 boolean bDone=false;
197 final float len=8;
198 rWave(float _x0, float _a0, float _v0, float _damp) { x0=_x0*len; a0=_a0; v0=_v0; t=0; damp = _damp; }
199 void move(double deltaMs) {
200 t += deltaMs*.001;
201 if (t>4) bDone=true;
202 }
203 float val(float _x) {
204 _x*=len;
205 float dist = t*v0 - abs(_x-x0);
206 if (dist<0) { a=1; return 0; }
207 a = a0*exp(-dist*damp) * exp(-abs(_x-x0)/(.2*len)); // * max(0,1-t/dur)
208 return -a*sin(dist);
209 }
210 }
211
212 void StartPattern() { zTheta=0; }
935819e6 213
214 int KeyPressed = -1;
215 boolean noteOn(Note note) {
216 int row = note.getPitch(), col = note.getChannel();
217 if (row == 57) {KeyPressed = col; return true; }
218 return super.noteOn(note);
219 }
220
a41f334c
MS
221 void StartRun(double deltaMs) {
222 t = lx.tempo.rampf();
223 amp = pAmp.Val();
224 rad = pRadius.getValuef();
225 bnc = pBounce.getValuef();
226 zTheta += deltaMs*(pSpin .Val()-.5)*.01;
227
228 Theta .set(pRotX.Val()*PI*2, pRotY.Val()*PI*2, pRotZ.Val()*PI*2 + zTheta);
229 TSin .set(sin(Theta.x), sin(Theta.y), sin(Theta.z));
230 TCos .set(cos(Theta.x), cos(Theta.y), cos(Theta.z));
231
232 if (t<LastMeasure) {
233 if (random(3) < 1) { CurRandTempo = int(random(4)); if (CurRandTempo == 3) CurRandTempo = int(random(4)); }
234 if (random(3) < 1) { CurRandTPat = pShape.Cur() > 6 ? 2+int(random(5)) : int(random(7)); }
235 } LastMeasure = t;
236
237 int nTempo = pTempoMult .Cur(); if (nTempo == 5) nTempo = CurRandTempo;
238 int nTPat = pTimePattern.Cur(); if (nTPat == 7) nTPat = CurRandTPat ;
239
240 switch (nTempo) {
241 case 0: t = t; break;
242 case 1: t = (t*2. )%1.; break;
243 case 2: t = (t*4. )%1.; break;
244 case 3: t = (t*8. )%1.; break;
245 case 4: t = (t*16.)%1.; break;
246 }
247
248 int i=0; while (i< waves.size()) {
249 rWave w = waves.get(i);
250 w.move(deltaMs); if (w.bDone) waves.remove(i); else i++;
251 }
252
935819e6 253 if ((t<LastBeat && pShape.Cur()!=14) || KeyPressed>-1) {
a41f334c 254 waves.add(new rWave(
935819e6 255 KeyPressed>-1 ? map(KeyPressed,0,7,0,1) : random(1), // location
a41f334c
MS
256 bnc*10, // bounciness
257 7, // velocity
258 2*(1-amp))); // dampiness
935819e6 259 KeyPressed=-1;
a41f334c
MS
260 if (waves.size() > 5) waves.remove(0);
261 }
262
263 if (t<LastBeat) {
264 cPrev.set(cRand); cRand.setRand();
265 a1.set(); a2.set(); a3.set(); a4.set();
266 } LastBeat = t;
267
268 switch (nTPat) {
269 case 0: t = sin(PI*t); break; // bounce
270 case 1: t = norm(sin(2*PI*(t+PI/2)),-1,1); break; // sin
271 case 2: t = t; break; // roll
272 case 3: t = constrain(int(t*8)/7.,0,1); break; // quant
273 case 4: t = t*t*t; break; // accel
274 case 5: t = sin(PI*t*.5); break; // deccel
275 case 6: t = .5*(1-cos(PI*t)); break; // slide
276 }
277
278 cMid.set (cPrev); cMid.interpolate (t,cRand);
279 cMidNorm.set (cMid); cMidNorm.setNorm();
280 a1.move(); a2.move(); a3.move(); a4.move();
281 }
282
283 color CalcPoint(xyz Px) {
284 if (Theta.x != 0) Px.RotateX(mCtr, TSin.x, TCos.x);
285 if (Theta.y != 0) Px.RotateY(mCtr, TSin.y, TCos.y);
286 if (Theta.z != 0) Px.RotateZ(mCtr, TSin.z, TCos.z);
287
288 Pn.set(Px); Pn.setNorm();
289
290 float mp = min(Pn.x, Pn.z);
291 float yt = map(t,0,1,.5-bnc/2,.5+bnc/2);
292 float r,d;
293
294 switch (pShape.Cur()) {
295 case 0: V.set(Pn.x, yt , Pn.z); break; // bouncing line
296 case 1: V.set(Pn.x, map(cos(PI*t * Pn.x),-1,1,0,1) , Pn.z); break; // top tap
297 case 2: V.set(Pn.x, bnc*map(Pn.x<.5?Pn.x:1-Pn.x,0,.5 ,0,t-.5)+.5, Pn.z); break; // V shape
298 case 3: V.set(Pn.x, Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
299 map(Pn.x,cMidNorm.x,1, yt,.5), Pn.z); break; // Random V shape
300
301 case 4: V.set(Pn.x, .5*(Pn.x < cMidNorm.x ? map(Pn.x,0,cMidNorm.x, .5,yt) :
302 map(Pn.x,cMidNorm.x,1, yt,.5)) +
303 .5*(Pn.z < cMidNorm.z ? map(Pn.z,0,cMidNorm.z, .5,yt) :
304 map(Pn.z,cMidNorm.z,1, yt,.5)), Pn.z); break; // Random Pyramid shape
305
306 case 5: V.set(Pn.x, bnc*map((Pn.x-.5)*(Pn.x-.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
307 case 6: V.set(Pn.x, bnc*map((mp -.5)*(mp -.5),0,.25,0,t-.5)+.5, Pn.z); break; // wings
308
309 case 7: d = min(
310 distToSeg(Px.x, Px.y, a1.getX(70),a1.getY(70), mCtr.x, mCtr.y),
311 distToSeg(Px.x, Px.y, a2.getX(40),a2.getY(40), mCtr.x, mCtr.y));
312 d = constrain(30*(rad*40-d),0,100);
e5308763 313 return lx.hsb(lxh(),100, d); // clock
a41f334c
MS
314
315 case 8: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
316 d = min(
317 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
318 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
319 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a1.getX(r),a1.getY(r)) // triangle
320 );
321 d = constrain(30*(rad*40-d),0,100);
e5308763 322 return lx.hsb(lxh(),100, d); // clock
a41f334c
MS
323
324 case 9: r = amp*200 * map(bnc,0,1,1,sin(PI*t));
325 d = min(
326 distToSeg(Px.x, Px.y, a1.getX(r),a1.getY(r), a2.getX(r),a2.getY(r)),
327 distToSeg(Px.x, Px.y, a2.getX(r),a2.getY(r), a3.getX(r),a3.getY(r)),
328 distToSeg(Px.x, Px.y, a3.getX(r),a3.getY(r), a4.getX(r),a4.getY(r)),
329 distToSeg(Px.x, Px.y, a4.getX(r),a4.getY(r), a1.getX(r),a1.getY(r)) // quad
330 );
331 d = constrain(30*(rad*40-d),0,100);
e5308763 332 return lx.hsb(lxh(),100, d); // clock
a41f334c
MS
333
334 case 10:
335 r = map(bnc,0,1,a1.r,amp*200*sin(PI*t));
e5308763 336 return lx.hsb(lxh(),100,c1c(.9+2*rad - dist(Px.x,Px.y,a1.getX(r),a1.getY(r))*.03) ); // sphere
a41f334c
MS
337
338 case 11:
339 Px.z=mCtr.z; cMid.z=mCtr.z;
e5308763 340 return lx.hsb(lxh(),100,c1c(1 - CalcCone(Px,cMid,mCtr) * 0.02 > .5?1:0)); // cone
a41f334c 341
e5308763 342 case 12: return lx.hsb(lxh() + noise(Pn.x,Pn.y,Pn.z + (NoiseMove+50000)/1000.)*200,
a41f334c
MS
343 85,c1c(Pn.y < noise(Pn.x + NoiseMove/2000.,Pn.z)*(1+amp)-amp/2.-.1 ? 1 : 0)); // noise
344
935819e6 345 case 13:
346 case 14: float y=0; for (rWave w : waves) y += .5*w.val(Pn.x); // wave
a41f334c
MS
347 V.set(Pn.x, .7+y, Pn.z);
348 break;
349
fb83f322 350 default: return lx.hsb(0,0,0);
a41f334c
MS
351 }
352
e5308763 353 return lx.hsb(lxh(), 100, c1c(1 - V.distance(Pn)/rad));
a41f334c
MS
354 }
355}
356//----------------------------------------------------------------------------------------------------------------------------------
357// 0 - TLB, L (b), BLB, B (l) // Fwd , Down, Back, Up
358// 4 - TLF, F (l), BLF, L (f) // Fwd , Down, Back, Up
359// 8 - TRF, R (f), BRF, F (r) // Back, Down, Fwd , Up
360// 12- TRB, B (r), BRB, R (b) // Back, Down, Fwd , Up
361// 1->7, 15->9
362
0fbf977b 363int randDir() { return round(random(1))*2-1; }
364//----------------------------------------------------------------------------------------------------------------------------------
365boolean dDebug = true;
a41f334c 366class dCursor {
41f26e8f 367 dVertex vCur, vNext, vDest;
368 float destSpeed;
369 int posStop, pos,posNext; // 0 - 65535
a41f334c 370 color clr;
0fbf977b 371
41f26e8f 372 dCursor() {}
373
374 boolean isDone () { return pos==posStop; }
375 boolean atDest () { return vCur.s==vDest.s ||
935819e6 376 pointDist(vCur.getPoint(0), vDest.getPoint(0)) < 12 ||
377 pointDist(vCur.getPoint(0), vDest.getPoint(15))< 12;}
378 void setCur (dVertex _v, int _p) { p2=null; vCur=_v; pos=_p; pickNext(); }
41f26e8f 379 void setCur (dPixel _p) { setCur(_p.v, _p.pos); }
380 void setNext (dVertex _v, int _p, int _s) { vNext = _v; posNext = _p<<12; posStop = _s<<12; }
381 void setDest (dVertex _v, float _speed) { vDest = _v; destSpeed = _speed; }
935819e6 382 void onDone () { setCur(vNext, posNext); pickNext(); }
41f26e8f 383
384 float minDist;
385 int nTurns;
386 boolean bRandEval;
387
935819e6 388 void evaluate(dVertex v, int p, int s) {
41f26e8f 389 if (v == null) return; ++nTurns;
390 if (bRandEval) {
391 if (random(nTurns) < 1) setNext(v,p,s); return; }
392 else {
935819e6 393 float d = pointDist(v.getPoint(15), vDest.getPoint(0));
41f26e8f 394 if (d < minDist) { minDist=d; setNext(v,p,s); }
395 if (d == minDist && random(2)<1) { minDist=d; setNext(v,p,s); }
396 }
397 }
a41f334c 398
935819e6 399 void evalTurn(dTurn t) {
41f26e8f 400 if (t == null || t.pos0<<12 <= pos) return;
935819e6 401 evaluate(t.v , t.pos1, t.pos0);
402 evaluate(t.v.opp, 16-t.pos1, t.pos0);
a41f334c
MS
403 }
404
935819e6 405 void pickNext() {
41f26e8f 406 bRandEval = random(.05+destSpeed) < .05; minDist=500; nTurns=0;
935819e6 407 evaluate(vCur.c0, 0, 16); evaluate(vCur.c1, 0, 16);
408 evalTurn(vCur.t0); evalTurn(vCur.t1);
a41f334c 409 }
41f26e8f 410
411 Point p1, p2; int i2;
412
413 int draw(int nAmount, SCPattern pat) {
414 int nFrom = (pos ) >> 12;
415 int nMv = min(nAmount, posStop-pos);
416 int nTo = min(15,(pos+nMv) >> 12);
417 dVertex v = vCur;
418
935819e6 419 if (dDebug) { p1 = v.getPoint(nFrom); float d = (p2 == null ? 0 : pointDist(p1,p2)); if (d>5) { println("too wide! quitting: " + d); exit(); }}
a5e5b14c 420 for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.ci + v.dir*i ] = clr; }
41f26e8f 421 if (v.same != null) for (int i = nFrom; i <= nTo; i++) { pat.getColors()[v.same.ci + v.same.dir*i] = clr; }
a5e5b14c 422
41f26e8f 423 if (dDebug) { p2 = v.getPoint(nTo); i2 = nTo; }
424
425 pos += nMv; return nAmount - nMv;
935819e6 426 }
a41f334c
MS
427}
428
a41f334c
MS
429//----------------------------------------------------------------------------------------------------------------------------------
430class Worms extends SCPattern {
0fbf977b 431 float StripsPerSec = 10;
41f26e8f 432 float TrailTime = 3000;
433 int numCursors = 50;
434 ArrayList<dCursor> cur = new ArrayList<dCursor>(30);
435
436 private GraphicEQ eq = null;
437
438 private BasicParameter pBeat = new BasicParameter("BEAT", 0);
439 private BasicParameter pSpeed = new BasicParameter("FAST", .2);
440 private BasicParameter pBlur = new BasicParameter("BLUR", .3);
441 private BasicParameter pWorms = new BasicParameter("WRMS", .3);
442 private BasicParameter pConfusion = new BasicParameter("CONF", .1);
443 private BasicParameter pEQ = new BasicParameter("EQ" , 0);
444 private BasicParameter pSpawn = new BasicParameter("DIR" , 0);
e5308763 445 private BasicParameter pColor = new BasicParameter("CLR" , .1);
41f26e8f 446
41f26e8f 447 int zMidLat = 82;
448 float nConfusion;
449 private final Click moveChase = new Click(1000);
450
451 xyz middle;
e5308763 452 int AnimNum() { return floor(pSpawn.getValuef()*(4-.01)); }
41f26e8f 453 float randX() { return random(model.xMax-model.xMin)+model.xMin; }
454 float randY() { return random(model.yMax-model.yMin)+model.yMin; }
455 xyz randEdge() {
456 return random(2) < 1 ? new xyz(random(2)<1 ? model.xMin:model.xMax, randY(), zMidLat) :
457 new xyz(randX(), random(2)<1 ? model.yMin:model.yMax, zMidLat) ;
458 }
459
460 Worms(GLucose glucose) {
461 super(glucose);
462 addModulator(moveChase).start();
463 addParameter(pBeat); addParameter(pSpeed);
464 addParameter(pBlur); addParameter(pWorms);
465 addParameter(pEQ); addParameter(pConfusion);
e5308763 466 addParameter(pSpawn); addParameter(pColor);
467
41f26e8f 468 middle = new xyz(model.cx, model.cy, 71);
469 if (lattice == null) lattice = new dLattice();
470 for (int i=0; i<numCursors; i++) { dCursor c = new dCursor(); reset(c); cur.add(c); }
471 onParameterChanged(pEQ); setNewDest();
472 }
473
a5e5b14c 474 void onParameterChanged(LXParameter parameter) {
41f26e8f 475 nConfusion = 1-pConfusion.getValuef();
476 for (int i=0; i<numCursors; i++) {
477 if (parameter==pSpawn) reset(cur.get(i));
478 cur.get(i).destSpeed = nConfusion;
479 }
480 }
0fbf977b 481
e5308763 482 float getClr() { return lx.getBaseHuef() + random(pColor.getValuef()*300); }
41f26e8f 483 void reset(dCursor c) {
484 switch(AnimNum()) {
e5308763 485 case 0: c.clr = lx.hsb(getClr(),100,100); // middle to edges
41f26e8f 486 c.setDest(lattice.getClosest(randEdge()).v, nConfusion);
487 c.setCur (lattice.getClosest(middle));
488 break;
0fbf977b 489
e5308763 490 case 1: c.clr = lx.hsb(getClr(),100,100); // top to bottom
41f26e8f 491 float xLin = randX();
492 c.setDest(lattice.getClosest(new xyz(xLin, 0 , zMidLat)).v, nConfusion);
493 c.setCur (lattice.getClosest(new xyz(xLin, model.yMax, zMidLat)));
494 break;
0fbf977b 495
e5308763 496 case 2: c.clr = lx.hsb(getClr(),100,100); break; // chase a point around
a5e5b14c 497
498 case 3: boolean bLeft = random(2)<1;
e5308763 499 c.clr = lx.hsb(getClr()+random(120),100,100); // sideways
a5e5b14c 500 float yLin = randX();
501 c.setDest(lattice.getClosest(new xyz(bLeft ? 0 : model.xMax,yLin,zMidLat)).v, nConfusion);
502 c.setCur (lattice.getClosest(new xyz(bLeft ? model.xMax : 0,yLin,zMidLat)));
503 break;
41f26e8f 504 }
a5e5b14c 505 if (pBlur.getValuef() == 1 && random(2)<1) c.clr = lx.hsb(0,0,0);
a41f334c
MS
506 }
507
41f26e8f 508 void setNewDest() {
509 if (AnimNum() != 2) return;
510 xyz dest = new xyz(randX(), randY(), zMidLat);
511 for (int i=0; i<numCursors; i++) {
512 cur.get(i).setDest(lattice.getClosest(dest).v, nConfusion);
e5308763 513 cur.get(i).clr = lx.hsb(getClr()+75,100,100); // chase a point around
41f26e8f 514 }
a41f334c
MS
515 }
516
41f26e8f 517 void run(double deltaMs) {
518 if (deltaMs > 100) return;
519 if (moveChase.click()) setNewDest();
520
521 float fBass=0, fTreble=0;
a5e5b14c 522 if (pEQ.getValuef()>0) { // EQ
41f26e8f 523 eq.run(deltaMs);
524 fBass = eq.getAverageLevel(0, 4);
525 fTreble = eq.getAverageLevel(eq.numBands-7, 7);
526 }
527
a5e5b14c 528 if (pBlur.getValuef() < 1) { // trails
529 for (int i=0,s=model.points.size(); i<s; i++) {
e5308763 530 color c = colors[i]; float b = lx.b(c);
531 if (b>0) colors[i] = lx.hsb(lx.h(c), lx.s(c), constrain((float)(b-100*deltaMs/(pBlur.getValuef()*TrailTime)),0,100));
a5e5b14c 532 }
0fbf977b 533 }
a41f334c 534
41f26e8f 535 int nWorms = floor(pWorms.getValuef() * numCursors *
536 map(pEQ.getValuef(),0,1,1,constrain(2*fTreble,0,1)));
537
538 for (int i=0; i<nWorms; i++) {
539 dCursor c = cur.get(i);
540 int nLeft = floor((float)deltaMs*.001*StripsPerSec * 65536 * (5*pSpeed.getValuef()));
541 nLeft *= (1 - lx.tempo.rampf()*pBeat.getValuef());
542 while(nLeft > 0) {
543 nLeft = c.draw(nLeft,this); if (!c.isDone()) continue;
544 c.onDone(); if (c.atDest()) reset(c);
545 }
a41f334c
MS
546 }
547 }
41f26e8f 548
549
550 public void onActive() { if (eq == null) {
551 eq = new GraphicEQ(lx, 16); eq.slope.setValue(0.6);
552 eq.level.setValue(0.65); eq.range.setValue(0.35);
553 eq.release.setValue(0.4);
554 }}
a41f334c
MS
555}
556//----------------------------------------------------------------------------------------------------------------------------------