
/* "past3 extends water" means that there are a lot
	of routines in water.java that this applet imports
	and uses, so if you want to understand this code,
	read and understand water.java first.

	The routines in this file replace those in water.java
	that have the same names. This is called "class inheritance".
	This allows a small amount of code to slightly modify
	existing programs that can be very large.

	Anyway. This is my bit for Java toy-making. I am Neal McDonald,
	this is my code, take and eat, but leave in a pointer to me:
	www.workly.com
	*/

import java.applet.*;
import java.awt.*;
import java.awt.image.*;


public class past3 extends water {

	final static int OC = 9;		// previous color
	final static int ODIR = 10;		// previous direction


	void initPart(int which) {
		int r, g, b, cx, cy;
		double gx, gy;

		gx = (Math.random() * (double)imageX * imageXscale)+(double)imageXoffset;
		gy = (Math.random() * (double)imageY * imageYscale)+(double)imageYoffset;

		parts[which][XPOS] = gx;
		parts[which][YPOS] = (((double)which/(double)NUM_PARTS)*(double)imageY*imageXscale);
		parts[which][ODIR] = Math.random() *20.0 -10.0;
		parts[which][DIR] = parts[which][ODIR];
		parts[which][UPD] = Math.random() *20;

		parts[which][OX] = gx;
		parts[which][OY] = gy;
		parts[which][OC] = 0.0;
		parts[which][SPEED] = 15.0;

		r = getColorG((int)gx, (int)gy);
		partColors[which] = new Color(r, r, r);
	}

	void drawPart(Graphics gr, int which) {
		int px,py,ox,oy,  cx, cy, r, mcol, closeness;
		double c, dc;
		Color ac;

		px = ((int)parts[which][XPOS]);
		py = ((int)parts[which][YPOS]);
		ox = ((int)parts[which][OX]);
		oy = ((int)parts[which][OY]);

		r = getColorG((int)px, (int)py);

		c = (double)r;
		dc = c - parts[which][OC];
		if (dc<0.0) { dc = 0.0-dc; }

		if (r>127) { // make white/gray
			closeness = r-127;
			mcol = 255;
		} else { // black/gray
			closeness = 127-r;
			mcol = 0;
		}
		if (Math.random()*127.0 > (double)closeness) {
			mcol = 128;
		}

		parts[which][OC] = c;

		if ((parts[which][JUMPED]==0) && (dc<20)) {
			ac = new Color(mcol,mcol,mcol);
			r_setColor(gr, ac);
			r_drawLine(gr, px, py, ox, oy);
		}

	}

	void movePart(int which) {
		double dx, dy, dr;
		int ix, jumped;

		ix = (int)(parts[which][DIR]) % 360;
		if (ix<0) ix+=360;
		parts[which][OX] = parts[which][XPOS];
		parts[which][OY] = parts[which][YPOS];
		parts[which][XPOS] += parts[which][SPEED] * cosines[ix];
		parts[which][YPOS] += parts[which][SPEED] * sines[ix];

		dr = parts[which][DIR] - parts[which][ODIR];
		dr += Math.random() * 5.0-2.5;
		if (dr>20.0) { dr = 20.0; }
		if (dr<-20.0) { dr = -20.0; }
		parts[which][DIR] = parts[which][ODIR] + dr;

		jumped=0;
		if (parts[which][XPOS] > MAX_X) {
			parts[which][XPOS] = MIN_X;
			jumped=1;
		}
		if (parts[which][XPOS] < MIN_X) {
			parts[which][XPOS] = MAX_X;
			jumped=1;
		}
		if (parts[which][YPOS] > MAX_Y) {
			parts[which][YPOS] = MIN_Y;
			jumped=1;
		}
		if (parts[which][YPOS] < MIN_Y) {
			parts[which][YPOS] = MAX_Y;
			jumped=1;
		}
		parts[which][JUMPED] = (double)jumped;
	}

}

