

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


/* "hatch3 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
	*/
public class hatch3 extends water {
	int doubleRays;
	double rayAngle, rayVaries, halfRayVaries;
	int wandering;


	void walkPart(Graphics gr, double ox, double oy, int dir, double speed, int color) {
		double x, y, dx, dy;
		int d, c;
		boolean notDone;

		x = ox; y=oy; d=dir;

		notDone=true;
		while (notDone) {
			ox = x;
			oy = y;
			x = x + (speed * cosines[dir]);
			y = y + (speed * sines[dir]);
			if (x > MAX_X) { notDone=false; }
			if (x < MIN_X) { notDone=false; }
			if (y > MAX_Y) { notDone=false; }
			if (y < MIN_Y) { notDone=false; }
			c  = getColorG((int)x, (int)y) - color;
			if (c<0) { c=0-c; }
			if ((notDone) && (c<20)) {
				r_drawLine(gr, (int)x, (int)y, (int)ox, (int)oy);
			}
		}
	}



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

		gx = Math.random() * imageX * imageXscale;
		gy = Math.random() * imageY * imageYscale;

		parts[which][DIR] = Math.random() *360.0;
		parts[which][JUMPED] = 0.0;
		parts[which][SPEED] = 3.0;

		partColors[which] = Color.white;
		parts[which][XPOS] = gx;
		parts[which][YPOS] = gy;
	}


	void initMore() {
		doubleRays =
			java.lang.Integer.parseInt(getParameter("doubleRays"));
		rayAngle = (double)
			java.lang.Integer.parseInt(getParameter("rayAngle"));
		wandering =
			java.lang.Integer.parseInt(getParameter("wandering"));
		rayVaries = (double)
			java.lang.Integer.parseInt(getParameter("rayVaries"));
		halfRayVaries = -0.5 * rayVaries;

	}



	void drawPart(Graphics gr, int which) {
		int px, py, d, c;
		Color ac;

		px = ((int)parts[which][XPOS]);
		py = ((int)parts[which][YPOS]);
		c = getColorG(px, py);
		ac = new Color(c, c, c);
		r_setColor(gr, ac);

		d = (int)(parts[which][DIR] + (Math.random()*rayVaries) + halfRayVaries + rayAngle) % 360;
		if (d<0) { d+=360; }

		walkPart(gr, parts[which][XPOS], parts[which][YPOS], d, 5.0, c);
		if (doubleRays==1) {
			walkPart(gr, parts[which][XPOS], parts[which][YPOS], (d+180)%360, 5.0, c);
		}
	}

	void movePart(int which) {
		double dx, dy;
		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];

		if (wandering==1) {
			parts[which][DIR] += Math.random() * 20.0-10.0;
		}

		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;

	}

}

