/*
	タイトル：もなーのもちつきver1.00
	制作：板わさ
*/

include "script/title.c"

//グローバル変数
int mona_x,mona_y;
int morara_x,morara_y;
int usu_x,usu_y;
int time;
int mouse_x,mouse_y,mouse;

int mona,morara,usu;
int time_tuki;

int tuki;
int r;


void main(){

	if (LoadGameFlag("save/rank.dat")) { //	読み込んでみる
		//	ファイルが存在しない
		ResetGameFlag();						//	フラグ２を丸ごとクリアして...
		SaveGameFlag("save/rank.dat");	//	ファイルを新規作成！
	}

	Initlize();
	Title();
	Start();

	loop{
		if (r == 0) goto label3;
		Game();
		Rank();

		label3:

		Initlize();
		Title();
		Start();
		halt;
	}




}

void Game(){
	int i;
	int time2;

	ResetTime(0);
	loop {
		GetMouseInfo(&mouse_x,&mouse_y,&mouse);
		if (mouse == 2){
			mouse = 0;
			if (GetTime(0)>=time){
				//やられ処理
				mona = 21;
				for (i = 0;i < 2;i++){
					if (i <= 1){
						 mona_x = mona_x - 16;
					}
					Draw();
				}
				mona = 24;
				morara = 12;
				mona_x = mona_x - 75;
				mona_y = 238;
				Draw();
				PlayWave(2);
				ResetTime(0);		//1.0s
				loop {
					halt;
					if (GetTime(0)>=1000){
						goto label1;
					}
				}

			}
			PlayWave(1);
			mona = 21;
			for (i = 0;i < 4;i++){
				if (i <= 1){
					 mona_x = mona_x - 16;
				} else {
					 mona_x = mona_x + 16;
				}
				Draw();
			}

			tuki++;

			mona = 22;
			usu = 17;
			Draw();

			ResetTime(1);		//0.1s
			loop {
				GetMouseInfo(&mouse_x,&mouse_y,&mouse);
/*				if (mouse == 2){
					mouse = 0;

					//やられ処理
					mona = 21;
					for (i = 0;i < 2;i++){
						if (i <= 1){
							 mona_x = mona_x - 16;
						}
						Draw();
					}
					mona = 23;
					morara = 13;
					Draw();
					ResetTime(0);		//1.0s
					loop {
						halt;
						if (GetTime(0)>=1000){
							Quit();
							halt;
						}
					}
				}
*/
				halt;
				if (GetTime(1)>=100) break;
			}
			PlayWave(3);
			morara = 11;
			Draw();

			ResetTime(1);
			loop {
				GetMouseInfo(&mouse_x,&mouse_y,&mouse);
				if (mouse == 2){
					mouse = 0;

					//やられ処理
				PlayWave(1);
				mona = 21;
				for (i = 0;i < 2;i++){
					if (i <= 1){
						 mona_x = mona_x - 16;
					}
					Draw();
				}
				mona = 23;
				morara = 12;
				Draw();
				ResetTime(0);		//1.0s
				loop {
					halt;
					if (GetTime(0)>=1000){
						goto label1;
					}
				}

				}
				halt;
				if (GetTime(1)>=time_tuki) break;
			}

			morara = 10;
			usu = 16;
			Draw();

			ResetTime(1);
			loop {
				GetMouseInfo(&mouse_x,&mouse_y,&mouse);
				if (mouse == 2){
					mouse = 0;

					//やられ処理
					PlayWave(1);
					mona = 21;
					for (i = 0;i < 2;i++){
						if (i <= 1){
							 mona_x = mona_x - 16;
						}
						Draw();
					}
					mona = 23;
					morara = 13;
					Draw();
					ResetTime(0);		//1.0s
					loop {
						halt;
						if (GetTime(0)>=1000){
							goto label1;
						}
					}
				}
				halt;
				if (GetTime(1)>=time_tuki) break;
			}

			usu = 15;
			Draw();

			ResetTime(0);
			time = time - 30;
			time_tuki = time_tuki - 5;


		}
		time2 = time + 300;
		if (GetTime(0)>=time2){
			//やられ処理
			PlayWave(0);
			mona = 25;
			morara = 13;
			usu = 26;
			Draw();
			ResetTime(0);		//1.0s
			loop {
				halt;
				if (GetTime(0)>=1000){
					goto label1;
				}
			}
		}

		halt;
	}

	label1:

}

