/**
 * 1-- Game Start
 *     1.1-- Easy
 *     1.2-- Normal
 *     1.3-- Panic
 *           1.X.1-- Sunny Milk
 *           1.X.2-- Luna Child
 *           1.X.3-- Star Sapphire
 * 2-- Practice
 *     2.1-- Easy
 *     2.2-- Normal
 *     2.3-- Panic
 *           2.X.1-- Sunny Milk
 *           2.X.2-- Luna Child
 *           2.X.3-- Star Sapphire
 *                   2.X.X.1-- 1
 *                   2.X.X.2-- 2
 *                   2.X.X.3-- 3
 *                   2.X.X.4-- EX
 * 3-- Result
 *     3,1-- Sunny Milk
 *     3,2-- Luna Child
 *     3,3-- Star Sapphire
 * 4-- Config
 *     4-1-- BackGround
 *     4-2-- Effect
 *     4-3-- Data Clear
 *           4-3-1 Realy? Yes
 *           4-3-2 Realy? No
 * 5-- Exit
 */

/**
 * タイトル画面
 */
task Title(){
	//カーソル最大移動値
	let MaxCursorX = 0;
	let MaxCursorY = 0;
	
	//現在のカーソル座標
	let NowCursorX = 0;
	let NowCursorY = 0;
	
	//難易度
	//  1:Easy
	//  2:Normal
	//  3:Panic
	let Difficult = 0;
	
	//プレイヤー
	//  1:サニー
	//  2:ルナチャ
	//  3:スター
	let Player = 0;
	
	//ステージ
	//  0:本編(通し)
	//  1:1
	//  2:2
	//  3:3
	//  4:EX
	let Stage = 0;
	
	//BGM再生
	ChangeBGM( "", bgmTitle );
	
	//ステート切り替え
	State = State_Title;
	
	//メニュー用最大カーソル値
	MaxCursorX = 0;
	MaxCursorY = 4;
	
	//メッセージカウンタ
	let messageCount = 0;
	
	//説明
	let Exp_Start = "ステージを開始します";
	let Exp_Practice = "任意の1ステージのみをプレイすることができます";
	let Exp_Result = "リザルトを表示します";
	let Exp_Config = "コンフィグ画面を開きます";
	let Exp_Exit = "ゲームを終了します";
	
	let Exp_Player  = "自機を選択してください";
	let Exp_Difficult = "難易度を選択してください";
	let Exp_Stage = "ステージを選択してください";
	let Exp_StageCantPlay = "選択したステージは現段階では選択不可能です";
	
	let Exp_Config_Switch_1 = "背景の描画有無を切り替えます";
	let Exp_Config_Switch_2 = "詳細なエフェクトの描画有無を切り替えます";
	
	let Exp_Config_Delete_1 = "セーブデータ＆コンフィグデータを削除します";
	let Exp_Config_Delete_2 = "本当に削除してもよろしいですか？";
	
	//各種名前空間用データ
	let players = [ "Sunny", "Luna", "Star" ];
	let difficults = [ "Easy", "Normal", "Panic" ];
	let stages = [ "1", "2", "3", "4" ];
	
	//タイトルの間中ループ
	while( State == State_Title ){
		//現在のカーソル位置取得
		alternative( Cursor_Depth )
			case( 0 ){
				NowCursorX = Cursor1_X;
				NowCursorY = Cursor1_Y;
			}
			case( 1 ){
				NowCursorX = Cursor2_X;
				NowCursorY = Cursor2_Y;
			}
			case( 2 ){
				NowCursorX = Cursor3_X;
				NowCursorY = Cursor3_Y;
			}
			case( 3 ){
				NowCursorX = Cursor4_X;
				NowCursorY = Cursor4_Y;
			}
		
		//リザルト時は専用のキーチェックを行う
		if( ResultMode != 0 ){
			ResultKeyCheck();
		}
		
		//キーチェック
		if( GetKeyState( VK_SHOT ) == KEY_PUSH ){
			//現在のカーソル位置取得
			alternative( Cursor_Depth )
				case( 0 ){
					alternative( Cursor1_Y )
						case( 0 ){
							//1.X 難易度選択(本編)
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth += 1;
						}
						case( 1 ){
							//2.X 難易度選択(プラクティス)
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth += 1;
						}
						case( 2 ){
							//3.X リザルト
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 0;
							Cursor_Depth += 1;
							//リザルトモード突入
							ResultMode = 1;
						}
						case( 3 ){
							//4.X コンフィグ
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth += 1;
						}
						case( 4 ){
							//5 ゲーム終了
							ClearStage();
						}
				}
				case( 1 ){
					alternative( Cursor1_Y )
						case( 0 ){
							//1.X.X キャラクター選択(本編)
							//難易度指定
							Difficult = NowCursorY + 1;
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth += 1;
						}
						case( 1 ){
							//2.X.X キャラクター選択(プラクティス)
							//難易度指定
							Difficult = NowCursorY + 1;
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth += 1;
						}
						case( 3 ){
							//4.X コンフィグ
							alternative( Cursor2_Y )
								case( 0 ){
									//4.1 背景描画有無
									SetIsDrawBackGround( !GetIsDrawBackGround() );
								}
								case( 1 ){
									//4.2 演出描画有無
									SetIsDrawEffect( !GetIsDrawEffect() );
								}
								case( 2 ){
									//4.3 データ消去
									//カーソル設定
									NowCursorX = 0;
									NowCursorY = 1;
									MaxCursorX = 0;
									MaxCursorY = 1;
									Cursor_Depth += 1;
								}
						}
				}
				case( 2 ){
					alternative( Cursor1_Y )
						case( 0 ){
							//1.X.X 本編開始
							//プレイヤー指定
							Player = NowCursorY + 1;
							//ステージ指定
							Stage = 0;
							
							//ゲーム開始
							StageStart( Difficult, Player, Stage );
						}
						case( 1 ){
							//2.X.X ステージ選択(プラクティス)
							//プレイヤー指定
							Player = NowCursorY + 1;
							//カーソル設定
							NowCursorX = 0;
							NowCursorY = 0;
							MaxCursorX = 0;
							MaxCursorY = 3;
							Cursor_Depth += 1;
						}
						case( 3 ){
							//4.X コンフィグ
							alternative( Cursor2_Y )
								case( 2 ){
									//4.3.X データ消去
									alternative( Cursor3_Y )
										case( 0 ){
											//4.3.1 OK
											//セーブデータ消去
											ClearSaveData();
										}
										case( 1 ){
											//4.3.2 NG
											//1つ前に戻る
											//カーソル設定
											NowCursorX = 0;
											NowCursorY = 2;
											Cursor3_X = 0;
											Cursor3_Y = 0;
											MaxCursorX = 0;
											MaxCursorY = 2;
											Cursor_Depth -= 1;
										}
								}
						}
				}
				case( 3 ){
					alternative( Cursor1_Y )
						case( 1 ){
							//2.X.X プラクティス開始
							let isPlayableStage = false;
							let ns = players[ Player - 1 ] ~ "PlayNum" ~ difficults[ Difficult - 1 ] ~ stages[ NowCursorY ];
							let playNum = GetCommonDataDefaultEx( "OFT_Save", ns, 0 );
							if( playNum > 0 ){
								isPlayableStage = true;
							}
							
							if (isPlayableStage) {
								//ステージ指定
								Stage = NowCursorY + 1;
								
								//ゲーム開始
								StageStart( Difficult, Player, Stage );
							} else {
								PlaySE(seBuzzer);
								messageCount = 180;
							}
						}
				}
		}else if( GetKeyState( VK_BOMB ) == KEY_PUSH ){
			//現在のカーソル位置取得
			alternative( Cursor_Depth )
				case( 0 ){
					//Exitに移動
					NowCursorY = 4;
				}
				case( 1 ){
					//トップへ戻る
					//カーソル設定
					NowCursorX = Cursor1_X;
					NowCursorY = Cursor1_Y;
					Cursor2_X = 0;
					Cursor2_Y = 0;
					MaxCursorX = 0;
					MaxCursorY = 4;
					Cursor_Depth -= 1;
					//リザルトモード解除
					ResultMode = 0;
					ShowPlayerIndex = 0;
					ShowDifficultIndex = 0;
				}
				case( 2 ){
					alternative( Cursor1_Y )
						case( 0 ){
							//1.X.X プレイヤー選択(本編)
							//1.X   難易度選択(本編) へ戻る
							//カーソル設定
							NowCursorX = Cursor2_X;
							NowCursorY = Cursor2_Y;
							Cursor3_X = 0;
							Cursor3_Y = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth -= 1;
						}
						case( 1 ){
							//2.X.X プレイヤー選択(プラクティス)
							//2.X   難易度選択(プラクティス) へ戻る
							//カーソル設定
							NowCursorX = Cursor2_X;
							NowCursorY = Cursor2_Y;
							Cursor3_X = 0;
							Cursor3_Y = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth -= 1;
						}
						case( 3 ){
							alternative( Cursor2_Y )
								case( 2 ){
									//4.3.X データ消去確認
									//4.X   コンフィグ へ戻る
									//カーソル設定
									NowCursorX = Cursor2_X;
									NowCursorY = Cursor2_Y;
									Cursor3_X = 0;
									Cursor3_Y = 2;
									MaxCursorX = 0;
									MaxCursorY = 2;
									Cursor_Depth -= 1;
								}
						}
				}
				case( 3 ){
					alternative( Cursor1_Y )
						case( 1 ){
							//2.X.X.X ステージ選択(プラクティス)
							//2.X.X   プレイヤー選択(プラクティス) へ戻る
							//カーソル設定
							NowCursorX = Cursor3_X;
							NowCursorY = Cursor3_Y;
							Cursor4_X = 0;
							Cursor4_Y = 0;
							MaxCursorX = 0;
							MaxCursorY = 2;
							Cursor_Depth -= 1;
						}
				}
		}else if( GetKeyState( VK_UP ) == KEY_PUSH ){
			//上
			if( MaxCursorY != 0 ){
				NowCursorY -= 1;
				if( NowCursorY < 0 ){
					NowCursorY = MaxCursorY;
				}
			}
		}else if( GetKeyState( VK_DOWN ) == KEY_PUSH ){
			//下
			if( MaxCursorY != 0 ){
				NowCursorY += 1;
				if( NowCursorY > MaxCursorY ){
					NowCursorY = 0;
				}
			}
		}else if( GetKeyState( VK_LEFT ) == KEY_PUSH ){
			//左
			if( MaxCursorX != 0 ){
				NowCursorX -= 1;
				if( NowCursorX < 0 ){
					NowCursorX = MaxCursorX;
				}
			}
		}else if( GetKeyState( VK_RIGHT ) == KEY_PUSH ){
			//右
			if( MaxCursorX != 0 ){
				NowCursorX += 1;
				if( NowCursorX > MaxCursorX ){
					NowCursorX = 0;
				}
			}
		}
		
		//動作試験版仕様
		alternative( Cursor1_Y )
			case( 0, 1 ){
				//本編、プラクティス
				alternative( Cursor_Depth )
					case( 1 ){
						NowCursorY = 2;
					}
					case( 2 ){
						NowCursorY = 0;
					}
			}
		
		//現在のカーソル位置設定
		alternative( Cursor_Depth )
			case( 0 ){
				Cursor1_X = NowCursorX;
				Cursor1_Y = NowCursorY;
			}
			case( 1 ){
				Cursor2_X = NowCursorX;
				Cursor2_Y = NowCursorY;
			}
			case( 2 ){
				Cursor3_X = NowCursorX;
				Cursor3_Y = NowCursorY;
			}
			case( 3 ){
				Cursor4_X = NowCursorX;
				Cursor4_Y = NowCursorY;
			}
		
		//説明文セット
		alternative( Cursor1_Y )
			case( 0 ){
				//1.X 本編
				alternative( Cursor_Depth )
					case( 0 ){
						Menu_Exp = Exp_Start;
					}
					case( 1 ){
						Menu_Exp = Exp_Difficult;
					}
					case( 2 ){
						Menu_Exp = Exp_Player;
					}
			}
			case( 1 ){
				//2.X プラクティス
				alternative( Cursor_Depth )
					case( 0 ){
						Menu_Exp = Exp_Practice;
					}
					case( 1 ){
						Menu_Exp = Exp_Difficult;
					}
					case( 2 ){
						Menu_Exp = Exp_Player;
					}
					case( 3 ){
						if (messageCount > 0) {
							Menu_Exp = Exp_StageCantPlay;
							messageCount -= 1;
						} else {
							Menu_Exp = Exp_Stage;
						}
					}
			}
			case( 2 ){
				//3.X リザルト
				alternative( Cursor_Depth )
					case( 0 ){
						Menu_Exp = Exp_Result;
					}
					case( 1 ){
						Menu_Exp = "";
					}
			}
			case( 3 ){
				//4.X コンフィグ
				alternative( Cursor_Depth )
					case( 0 ){
						Menu_Exp = Exp_Config;
					}
					others{
						alternative( Cursor2_Y )
							case( 0 ){
								Menu_Exp = Exp_Config_Switch_1;
							}
							case( 1 ){
								Menu_Exp = Exp_Config_Switch_2;
							}
							case( 2 ){
								alternative( Cursor_Depth )
									case( 1 ){
										Menu_Exp = Exp_Config_Delete_1;
									}
									case( 2 ){
										Menu_Exp = Exp_Config_Delete_2;
									}
							}
					}
			}
			case( 4 ){
				//5.X 終了
				Menu_Exp = Exp_Exit;
			}
		
		yield;
	}
}

