/* 
	Game.java

	[ more java @ mlab - 10-17.1.2000 - juhuu@katastro.fi ]

*/

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.util.*;
import Target;
import Field;

public class Game extends Applet implements Runnable
{
	Image		imgDoubleBuffer;
	Graphics	gDoubleBuffer;
	Thread		engine = null;
  
	int			nMousex, nMousey;
	boolean 	bMouse_down;
	
	Image 		imgBackground;
	Image 		imgBat;
	Image 		imgBall;

	String[]	sImages = {"test.gif", "test2.gif", "test3.gif"};
	Image[]		imgImages;

	int			nScr_width;
	int			nScr_height;
	int			nOhNocount;
	byte[]		yEmptyField		= {0,0,0,0,0,0,0,0,0,0,
								   0,0,0,0,0,0,0,0,0,0,
								   0,0,0,0,0,0,0,0,0,0,
								   0,0,0,0,0,0,0,0,0,0,
							       0,0,0,0,0,0,0,0,0,0};

	byte[]		ySmileField		= {0,0,1,1,0,0,1,1,0,0,
								   0,0,1,1,0,0,1,1,0,0,
								   2,0,0,0,3,3,0,0,0,2,
								   2,0,0,0,0,0,0,0,0,2,
							       2,2,2,2,2,2,2,2,2,2};
	
	Field		fieldSmile;
	Bat			bat;
	Ball		ball;
	
	public void init() {
		// initialize double buffering
		imgDoubleBuffer = createImage(size().width, size().height);
		gDoubleBuffer = imgDoubleBuffer.getGraphics();
		gDoubleBuffer.setColor(Color.white);
		gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		// font
		Font font = new Font("Arial", Font.BOLD, 16);
		gDoubleBuffer.setFont(font);

		nScr_width		= size().width;
		nScr_height		= size().height;

		nMousex 		= 0;
		nMousey 		= 0;
		bMouse_down		= false;
				// counter for the 'oh no!' text
		nOhNocount		= 0;
				// let's load the images
		String sBackground	= "background.jpg";
		String sBat			= "bat.jpg";
		String sBall		= "ball.jpg";

		MediaTracker med = new MediaTracker(this);

		imgImages = new Image[sImages.length];

		for (int i = 0 ; i < imgImages.length ; i++)
		{
			imgImages[i] = getImage(getDocumentBase(), sImages[i]);
			med.addImage(imgImages[i], i);
		}
		
		imgBackground = getImage(getDocumentBase(), sBackground);
		med.addImage(imgBackground, imgImages.length);

		imgBat = getImage(getDocumentBase(), sBat);
		med.addImage(imgBat, imgImages.length + 1);

		imgBall = getImage(getDocumentBase(), sBall);
		med.addImage(imgBall, imgImages.length + 2);
	
		try
		{
			med.waitForAll();
			}	catch (InterruptedException e) {
		}
		
		fieldSmile		= new Field(ySmileField, 10, 5, 0, 80, imgImages);
		bat				= new Bat(0, 450, imgBat);
		ball			= new Ball(0, 400, 2, -2, imgBall);
		ball.setMinMax(0,0,nScr_width, nScr_height);
	}

//----------------------------------------------------

	public void paint(Graphics g) {

		// clear the screen

		//    	gDoubleBuffer.setColor(Color.white);
		//    	gDoubleBuffer.fillRect(0, 0, size().width, size().height);

		int nHit;
		
		if (imgBackground != null)
		{
			gDoubleBuffer.drawImage(imgBackground, 0, 0, null);	
			// move the bat
			int nBatwidth = bat.getWidth();
			int nBatx = nMousex - (nBatwidth / 2);			

			if(nBatx > (nScr_width - nBatwidth))			{				bat.setX(nScr_width - nBatwidth);
			} else if(nBatx < 0) {				bat.setX(0);
			} else {
				bat.setX(nBatx);
			}
			// move the ball			boolean bMiss = ball.updateLocation();
			if (bMiss)
			{
				fieldSmile		= new Field(ySmileField, 10, 5, 0, 80, imgImages);
				nOhNocount		= 50;
			}			// check collisions
			nHit = ball.hit(bat);			if(nHit != 0)
			{
				if(nHit == 1 || nHit == 2)
				{
					System.out.println("changey\n");
					ball.changeYdirection();
				}

				if(nHit == 3 || nHit == 4)
				{
					System.out.println("changex\n");
					ball.changeXdirection();
				}
			}

			fieldSmile.checkCollisions(ball);

			fieldSmile.draw(gDoubleBuffer);
			ball.draw(gDoubleBuffer);
			bat.draw(gDoubleBuffer);
			gDoubleBuffer.setColor(Color.yellow);
			if(nOhNocount > 0)			{				gDoubleBuffer.drawString("OH NO!!!", 20, nScr_height - 200);
				nOhNocount--;			} else if (fieldSmile.getAmount() == 0) {				gDoubleBuffer.drawString("VICTORY!!!", 20, nScr_height - 200);
			} else {				gDoubleBuffer.drawString("STILL LEFT: " + fieldSmile.getAmount(), 20, nScr_height - 200);
			}
		}

		// draw all the stuff from the double buffer to the screen
		g.drawImage(imgDoubleBuffer, 0, 0, null);				
	}


//----------------------------------------------------


	// start the applet
	public void start() {
		if (engine == null)
		{
			engine = new Thread(this);
			engine.start();
		}
		showStatus(getAppletInfo());
	} 

	// stop the applet
	public void stop() {
		if (engine != null && engine.isAlive())
		{
			engine.stop();
		}

		engine = null; 
	}

	public void run()
	{                  
		while (true)
		{
			try
			{
				repaint();
				Thread.sleep(10);
			} catch (InterruptedException e) {
				stop();
			}
		}
	}

	public void update(Graphics g) {    
	  paint(g);
	}

	// print to the console & browserstatus
	private void print(String s) {
		System.out.println(s); 		// console
		showStatus(s);         		// browser status  
	}

//---

	public boolean mouseDown(Event e, int x, int y) {
		bMouse_down = true;
		return true;  
	}

	public boolean mouseUp(Event e, int x, int y) {
		bMouse_down = false;
		return true;  
	}

	public boolean mouseMove(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}

	public boolean mouseDrag(Event e, int x, int y)
	{
		nMousex = x;
		nMousey = y;
		return true;  
	}
//---
}