void Draw(){

	ClearSecondary();
	Blt(3,0,0);
	Blt(mona,mona_x,mona_y);
	Blt(morara,morara_x,morara_y);
	Blt(usu,usu_x,usu_y);
	sprintf1(string[5],"%dもちつけ",tuki);
	TextOut(5,string[5]);
	halt;
}

void Rank(){
	int i;

	StopMIDI();
	ReleaseMIDI();
	TextOut(5,"");
	ClearSecondary();

	if (tuki >= gameflag[1]){
		gameflag[3] = gameflag[2];
		gameflag[2] = gameflag[1];
		gameflag[1] = tuki;
		gameflag[4] = 1;		
	} else if(tuki >= gameflag[2]){
		gameflag[3] = gameflag[2];
		gameflag[2] = tuki;
		gameflag[4] = 2;
	} else if(tuki >= gameflag[3]){
		gameflag[3] = tuki;
		gameflag[4] = 3;
	}

	sprintf1(string[1],"１　：　%dもちつけ",gameflag[1]);
	sprintf1(string[2],"２　：　%dもちつけ",gameflag[2]);
	sprintf1(string[3],"３　：　%dもちつけ",gameflag[3]);
	for (i = 0;i < 4;i++){
		TextOut(i,string[i]);
	}

	if (gameflag[4] >= 1) TextOut(4,"ランクイン！");

	gameflag[4] = 0;

	SaveGameFlag("save/rank.dat");

	loop{
		GetMouseInfo(&mouse_x,&mouse_y,&mouse);
		if (mouse == 2){
			PlayWave(4);
			for (i = 0;i < 5;i++){
				TextOut(i,"");
			}
			mouse = 0;
		ResetTime(0);		//0.3s
		loop {
			halt;
			if (GetTime(0)>=300) break;
		}
			break;
		}
		Blt(2,0,0);
		halt;
	}

}

void Initlize(){
//ビットマップのロードとか
	int i;

	LoadBitmap("resource/morara01.bmp",10,0);		//モララー
	LoadBitmap("resource/morara02.bmp",11,0);
	LoadBitmap("resource/morara03.bmp",12,0);
	LoadBitmap("resource/morara04.bmp",13,0);
	LoadBitmap("resource/morara05.bmp",14,0);
	LoadBitmap("resource/usu01.bmp",15,0);		//うす
	LoadBitmap("resource/usu02.bmp",16,0);
	LoadBitmap("resource/usu03.bmp",17,0);
	LoadBitmap("resource/mona01.bmp",18,0);		//モナー
	LoadBitmap("resource/mona02.bmp",19,0);
	LoadBitmap("resource/mona03.bmp",20,0);
	LoadBitmap("resource/mona04.bmp",21,0);
	LoadBitmap("resource/mona05.bmp",22,0);
	LoadBitmap("resource/mona06.bmp",23,0);
	LoadBitmap("resource/mona07.bmp",24,0);
	LoadBitmap("resource/mona08.bmp",25,0);
	LoadBitmap("resource/usu04.bmp",26,0);
	LoadBitmap("resource/cursor.bmp",0,0);		//カーソル
	LoadBitmap("resource/title.bmp",1,0);		//タイトル
	LoadBitmap("resource/rank.bmp",2,0);		//ランキング
	LoadBitmap("resource/back.bmp",3,0);		//背景

	LoadMIDI("resource/bgm.mid");			//ＢＧＭ

	LoadWave("resource/biyon.wav",0);			//びよ〜ん
	LoadWave("resource/mona.wav",1);			//手
	LoadWave("resource/head.wav",2);			//ヘッドショット
	LoadWave("resource/tuki.wav",3);			//もちつけ
	LoadWave("resource/decide.wav",4);			//決定
	LoadWave("resource/explosion.wav",5);			//爆発

	for (i = 4;i < 100;i++){
		SetColorKeyPos(i,0,0);
	}

	SetColorKeyRGB(1,0,0,0);
	SetColorKeyRGB(2,0,0,0);
	SetColorKeyRGB(3,1,1,1);


	mona_x = 500;
	mona_y = 100;

	morara_x = 45;
	morara_y = 125;

	usu_x = 180;
	usu_y = 130;

	mouse_x = 0;
	mouse_y = 0;

	mouse = 5;

	time = 2000;
	time_tuki = 500;

	mona = 0;
	morara = 0;
	usu = 0;

	tuki = 0;

	MouseLayerOn(0,0,0);
	SetMouseInfo(250,185);

	TextLayerOn(1,120,99);
	TextLayerOn(2,120,99+91);
	TextLayerOn(3,120,99+91+91);
	TextLayerOn(4,355,323);
	TextLayerOn(5,40,50);

	for (i = 0;i < 6;i++){
		TextFont(i,1);
		TextColor(i,127,255,191);
		TextBackColorDisable(i);
		TextSize(i,32);
	}
	TextSize(4,20);
	TextColor(5,255,122,0);
	TextSize(5,50);

}