/**
 * タイトルの文字描画
 */
function TitleDraw(){
	//説明
	DrawText( Menu_Exp, 40, 360, 14, 255 );
	
	if( Cursor_Depth >= 0 ){
		//1階層目
		
		if( Cursor_Depth == 1 && Cursor1_Y == 2 ){
			//リザルト画面
			ResultDraw();
		}else{
			//リザルト画面以外は通常のメニュー表示
			
			//Game Start
			DrawText( "Game Start", 60, 30 + 30 * 0, 20, 255 );
			//Practice
			DrawText( "Practice", 60, 30 + 30 * 1, 20, 255 );
			//Result
			DrawText( "Result", 60, 30 + 30 * 2, 20, 255 );
			//Config
			DrawText( "Config", 60, 30 + 30 * 3, 20, 255 );
			//Exit
			DrawText( "Exit", 60, 30 + 30 * 4, 20, 255 );
			//第1階層カーソル
			DrawText( "☆", 34, 30 + 30 * Cursor1_Y, 20, 255 );
		}
	}
	if( Cursor_Depth >= 1 ){
		//2階層目
		
		alternative( Cursor1_Y )
			case( 0, 1 ){
				//1.X 難易度選択(本編)
				//2.X 難易度選択(プラクティス)
				
				//Easy
				DrawText( "Easy", 220, 30 + 30 * 0, 20, 255 );
				//Normal
				DrawText( "Normal", 220, 30 + 30 * 1, 20, 255 );
				//Panic
				DrawText( "Panic", 220, 30 + 30 * 2, 20, 255 );
				//第2階層カーソル
				DrawText( "☆", 194, 30 + 30 * Cursor2_Y, 20, 255 );
			}
			case( 3 ){
				//4.X コンフィグ
				
				//BackGround
				DrawText( "BackGround", 220, 30 + 30 * 0, 20, 255 );
				
				//ON／OFF
				if( GetIsDrawBackGround() ){
					DrawText( "ON", 360, 30 + 30 * 0, 20, 255 );
				}else{
					DrawText( "OFF",360, 30 + 30 * 0, 20, 255 );
				}
				
				//Clear Data
				DrawText( "Effect",     220, 30 + 30 * 1, 20, 255 );
				
				//ON／OFF
				if( GetIsDrawEffect() ){
					DrawText( "ON", 360, 30 + 30 * 1, 20, 255 );
				}else{
					DrawText( "OFF",360, 30 + 30 * 1, 20, 255 );
				}
				
				//Clear Data
				DrawText( "Clear Data", 220, 30 + 30 * 2, 20, 255 );
				//第2階層カーソル
				DrawText( "☆", 194, 30 + 30 * Cursor2_Y, 20, 255 );
			}
	}
	if( Cursor_Depth >= 2 ){
		//3階層目
		
		alternative( Cursor1_Y )
			case( 0, 1 ){
				//1.X.X プレイヤー選択(本編)
				//2.X.X プレイヤー選択(プラクティス)
				
				//Sunny Milk
				DrawText( "Sunny Milk",    220, 130 + 30 * 0, 20, 255 );
				//Luna Child
				DrawText( "Luna Child",    220, 130 + 30 * 1, 20, 255 );
				//Star Sapphire
				DrawText( "Star Sapphire", 220, 130 + 30 * 2, 20, 255 );
				//第3階層カーソル
				DrawText( "☆",            194, 130 + 30 * Cursor3_Y, 20, 255 );
			}
			case( 3 ){
				//4.X コンフィグ
				
				//Realy Delete?
				DrawText( "Realy Delete?", 240, 120 + 30 * 0, 20, 255 );
				//Yes
				DrawText( "Yes",           270, 120 + 30 * 1, 20, 255 );
				//No
				DrawText( "No",            270, 120 + 30 * 2, 20, 255 );
				//第3階層カーソル
				DrawText( "☆",            244, 120 + 30 * (Cursor3_Y + 1), 20, 255 );
			}
	}
	if( Cursor_Depth >= 3 ){
		//4階層目
		alternative( Cursor1_Y )
			case( 1 ){
				//2.X.X.X ステージ選択(プラクティス)
				
				//1
				DrawText( "Stage 1",  280, 240 + 30 * 0, 20, 255 );
				//2
				DrawText( "Stage 2",  280, 240 + 30 * 1, 20, 255 );
				//3
				DrawText( "Stage 3",  280, 240 + 30 * 2, 20, 255 );
				//EX
				DrawText( "Stage EX", 280, 240 + 30 * 3, 20, 255 );
				//第4階層カーソル
				DrawText( "☆",         254, 240 + 30 * Cursor4_Y, 20, 255 );
			}
	}
}
