/* 
	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;

	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);

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

		nMousex 		= 0;
		nMousey 		= 0;
		bMouse_down		= false;
		
		// 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);

		if (imgBackground != null)
		{
			gDoubleBuffer.drawImage(imgBackground, 0, 0, null);
			fieldSmile.draw(gDoubleBuffer);
			bat.setX(nMousex);
			bat.draw(gDoubleBuffer);
			ball.updateLocation();
			ball.draw(gDoubleBuffer);
		}

		// 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;  
	}
//---
}
