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

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

	Image 		imgBackground;

	int		nScr_width;
	int		nScr_height;

	Matrix3D	amat, tmat;
	Grid		grid;
		
	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;
				
		amat	= new Matrix3D();
		tmat	= new Matrix3D();
		
		amat.yrot(20);
		amat.xrot(20);
		
		grid 	= new Grid(16, 16);

		// let's load the images
		String sBackground	= "background.jpg";

		MediaTracker med = new MediaTracker(this);

		imgBackground = getImage(getDocumentBase(), sBackground);
		med.addImage(imgBackground, 0);

		try
		{
			med.waitForAll();
			}	catch (InterruptedException e) {
		}
  }

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

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

			grid.mat.unit();
			grid.mat.mult(amat);
			float xfac = (float) 0.7;	
			grid.mat.scale(xfac, -xfac, 16 * xfac / size().width);
			grid.mat.translate(size().width / 2, size().height / 2, 8);

			grid.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)
	{
		tmat.unit();
		float xtheta = (nMousey - y) * 360.0f / size().width;
		float ytheta = (x - nMousex) * 360.0f / size().height;
		tmat.xrot(xtheta);
		tmat.yrot(ytheta);
		amat.mult(tmat);

		nMousex = x;
		nMousey = y;
		return true;  
	}
//---
}
