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

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

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

public class novel extends Applet {
	
	// グローバル変数
	
	// テキスト保存
	String scene[];
	// 画面表示文字列
	String screen[];
	// 読み込み行番号
	int now_line;
	// 表示画像
	Image img;
	// フラグ
	boolean flag[];
	// 次のシーンファイル文字列
	String jump[];
	
	// -----------------------------------------------------------
	public void init() {  // 初期処理
		
		// 変数の初期化
		scene = new String[1000];
		screen = new String[15];
		now_line = 0;
		img = null;
		flag = new boolean[30];
		jump = new String[15];
		
		// フラグの初期化
		for (int i=0; i<30; i++) flag[i] = false;
		flag[0] = true;
		
		// テキストファイル start.txt の読み込み
		LoadScene("start.txt");
		// 画面表示のための文字列設定（0行から）
		now_line = SetText( 0 );
		// 再作画
		repaint();
		
		// マウス処理アクションリスナー定義と登録
		addMouseListener(new MouseAction());
	} // init()
	
	// -----------------------------------------------------------
	public void paint(Graphics g) {  // 作画処理
		
		// 背景を灰色に塗る
		g.setColor( Color.gray );
		g.fillRect( 0, 0, 480, 360 );
		
		// 背景画像を表示
		if( img != null ) {
			g.drawImage( img, 0, 0, this );
		}
		
		// 画面に文章表示
		g.setFont(new Font( "Dialog", Font.BOLD, 17 ));
		for ( int i=0; i<15; i++ ) {
			if( screen[i]!=null ){
				g.setColor( Color.black );
				g.drawString( screen[i], 3+2, (i*22)+22+1); // 文字影
				// 表示色設定(White or Cyan)
				if( jump[i]==null ){
					g.setColor( Color.white );
				} else {
					g.setColor( Color.cyan );
				}
				g.drawString( screen[i], 3, (i*22)+22); // 文字
			}
		}
		
	} // paint()
	
	// -----------------------------------------------------------
	public void update(Graphics g) {  // 再作画処理
		paint( g );
	} // update()
	
	// -----------------------------------------------------------
	// テキストファイル読み込み
	public void LoadScene( String filename ) {
		
		BufferedReader ds = null;
		String s = null;
		
		// 読み込み行番号初期化
		now_line = 0;
		
		// まず scene[] の全てに null を入れて初期化する
		for(int i=0; i<1000; i++) {
			scene[i] = null;
		}
		
		// テキストファイルを読み込むための初期宣言
		try {
			InputStream is = new URL(getCodeBase(), filename ).openStream();
			InputStreamReader myin = new InputStreamReader( is );
			ds = new BufferedReader(myin);
		} catch ( IOException e ) {
			System.out.println("テキストファイル読み込みエラー  " + filename );
		}
		
		// 実際のテキストファイル読み込み readLine()
		for(int i=0; i<1000; i++) {
			try {
				s = ds.readLine();
			} catch ( IOException e ) {
				System.out.println("テキストファイル読み込みエラー" );
			}
			if ( s==null ) break;
			scene[i] = s;
		}
		
	}// LoadScene()
	
	// -----------------------------------------------------------
	// 文章を表示する為、screen[] に文章を入れる関数
	public int SetText( int line ) {
		
		// まず screen[]とjump[] を全てに null を入れて初期化する
		for ( int i=0; i<15; i++ ) {
			screen[i] = null;
			jump[i] = null;
		}
		
		// 画面表示行番号（最大15行）
		int crt_line = 0;
		// 文章分割保存用
		String st1,st2,st3,st4;
		StringTokenizer st;
		
		// scene[]を読み込むためのループ
		for ( int i=0; i<1000; i++ ) {
			
			// 文章分割保存用変数初期化
			st1=null; st2=null; st3=null; st4=null;
			// StringTokenizer を使った文字列の分割準備
			st = new StringTokenizer(scene[line+i]);
			// 文字列を空白で分割
			if ( st.hasMoreTokens() ) st1 = st.nextToken();
			if ( st.hasMoreTokens() ) st2 = st.nextToken();
			if ( st.hasMoreTokens() ) st3 = st.nextToken();
			if ( st.hasMoreTokens() ) st4 = st.nextToken();
			if( st1 == null ) st1=" ";
			
			// ＜＜タグの処理＞＞
			
			// 「END」タグ処理
			if( st1.equals("END")) {
				return 9999;
			}
			
			// 「#」タグ処理
			if( st1.equals("#")) {
				continue;
			}
			
			// 「CG」タグ処理
			if( st1.equals("CG")) {
				img = getImage(getDocumentBase(), st2 );
				continue;
			}
			
			// 「CGOFF」タグ処理
			if( st1.equals("CGOFF")) {
				img = null;
				continue;
			}
			
			// 「FUP」タグ処理
			if( st1.equals("FUP")) {
				int no = Integer.parseInt( st2 );
				flag[no] = true;
				System.out.println("フラグ"+no+"番アップ\n");
				continue;
			}
			
			// 「FDOWN」タグ処理
			if( st1.equals("FDOWN")) {
				int no = Integer.parseInt( st2 );
				flag[no] = false;
				System.out.println("フラグ"+no+"番ダウン\n");
				continue;
			}
			
			// 「IF」タグ処理
			if( st1.equals("IF")) {
				int no = Integer.parseInt( st2 );
				if( flag[no] ){
					screen[crt_line] = st3;
					jump[crt_line] = st4;
					crt_line++;
				}
				continue;
			}
			
			// 「NIF」タグ処理
			if( st1.equals("NIF")) {
				int no = Integer.parseInt( st2 );
				if( ! flag[no] ){
					screen[crt_line] = st3;
					jump[crt_line] = st4;
					crt_line++;
				}
				continue;
			}
			
			
			// ＜＜タグがない場合の処理＞＞
			// screen[] に文字列をコピーする
			screen[crt_line] = scene[line+i];
			crt_line++;
			// もし読み込んだ行の下が END タグの場合は、返り値 9999
			if (scene[line+i+1].equals("END")) return 9999;
			// 画面表示が１５行に達したら、return
			if(crt_line == 15) return line+i+1;
		}
		
		return 9999;
		
	} // SetText()
	
// -----------------------------------------------------------
// マウス処理クラス(MouseAction Class)
class MouseAction extends MouseAdapter{
	
	public void mousePressed(MouseEvent e) {
		// マウスダウンイベント
		
		// クリックされたのが文章の何行目か調べる
		int no = e.getY()/22;
		if (no>14) return;
		// クリックされた行の jump[] がnullでない場合は次のシーンを読み込む
		if ( jump[no] != null ) {
			// ファイル jump[no] の読み込み
			LoadScene( jump[no] );
			// 画面表示のための文字列設定（0行から）
			now_line = SetText( 0 );
			// 再作画
			repaint();
			return;
		}
		
		// now_line が 9999 だったら何もしない
		if( now_line==9999 ) return;
		
		// 画面表示のための文字列設定（now_line行から）
		now_line = SetText( now_line );
		// 再作画
		repaint();
		
	} // mousePressed()
	
	public void mouseReleased(MouseEvent e) {
		// マウスアップイベント
		
	} // mouseReleased()
	
} // myWindowAction

// ===============================================================
}  // End of Class

