// ====================================================================
//            JAVA APPLET PROGRAM BASE SOURCE     BakuretuKen          
// ====================================================================

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.MalformedURLException;
import java.io.*;

// ===============================================================

public class MapPaint extends Applet {
	
	// グローバル変数
	
	// 背景イメージ
	Image haikei[];
	//その他
	Image player1, player2;
	// MAPデータ
	int map[][];
	// キャラの座標
	int cx, cy;
	
	// -----------------------------------------------------------
	public void init() {  // 初期処理
		
		// 背景配列の初期化
		haikei = new Image[6];
		
		haikei[0] = getImage(getDocumentBase(), "back00.gif");
		haikei[1] = getImage(getDocumentBase(), "back01.gif");
		haikei[2] = getImage(getDocumentBase(), "back02.gif");
		haikei[3] = getImage(getDocumentBase(), "back03.gif");
		haikei[4] = getImage(getDocumentBase(), "back04.gif");
		haikei[5] = getImage(getDocumentBase(), "back05.gif");
		player1   = getImage(getDocumentBase(), "kuma01.gif");
		player2   = getImage(getDocumentBase(), "kuma02.gif");
		
		// マップ配列の初期化
		map = new int[300][300];
		// ランダムで 300 x 300 のマップを作る
		for (int yy=0; yy<300; yy++){
			for (int xx=0; xx<300; xx++){
				map[xx][yy] = (int) (4 * Math.random());// ランダム
				if( xx<4 )   map[xx][yy] = 4;
				if( xx>295 ) map[xx][yy] = 4;
				if( yy<4 )   map[xx][yy] = 4;
				if( yy>295 ) map[xx][yy] = 4;
			}
		}
		
		// キャラ座標の初期値
		cx=50; cy=50;
		
	} // init()
	
	// -----------------------------------------------------------
	public void paint(Graphics g) {  // 作画処理
		
		int no, mx, my;
		
		// 背景を作画
		for ( my=0; my<9; my++) {
			for ( mx=0; mx<9; mx++) {
				no = map[cx-4+mx][cy-4+my];
				g.drawImage( haikei[no], mx*40, my*40, this);
			}
		}
		// キャラを中央に作画
		g.drawImage( player1, 4*40, 4*40, this);
		
	} // paint()
	
	// -----------------------------------------------------------
	public void update(Graphics g) {  // 再作画処理
		paint( g );
	} // update()
	
	// -----------------------------------------------------------
	public boolean mouseDown(Event e, int x, int y) {  // マウスダウン
		
		int pos;
		
		// 押された場所を調べる
		pos = checkDirection( x, y);
		
		// 押された場所にあわせて、キャラ座標を変更
		if (pos == 1) cy--;
		if (pos == 3) cy++;
		if (pos == 2) cx++;
		if (pos == 4) cx--;
		
		// 再作画
		repaint();
		
		return true;
	} // mouseDown()
	
	// -----------------------------------------------------------
	public boolean mouseUp(Event e, int x, int y) {  // マウスアップ
		
		return true;
	} // mouseUp()
	
	// -----------------------------------------------------------
	public boolean mouseDrag(Event e, int x, int y) { //  マウス移動
		
		return true;
	} // mouseDrag()
	
	// -----------------------------------------------------------
	// 場所判定プログラム checkDirection()
	// 返り値(int)  上:1  右:2  下:3  左:4
	public int checkDirection(int mx, int my) {
		boolean c1=false;
		boolean c2=false;
		Dimension dim = this.size();
		my = dim.height-my;
		double dx = (double)dim.width;
		double dy = (double)dim.height;
		if(my>(int)((dy/dx)*mx)) c1=true;
		if(my>(int)(dy-(dy/dx)*mx)) c2=true;
		if (c1&&c2)   return 1;
		if (!c1&&c2)  return 2;
		if (!c1&&!c2) return 3;
		if (c1&&!c2)  return 4;
		return 0;
	} // checkDirection()
	
// ===============================================================
}  // End of Class

