function GameMainLoopSystem
{
	//-----------
	
	score=GetCommonData("Score");
	
	if(debug_mode){Debug_LSystem;}
	Extend_LSystem;
	Item_LSystem;
	State_LSystem;
	Timer_LSystem;
	
	
	SetCommonData("Score",score);
	
	//-----------
	
	BG_LSystem;
}


function Extend_LSystem
{
	if(itempoint_getcount>=extend_1up)
	{
		CreateEnemyFromFile(GetCurrentScriptDirectory~".\GameData\SystemData\SetExtendItem.txt",0,0,0,0,2);
		itempoint_getcount-=extend_1up;
		alternative(gamemode)
		case(0){extend_1up+=50;}
		case(1){extend_1up+=30;}
		case(2){extend_1up+=40;}
		case(3){extend_1up+=100;}
		SetEffect(1);
	}
	if(itemmulti_getcount>=extend_bomb)
	{
		CreateEnemyFromFile(GetCurrentScriptDirectory~".\GameData\SystemData\SetExtendItem.txt",0,0,0,0,1);
		itemmulti_getcount-=extend_bomb;
		SetEffect(0);
	}
	
	if(GetGraze>=graze_break)
	{
		if(!OnEnemySpell)
		{
			CreateEnemyFromFile(GetCurrentScriptDirectory~".\GameData\SystemData\SetExtendItem.txt",0,0,0,0,0);
			SetEffect(2);
		}
		AddGraze(-graze_break);
	}
	
	task SetEffect(let type)
	{
		PlaySE(se[SE_BREAK]);
		loop(80)
		{
			let sx=cenX;let sy=cenY;
			let angle=rand(0,360);
			let speed=rand(0,4);
			if(type==2){sx=GetPlayerX;sy=GetPlayerY;}
			
			BreakEffectObject(sx,sy,speed,angle,0);
		}	
	}
}

function Item_LSystem
{
	let itemstate=GetCommonDataDefault("ItemState",[]);
	while(length(itemstate)>0)
	{
		let data=itemstate[0];
		let x=data[0];
		let y=data[1];
		let type=data[2];
		SetItem(x,y,type);
		itemstate=erase(itemstate,0);
		SetCommonData("ItemState",itemstate);
	}
	
	if(OnPlayerMissed){itemrate_num-=7;}
	if(OnBomb&&gameloop_count%2){itemrate_num-=1;}
	if(!IsBossExisting)
	{
		if(multiply_delay>10000){multiply_delay=10000;}
		if(multiply_delay>1){multiply_delay=ceil(multiply_delay*0.98);}
		if(multiply_num>0)
		{
			multiply_num-=75/ceil(multiply_delay/60);
		}
		else
		{
			multiply_num=0;
			itemrate_num-=5;
		}
	}
	
	if(itemrate_num<1){itemrate_num=1;}
	if(itemrate_num>999){itemrate_num=999;}
	SetCommonData("Rate",itemrate_num);
	SetCommonData("ItemPoint",itempoint);
	
	if(GetKeyState(VK_SHOT)==KEY_HOLD){itemconv_delay=15;}
	if(itemconv_delay==0){ConvCircle;}
	
	if(!menu_flg)
	{
		let scp=0;
		if(itempoint>itempoint_prev){scp=0.08;}
		DrawBaseCount(itempoint,scp);
		
		scp=0;
		if(itemrate_num>itemrate_prev){scp=0.12;}
		DrawMultiplyCount(itemrate_num,scp);
		
		itempoint_prev=itempoint;
		itemrate_prev=itemrate_num;
	}
	itemconv_delay--;
}


function State_LSystem
{
	if(GetPlayerLife>=17){ExtendPlayer(-1);}
	if(GetPlayerBomb>=9){AddBomb(-1);}
	
	if(GetPlayerType!=USER_PLAYER)
	{
		if(GetKeyState(VK_SHOT)==KEY_HOLD||GetKeyState(VK_SHOT)==KEY_PUSH){ForbidShot(false);}
		else{ForbidShot(true);}
	}
	let hitcount=GetCommonDataDefault("EnemyHitCount",0);
	if(hitcount>0)
	{
		let rate=GetCommonDataDefault("Rate",1);
		score+=rate*rate*hitcount*floor(itempoint/300);
		multiply_num+=30*hitcount;
		multiply_delay+=2*hitcount;
	}
	SetCommonData("EnemyHitCount",0);
	
	let enemydelflg=GetCommonDataDefault("EnemyDelFlg",0);
	if(enemydelflg)
	{
		multiply_num=1000;
		multiply_delay+=200;
	}
	SetCommonData("EnemyDelFlg",0);
	
	let shotdelflg=GetCommonDataDefault("EnemyShotDelFlg",0);
	if(shotdelflg)
	{
		PlaySE(se[SE_SHOTDEL]);
		SetShotDelete;
		task SetShotDelete
		{
			loop(60)
			{
				DeleteEnemyShotToItem(ALL);
				yield;
			}
		}
	}
	SetCommonData("EnemyShotDelFlg",0);
	
	
	if(multiply_num>1000){multiply_num=1000;}
	
	if(IsBossExisting&&red_alpha>3){red_alpha-=2;}
	if(!IsBossExisting&&red_alpha<250){red_alpha+=2;}
	
	if(OnBomb&&red_alpha>10){red_num-=100;}
	
	if(red_num<0){red_num=0;}
	if(red_num>10000){red_num=10000;}
	SetCommonData("RED",red_num);
	
/*	
	let e_num=GetEnemyNum*30;
	if(IsBossExisting){e_num=GetEnemyNum*5;}
	if(GetEnemyShotCount+e_num>1200)
	{
		if(GetFps>40&&!IsReplay){Slow(1);}
	}
	else
	{
		Slow(0);
	}*/
	
	if(GetScore==0)
	{
		score=0;
		AddScore(1);
	}
	
	gameloop_count++;
}

function Timer_LSystem
{
	if(!(gameloop_count%60))
	{
		timer_seconds++;
	}
	
	if(timer_seconds>=60)
	{
		timer_seconds-=60;
		timer_minutes++;
	}
	if(timer_minutes>=60)
	{
		timer_minutes-=60;
		timer_hours++;
	}
}


function Debug_LSystem
{
	if(GetKeyState(VK_USER)==KEY_HOLD)
	{
		if(GetKeyState(VK_LEFT)==KEY_PUSH)
		{
			red_num-=2000;
		}
		if(GetKeyState(VK_RIGHT)==KEY_PUSH)
		{
			red_num+=2000;
		}
		if(GetKeyState(VK_UP)==KEY_PUSH)
		{
			itemrate_num+=200;
			multiply_num=1000;
			multiply_delay=10000;
		}
		if(GetKeyState(VK_DOWN)==KEY_PUSH)
		{
			itemrate_num-=200;
		}
		
		if(GetKeyState(VK_SHOT)==KEY_HOLD)
		{
			itempoint+=1000;
		}
		if(GetKeyState(VK_SLOWMOVE)==KEY_HOLD)
		{
			itempoint-=1000;
		}
		if(GetKeyState(VK_SKIP)==KEY_PUSH)
		{
			AddGraze(graze_break);
		}
	}
}