/***************************************************************************************

						メインファイル

****************************************************************************************/

#include "global.h"

#define	TITLE		0	
#define	TITLE_WAIT	1
#define	GAME_WAIT	2	
#define	GAME		3	
#define CLEAR		4
#define	DEAD		5	
#define	ENDING		6	
#define ROLL		7

int	Init(void);
int	get_rand(void);

int global_counter;	/*ゲームのモード*/
int stage_counter;
int life_counter;

int	key;			/*キーの情報を格納*/
int	i,j;
int	continue_flag;

/*メイン関数*/
void main(void)
{
	if(Init())	return;


	while(key != KEY_START)
	{

		switch(global_counter)
		{
			case	TITLE:
				title_draw();
				global_counter=TITLE_WAIT;
				sound_play(1);
				break;

			case	TITLE_WAIT:
				key=key_hit_check();
				
				if(key!=0&&key!=KEY_START&&key!=KEY_A)
				{
					global_counter=GAME_WAIT;
					
					for(i=0;i<9;i++)
						for(j=0;j<14;j++)
							stage[i][j]=map[stage_counter][i][j];

					sprite_setup();
					game_setup();
					if(stage_counter<4)	sound_play(0);
					else				sound_play(4);
				}
				break;

			case	GAME_WAIT:

				if(key==KEY_X2||key==KEY_Y3)
				{
					global_counter=GAME;
				}
				game_wait(key);
				draw();
				break;

			case	GAME:
				i=game(key);
				if(i==1){
					if(stage_counter>=4)	global_counter=ENDING,sound_stop();
					else					global_counter=CLEAR,sound_play(2);
					movie_init();
				}
				if(i==2){
					sound_stop();
					global_counter=DEAD;
					movie_init();
					sound_play(3);
				}
				draw();
				break;
			
			case	CLEAR:
				key=key_hit_check();
				if(key!=KEY_START&&key!=0&&key!=KEY_A)
				{
					if(movie_count>10)
					{
						global_counter=GAME_WAIT;
						stage_counter++;
						for(i=0;i<9;i++)
							for(j=0;j<14;j++)
								stage[i][j]=map[stage_counter][i][j];

						sprite_setup();
						game_setup();
						if(stage_counter<4)	sound_play(0);
						else				sound_play(4);
					}
				}
				else if(clear(key))
				{
					global_counter=GAME_WAIT;
					stage_counter++;
					for(i=0;i<9;i++)
						for(j=0;j<14;j++)
							stage[i][j]=map[stage_counter][i][j];

					sprite_setup();
					game_setup();
					sound_play(0);
				}

				break;

			case	DEAD:
			/*	key=key_hit_check();*/
				
				continue_flag=dead(key);
				if(continue_flag==1)
				{
					global_counter=GAME_WAIT;
					for(i=0;i<9;i++)
						for(j=0;j<14;j++)
							stage[i][j]=map[stage_counter][i][j];

					ship.flag=5;
					sprite_setup();
					game_setup();
					if(stage_counter<4)	sound_play(0);
					else				sound_play(4);
				}
				else if(continue_flag==2)
				{
					sprite_setup();
					global_counter=TITLE;
					stage_counter=0;
					ship.flag=5;
					draw_init();
				}
				
				break;
		
			case	ENDING:
				
				ending();

				if(movie_count>360)
				{
					global_counter=ROLL;
					movie_init();
				}					

				break;
		
			case	ROLL:

				roll();
				
				break;
		}	


		key=key_press_check();
		life_counter++;
		if(key==KEY_A&&life_counter>30)
		{
			sprite_setup();
			global_counter=TITLE;
			stage_counter=0;
			ship.flag=5;
			draw_init();
		}

		sys_wait(5);	

	}

	wwc_set_color_mode(COLOR_MODE_GRAYSCALE);

	sound_function_end();
}


int	Init(void)
{
	if(sound_function())	return	1;
	
	if(draw_init())			return	1;

	movie_init();
	global_counter=TITLE;
	stage_counter=0;

	ship.flag=5;

	life_counter=0;

	return	0;
}

int	get_rand(void)
{

	sound_set_channel(0x88);
	sound_set_noise(0x10);

	return	sound_get_random();

}
