/*
 *       Mogura-tataki on the web  -  Webmoles
 *
 *                        (c) 1996 Tatsuya Miyamae
 */

import java.applet.Applet;
import java.awt.*;

public class Mogura extends Applet implements Runnable {
	MediaTracker mt = new MediaTracker(this);
	Thread thread;
	Graphics gra;
	Button btn = new Button("Start");
	int run_mode = 0;
	int matsplash = 4;
	int Width  = matsplash * 64;
	int Height = matsplash * 64;
	int matsize_x = Width  / matsplash;
	int matsize_y = Height / matsplash;
	int matrix[] = new int[matsplash * matsplash];
	int SpeedMin;
	int SpeedLen;
	int Moles;
	int Score;
	int Time;
	int HiScore  = 0;
    Image MoleImg0, MoleImg1, MoleImg2, MoleImg3, MoleImg4, MoleOuchImg;

	public void init() {
		gra    = getGraphics();
		setBackground(Color.black);

		MoleImg0 = getImage(getDocumentBase(), "datas/t1.gif");
		mt.addImage(MoleImg0, 0);
		MoleImg1 = getImage(getDocumentBase(), "datas/t2.gif");
		mt.addImage(MoleImg1, 0);
		MoleImg2 = getImage(getDocumentBase(), "datas/t3.gif");
		mt.addImage(MoleImg2, 0);
		MoleImg3 = getImage(getDocumentBase(), "datas/t4.gif");
		mt.addImage(MoleImg3, 0);
		MoleImg4 = getImage(getDocumentBase(), "datas/t5.gif");
		mt.addImage(MoleImg4, 0);
		MoleOuchImg = getImage(getDocumentBase(), "datas/ouch.gif");
		mt.addImage(MoleOuchImg, 0);

		setLayout(null);
		init_param();
	}

	public void init_param() {
		add(btn);
		btn.reshape(Width + 40, 180, 60, 20);
		resize(Width + 200, Height);

		SpeedMin = 10;
		SpeedLen = 10;
		Moles    = 95;
		Score    = 0;
		Time     = 60000;
		run_mode = 0;
		repaint();
	}

	public boolean action(Event e, Object o) {
		run_mode = 1;
		repaint();
		remove(btn);
		return true;
	}

	public boolean mouseDown(Event e, int x, int y) {
		if (run_mode == 1 && x <= Width) {
			int mx = x / matsize_x;
			int my = y / matsize_y;
			if (matrix[mx+my*matsplash] > 0) {
				matrix[mx+my*matsplash] = -1;
				gra.setFont(new Font("Helvetica", Font.PLAIN, 18));
				gra.setColor(Color.black);
				gra.drawString(String.valueOf(Score)    , Width + 100, 120);
				Score++;
				gra.setColor(Color.white);
				gra.drawString(String.valueOf(Score)    , Width + 100, 120);
			}
		}
		return true;
	}

	public void paint(Graphics g) {
		if (mt.isErrorID(0)) {
			g.setColor(Color.red);
			g.fillRect(0, 0, size().width, size().height);
			g.setColor(Color.yellow);
			g.drawString("Error !!", 20, 60);
			return;
		}
		if (mt.checkID(0)) {
//			for (int ly=0; ly < matsplash; ly++) {
//				for (int lx=0; lx < matsplash; lx++) {
//					g.drawImage(MoleImg0, matsize_x*lx, matsize_x*ly, this);
//				}
//			}
			g.setColor(Color.white);
			g.setFont(new Font("TimesRoman", Font.ITALIC, 12));
			g.drawString("Mogura-tataki on the web", Width + 10, 50);
			g.setFont(new Font("Helvetica", Font.BOLD, 20));
			g.drawString("W e b m o l e s", Width + 40, 70);
			g.setFont(new Font("TimesRoman", Font.ITALIC, 12));
			g.drawString("Programmed by Tatsuya Miyamae", Width + 10, 230);

			g.setFont(new Font("TimesRoman", Font.PLAIN, 16));
			g.drawString("HiScore : ", Width + 10, 100);
			g.drawString("Score : "  , Width + 10, 120);
			g.drawString("Time :"    , Width + 10, 140);
			g.setFont(new Font("Helvetica", Font.PLAIN, 18));
			g.drawString(String.valueOf(HiScore)  , Width + 100, 100);
			g.drawString(String.valueOf(Score)    , Width + 100, 120);
			g.drawString(String.valueOf(Time/1000), Width + 100, 140);
			if (run_mode == 0) {
				g.setColor(Color.yellow);
				g.setFont(new Font("Helvetica", Font.BOLD, 16));
				g.drawString("- GAME OVER -", Width + 10, 170);
				g.setColor(Color.white);
			}
		} else {
			g.drawString("Loading..", 20, 60);
		}
	}

	public void start() {
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stop() {
		if (thread != null) {
			thread.stop();
			thread = null;
		}
	}

	public void run() {
		Image drawmole;

		try {
			mt.waitForID(0);
		} catch (InterruptedException e) {
			return;
		}

		repaint();

		while (true) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				break;
			}

			if (run_mode == 1) {
				Time -= 50;
				if (Time % 1000 == 0) {
					gra.setColor(Color.black);
					gra.fillRect(Width+100, 120, 100, 20);
					gra.setFont(new Font("Helvetica", Font.PLAIN, 18));
					if (Time <= 5000)
						gra.setColor(Color.red);
					else
						gra.setColor(Color.white);
					gra.drawString(String.valueOf(Time/1000), Width + 100, 140);
					if (Time == 0) {
						if (Score > HiScore)
							HiScore = Score;
						init_param();
					}
					if (Time % 5000 == 0) {
						Moles--;
					}
					if (Time % 8000 == 0) {
						SpeedMin--;
						SpeedLen--;
					}
				}
			}

			int pos = (int)(Math.random() * 1000 % (matsplash * matsplash));

			if (matrix[pos] <= 0 && (int)(Math.random() * 100) > Moles) {
				matrix[pos] =
				       1 + (int)(Math.random() * 1000 % SpeedLen + SpeedMin);
			//	System.out.println(String.valueOf(matrix[pos]));
				gra.drawImage(MoleImg2, matsize_x*(pos%matsplash),
				                     matsize_x*(pos/matsplash), this);
				continue;
			}

			for (int i=0,ly=0; ly < matsplash; ly++) {
				for (int lx=0; lx < matsplash; lx++, i++) {
					if (matrix[i] > 0) {
						matrix[i]--;
						if (matrix[i] > 1) {
							if (matrix[i] % 8 == 0)
								drawmole = MoleImg4;
							else
								drawmole = MoleImg1;
						} else {
							if (matrix[i] == 0)
								drawmole = MoleImg3;
							else
								drawmole = MoleImg2;
						}
					} else if (matrix[i] == -1) {
						drawmole = MoleOuchImg;
						matrix[i] = 0;
					} else {
						drawmole = MoleImg0;
					}
					gra.drawImage(drawmole, matsize_x*lx, matsize_x*ly, this);
				}
			}
		}
	}
}