void Start(){
	int i;
	int vy;

	r = Rand(99);
	if (r == 0){
		i = 0;
		Blt(3,0,0);
		Blt(15,usu_x,usu_y);
		Blt(20,mona_x,mona_y);
		Blt(14,morara_x,morara_y);
	
		for (mona_x = mona_x;mona_x > -400;mona_x = mona_x - 6){	//ジンジャーが・・・
			if (i == 0){
				mona_y = mona_y - 3;
				i++;
			} else {
				mona_y = mona_y + 3;
				i = 0;
			}
			ClearSecondary();
			Blt(3,0,0);	
			Blt(20,mona_x,mona_y);
			Blt(15,usu_x,usu_y);
			Blt(14,morara_x,morara_y);
			halt;
		}
		PlayWave(5);
		ResetTime(0);		//5s
		loop {
			halt;
			if (GetTime(0)>=5000) break;
		}
		goto label2;
	}
		PlayMIDI();

	i = 0;
	Blt(3,0,0);
	Blt(15,usu_x,usu_y);
	Blt(18,mona_x,mona_y);
	Blt(14,morara_x,morara_y);

	for (mona_x = mona_x;mona_x > 280;mona_x = mona_x - 6){	//ジンジャーで出現
		if (i == 0){
			mona_y = mona_y - 3;
			i++;
		} else {
			mona_y = mona_y + 3;
			i = 0;
		}
		ClearSecondary();
		Blt(3,0,0);
		Blt(18,mona_x,mona_y);
		Blt(15,usu_x,usu_y);
		Blt(14,morara_x,morara_y);
		halt;
	}

	ResetTime(0);		//0.1s
	loop {
		halt;
		if (GetTime(0)>=100) break;
	}

	for (vy = -20;mona_y < 183;mona_y = mona_y + vy){	//グリコジャンプ
		vy++;
		ClearSecondary();
		Blt(3,0,0);
		Blt(19,mona_x,mona_y);
		Blt(15,usu_x,usu_y);
		Blt(10,morara_x,morara_y);
		halt;

	}

	mona_y = 193;

	ClearSecondary();
		Blt(3,0,0);
	Blt(22,mona_x,mona_y);
	Blt(15,usu_x,usu_y);
	Blt(10,morara_x,morara_y);
	halt;

	morara = 10;
	usu = 15;

	sprintf1(string[5],"%dもちつけ",tuki);
	TextOut(5,string[5]);

	label2:
}

