
//スフィアの挙動

let obj_sphere;
let m_sphereX;
let m_sphereY;
let m_sphere_radius;
let m_sphere_state;
let m_sphere_speed;
let m_sphere_brake;
let m_reflection_angle;
let m_reflection_power;

//ローリング
let m_sphere_angle;
let m_sphere_chain;
let m_sphere_roll_speed;
let m_sphere_roll_direction;


let m_sphere_chain_min;
let m_sphere_chain_max;
let m_sphere_roll_speed_max;
let m_flag_having_sphere;
let m_flag_crash;
let m_flag_combo_countstop;
let m_score_rate;
let m_score_combo_time;
let m_score_combo_counter;
let m_sphere_stone_num;

let _SPHERE_STOP = 0;
let _SPHERE_CHARGE = 1;
let _SPHERE_MOVESTART = 2;
let _SPHERE_MOVE = 3;
let _SPHERE_ROLLING = 5;
let _SPHERE_FOLLOW = 6;
let _SPHERE_FREE = 4;

let default_SphereX = GetCenterX();
let default_SphereY = GetCenterY();
let default_SphereSpeed = 15;
let default_SphereBrake = 0.3;
let default_ChainLengthMin = 50;
let default_ChainLengthMax = 32;
let default_RollSpeedMax = 6.5;
//let default_RollSpeedMax = 5.5;

let const_SphereClipMinX = GetClipMinX()-60;
let const_SphereClipMaxX = GetClipMaxX()+60;
let const_SphereClipMinY = GetClipMinY()-60;
let const_SphereClipMaxY = GetClipMaxY()+60;
let const_SphereRadius = [10, 14, 20, 28, 32, 44 ];
//let const_SphereSpeedMax = [15, 14, 13.5, 12, 10.5, 9];
let const_SphereSpeedMax = [16, 15, 14, 12.5, 11, 9];
let const_SphereBrake = [0.3, 0.25, 0.2, 0.15, 0.1, 0.05];
let const_SphereReflectionVal = 0.6;
let const_SphereReflectionRollSpeed = default_RollSpeedMax/5;
let const_SphereReflectionChargeAngle = default_RollSpeedMax/2;
let const_SphereCatchRadius = 15;
let const_SphereWeightUnit = [0, 64*1, 64*3, 64*7, 64*12, 64*16, 64*24];
let const_ScoreRateUnit = 10;

//スフィア生成
function once_system_CreateSphere(){
//CreateDebugWindow();

	obj_sphere = REMY_CreateEffectObject(img_SphereRender, 4, 6);
	REMY_SetObjectVertexSquare(obj_sphere, [0,0,256,256], 0, 0, 255, 255, 255, 255);
	ObjEffect_SetRenderState(obj_sphere,ALPHA);
	m_sphereX = default_SphereX;
	m_sphereY = default_SphereY;
	system_SphereLocationToAngle();
	m_sphere_speed = 0;
	m_sphere_brake = default_SphereBrake;
	m_sphere_state = _SPHERE_STOP;
	m_sphere_chain_min = default_ChainLengthMin;
	m_sphere_chain_max = default_ChainLengthMax;
	m_sphere_roll_speed = 0;
	m_sphere_roll_speed_max = default_RollSpeedMax;
	m_sphere_roll_direction = 1;
	m_sphere_radius = const_SphereRadius[0];
	m_sphere_stone_num = 0;
	m_score_rate = 10;
	m_score_combo_time = 0;
	m_score_combo_counter = 0;
	m_reflection_angle = NULL;
	m_reflection_power = 0;
	m_flag_having_sphere = true;
	m_flag_crash = false;
	m_flag_combo_countstop = false;
	Obj_SetPosition(obj_sphere, m_sphereX, m_sphereY);

}//end 

//スフィアのメイン---------------------------------------------
task once_system_TaskSphereMain(){
//CreateDebugWindow();
	system_TaskSphereDraw();
	system_TaskSphereFollow();
	system_TaskSpherePull();
	system_TaskSphereRolling();
	system_TaskSphereCrash();
	system_TaskSphereLevelCheck();

	//ステートの変更
	loop{
		while(system_IsGameOver()){yield;}
//OutputDebugString(2, "state", m_sphere_state);

		//クラッシュ
		if(g_Stick[_KEY_BOMB] && !m_flag_crash){
			m_flag_crash = true;
		}

		//スフィア保持状態
		if(m_flag_having_sphere){
			
			if(g_Stick[_KEY_SHOT] && m_sphere_state != _SPHERE_ROLLING){
				m_sphere_state = _SPHERE_ROLLING;
				system_TaskSphereChargeRolling();
			}else if(m_sphere_state ==_SPHERE_ROLLING){
				if(!g_Stick[_KEY_SHOT]){
					m_sphere_state = _SPHERE_MOVESTART; 
					m_sphere_roll_speed = 0;
					m_flag_having_sphere = false;
				}
			}else{
				m_sphere_state = _SPHERE_FOLLOW;
			}
		}

		//スフィア非保持状態
		else{

			if(g_Stick[_KEY_SHOT] && m_sphere_state != _SPHERE_CHARGE ){
				m_sphere_state = _SPHERE_CHARGE;
				system_TaskSphereCharge();
			}else if(m_sphere_state == _SPHERE_CHARGE){
				if(!g_Stick[_KEY_SHOT]){ m_sphere_state = _SPHERE_MOVESTART; }
				else if(system_IsIntersectWithSphere(GetPlayerX(), GetPlayerY(), m_sphere_radius + const_SphereCatchRadius)){
					system_SphereLocationToAngle();
					m_flag_having_sphere = true;
					sphere_catch();
				}

			}else if(m_sphere_state != _SPHERE_MOVE){
				if(system_IsIntersectWithSphere(GetPlayerX(), GetPlayerY(), m_sphere_radius + const_SphereCatchRadius)){
					system_SphereLocationToAngle();
					m_flag_having_sphere = true;
					sphere_catch();
				}
			}

			if(m_sphere_state == _SPHERE_MOVE && m_sphere_speed == 0 ){
				m_sphere_state = _SPHERE_STOP;
			}

		}

		Obj_SetPosition(obj_sphere, m_sphereX, m_sphereY);

		m_sphere_radius = const_SphereRadius[ system_GetSphereLevel() ];
		m_sphere_chain_min = default_ChainLengthMin + default_ChainLengthMax * system_GetSphereLevel()/5;
		m_reflection_angle = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_REFLECTION", NULL);
		m_reflection_power = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_REFLECTION_POWER", 0);
		local{
			let shot = GetEnemyShotCountEx( m_sphereX, m_sphereY, m_sphere_radius-2, ALL);
			if(shot>0){
				PlaySE(se_Reflect);
				system_AddWeight(-(system_GetSphereLevel()*2+3) * shot);
				loop(system_GetSphereLevel() * shot){
					system_TaskWeightItemLost(system_GetSphereX(), system_GetSphereY(), rand(0,360));
				}
				system_AddScoreRate(-3);
				DeleteEnemyShotImmediatelyInCircle(ALL, m_sphereX, m_sphereY, m_sphere_radius-2);
			}
		}

/*		local{
			let ref_ang = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_REFLECTION", NULL);
			if(ref_ang != NULL){ 
				m_reflection_angle = ref_ang;
			}
		}
*/

		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION", NULL);
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION_POWER", 0);
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_POSITION", [m_sphereX, m_sphereY]);
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_RADIUS", m_sphere_radius);
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_STATE", m_sphere_state);
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_LEVEL", system_GetSphereLevel());
		yield;

	}


	task sphere_catch(){

		let obj = REMY_CreateEffectObject(img_Text, 4, 6);
		let rect = system_GetGraphicRect("SPHERE_CATCH");
		REMY_SetObjectVertexSquare(obj, rect, 0, 0, 200, 80, 205, 85);
		Obj_SetPosition(obj, GetPlayerX(), GetPlayerY());

		ascent(i in 0..50){
			Obj_SetPosition(obj, Obj_GetX(obj), Obj_GetY(obj)-0.6);
			ObjEffect_SetScale(obj, 1 + 0.2*(i/50), 1 + 0.2*(i/50));
			REMY_SetObjectColorSquare(obj, 220*(1-(i/50)), 220, 55, 225);

			yield;
		}
		Obj_Delete(obj);

	}//end task

}//end task


//スフィアを引きずる
task system_TaskSphereFollow(){

	let old_x = m_sphereX;
	let old_y = m_sphereY;
	let old_angle = m_sphere_angle;

	loop{

		system_SphereLocationToAngle();
		//力がかかっている
		if(m_sphere_chain > m_sphere_chain_min){

			let to_angle = REMY_GetAngleLimited( atan2(old_y-GetPlayerY(), old_x-GetPlayerX()) );
			let angle_gap = (to_angle - old_angle);

			if(REMY_GetAngleLimited(angle_gap) > 2 && REMY_GetAngleLimited(angle_gap) < 358){
				m_sphere_roll_speed = REMY_GetNumLimited( m_sphere_roll_speed + (REMY_GetAngleLimited(angle_gap+180)-180)/2, - m_sphere_roll_speed_max,  m_sphere_roll_speed_max, false);
			}

			m_sphere_roll_direction = REMY_GetSign(m_sphere_roll_speed);
			m_sphere_chain = m_sphere_chain_min;

		//力がかかってない
		}else{
			if(m_sphere_roll_speed == 0){
				m_sphere_chain = system_GetDistanceToSphere(GetPlayerX(), GetPlayerY());
			}
			//system_SetSphereX(m_sphereX + m_sphere_speed * cos(m_sphere_angle+180));
			//system_SetSphereY(m_sphereY + m_sphere_speed * sin(m_sphere_angle+180));
			//m_sphere_speed = REMY_GetNumMax(m_sphere_speed, 0);
		}

		while(!m_flag_having_sphere){yield;}
		old_x = m_sphereX;
		old_y = m_sphereY;
		old_angle = m_sphere_angle;
		yield;
	}

}//end task



//スフィアを引っ張る
task system_TaskSpherePull(){

	while(m_flag_having_sphere){yield;}

	loop{
		while(system_IsGameOver()){yield;}

		system_SphereLocationToAngle();
		if( !system_IsIntersectWithSphere(GetPlayerX(), GetPlayerY(), m_sphere_radius + const_SphereCatchRadius)){
			m_sphere_chain = (m_sphere_chain-[2,7][g_Stick[_KEY_SLOW]]);
		}
		check_reflection();
		if( m_sphere_roll_speed !=0 ){
			m_sphere_angle = REMY_GetAngleLimited(m_sphere_angle + m_sphere_roll_speed);
			m_sphere_roll_speed = m_sphere_roll_direction * REMY_GetNumMax( absolute(m_sphere_roll_speed) - 0.1, 0);
		}
		system_SphereAngleToLocation();

		while(m_flag_having_sphere){yield;}
		yield;
	}

	//跳ね返り判定
	function check_reflection(){
		if(m_reflection_angle == NULL){ return; }
		m_sphere_roll_speed = m_sphere_roll_speed + sin(m_reflection_angle - m_sphere_angle) * const_SphereReflectionRollSpeed * m_reflection_power;
		if( absolute(m_sphere_roll_speed)>15 ){ m_sphere_roll_speed = 15 * REMY_GetSign(m_sphere_roll_speed);}
		m_sphere_roll_direction = REMY_GetSign(m_sphere_roll_speed);
		m_sphere_chain = m_sphere_chain + cos(m_reflection_angle-m_sphere_angle)+4;

		//m_sphere_angle = REMY_GetAngleLimited(m_sphere_angle + sin(m_reflection_angle-m_sphere_angle) * const_SphereReflectionChargeAngle * m_reflection_power);

	}

}//end task



//スフィアまわす
task system_TaskSphereRolling(){

	PlaySwingSE();
	loop{
		while(system_IsGameOver()){yield;}

		check_reflection();

		if( m_sphere_roll_speed !=0 ){
			m_sphere_angle = REMY_GetAngleLimited(m_sphere_angle + m_sphere_roll_speed);
			m_sphere_roll_speed = m_sphere_roll_direction * REMY_GetNumMax( absolute(m_sphere_roll_speed) - 0.1, 0);
		}
		system_SphereAngleToLocation();

		yield;
		while(!m_flag_having_sphere){yield;}
	}

	//跳ね返り判定
	function check_reflection(){
		if(m_reflection_angle == NULL){ return; }
		m_sphere_roll_speed = m_sphere_roll_speed + sin(m_reflection_angle - m_sphere_angle) * const_SphereReflectionRollSpeed * m_reflection_power;
		if( absolute(m_sphere_roll_speed)>15 ){ m_sphere_roll_speed = 15 * REMY_GetSign(m_sphere_roll_speed);}
		m_sphere_roll_direction = REMY_GetSign(m_sphere_roll_speed);

	}

	task PlaySwingSE(){
		loop{
			while(system_IsGameOver()){yield;}

			if(absolute(m_sphere_roll_speed) == m_sphere_roll_speed_max){
				PlaySE(se_Swing);
				loop(45){yield;}
			}
			yield;
		}
	}

}//end task


//ローリングチャージ
task system_TaskSphereChargeRolling(){

	let max_power = 60;
	let move_angle = m_sphere_angle + 90 * m_sphere_roll_direction;
	let move_power = (m_sphere_speed / default_SphereSpeed) * 15 + 15;

	DrawMarker();


	local{
		while(m_sphere_state == _SPHERE_ROLLING){
			
			//if(m_sphere_roll_speed ==0 ){return;}
			if(m_sphere_roll_direction == 0){m_sphere_roll_direction = 1;}
			let accel = REMY_GetNumMax( 0.8 / ((system_GetSphereLevel() + 1)), 0.05);
			m_sphere_roll_speed = REMY_GetNumLimited(m_sphere_roll_speed + m_sphere_roll_direction * accel, - m_sphere_roll_speed_max,  m_sphere_roll_speed_max, false);
			m_sphere_chain = REMY_GetNumMin( m_sphere_chain + 1, m_sphere_chain_min);

			move_power = REMY_GetNumMin(max_power * (absolute(m_sphere_roll_speed)/ m_sphere_roll_speed_max), max_power);
			move_angle = m_sphere_angle + 90 * m_sphere_roll_direction;
			yield;
		}
		system_TaskSphereMove(move_angle, move_power/max_power);
	}



	//マーカ描画
	task DrawMarker(){

		let obj_frame = REMY_CreateEffectObject(img_Sphere, 4, 5);
		REMY_SetObjectVertexSquare(obj_frame, system_GetGraphicRect("SPHERE_MARKER_FRAME"),-48,0,255,255,255,255);
		ObjEffect_SetScale(obj_frame, 1, 0.6);


		let obj = REMY_CreateEffectObject(img_Sphere, 4, 5);
		let counter = 0;
		ObjEffect_SetAngle(obj,0,0,move_angle);
		ObjEffect_SetAngle(obj_frame,0,0,move_angle);

		while(m_sphere_state == _SPHERE_ROLLING){
			REMY_SetObjectVertexSquare(obj, system_GetGraphicRect(["SPHERE_MARKER01","SPHERE_MARKER02","SPHERE_MARKER03"][trunc(counter/6)%3]),-48,0,150,255,255,0);
			if(move_angle != NULL){
				Obj_SetPosition(obj, m_sphereX + m_sphere_radius * cos(move_angle), m_sphereY+ m_sphere_radius*sin(move_angle));
				ObjEffect_SetScale(obj, move_power/max_power, 0.6);
				ObjEffect_SetAngle(obj,0,0,move_angle);

				Obj_SetPosition(obj_frame, m_sphereX + m_sphere_radius*cos(move_angle), m_sphereY+ m_sphere_radius*sin(move_angle));
				ObjEffect_SetAngle(obj_frame,0,0,move_angle);
			}
			counter++;
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj_frame);
	}

}//end task

//スフィアチャージ
task system_TaskSphereCharge(){

	let max_power = 60;
	let move_angle = NULL;
	let move_power = (m_sphere_speed / default_SphereSpeed) * 15 + 15;

	DrawMarker();
	
	local{
		

		while(m_sphere_state == _SPHERE_CHARGE){
			if(move_angle != NULL){
				move_power = REMY_GetNumMin((move_power+1), max_power);
			}
			DirectionChagne();
			yield;
		}
		if(m_sphere_state == _SPHERE_ROLLING){ move_angle = NULL; }
		if(move_angle != NULL){
			system_TaskSphereMove(move_angle, move_power/max_power);
		}
	}

	//方向変更
	function DirectionChagne(){
		let to_angle = 0;
		if(REMY_GetStickX() !=0 || REMY_GetStickY() !=0){
			to_angle = REMY_GetAngleLimited( atan2(REMY_GetStickY(), REMY_GetStickX()) );
			if(move_angle == NULL){ move_angle = to_angle; return; }

			let check_angle = REMY_GetAngleLimited(to_angle-move_angle);
			if(check_angle > 135 && check_angle < 225){ move_angle = to_angle; return; }
			else if(check_angle < 3 || check_angle > 357){move_angle = to_angle; return;}
			else{move_angle += 4*[-1,1][check_angle<180];}

		}
	}

	//マーカ描画
	task DrawMarker(){
		let obj_frame = REMY_CreateEffectObject(img_Sphere, 4, 5);
		REMY_SetObjectVertexSquare(obj_frame, system_GetGraphicRect("SPHERE_MARKER_FRAME"),-48,0,255,255,255,255);
		ObjEffect_SetScale(obj_frame, 1, 0.6);

		let obj = REMY_CreateEffectObject(img_Sphere, 4, 5);
		let counter=0;
		while(m_sphere_state == _SPHERE_CHARGE){
			let is_roll = (m_sphere_state == _SPHERE_ROLLING);

			REMY_SetObjectVertexSquare(obj, system_GetGraphicRect(["SPHERE_MARKER01","SPHERE_MARKER02","SPHERE_MARKER03"][trunc(counter/6)%3]),-48,0,150,[100, 255][is_roll],255,0);
			if(move_angle != NULL){
				Obj_SetPosition(obj, m_sphereX + m_sphere_radius * cos(move_angle), m_sphereY+ m_sphere_radius*sin(move_angle));
				ObjEffect_SetScale(obj,move_power/max_power, 0.6);
				ObjEffect_SetAngle(obj,0,0,move_angle);

				Obj_SetPosition(obj_frame, m_sphereX + m_sphere_radius*cos(move_angle), m_sphereY+ m_sphere_radius*sin(move_angle));
				ObjEffect_SetAngle(obj_frame,0,0,move_angle);

			}
			counter++;
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj_frame);
	}

}//end function


//スフィア移動
task system_TaskSphereMove(let angle, let power_rate){

	//let max_speed = default_SphereSpeed * power_rate - REMY_GetNumMin(0.2 * system_GetSphereLevel(), 4);
	let max_speed = const_SphereSpeedMax[system_GetSphereLevel()] * power_rate;
	//let accel = REMY_GetNumMax( default_SphereSpeed / (system_GetSphereLevel() + 1), 0.1);
	let accel = REMY_GetNumMax( max_speed / (system_GetSphereLevel()/2 + 1), 0.1);
	let speed = accel ;
 
	//let brake = REMY_GetNumMax( default_SphereBrake/ (system_GetSphereLevel() + 1), 0.1);
	let brake = const_SphereBrake[system_GetSphereLevel()];
	let flag_fullspeed = false;
	m_sphere_state = _SPHERE_MOVE;


	PlaySE(se_Move);

	local{
		system_MoveCircle(m_sphereX, m_sphereY, angle );
		while( speed > 0 && (m_sphere_state != _SPHERE_MOVESTART)){
//		while( speed > 0 ){
			check_reflection();
			m_sphereX = REMY_GetNumLimited( m_sphereX + speed * cos(angle), const_SphereClipMinX, const_SphereClipMaxX, false);
			m_sphereY = REMY_GetNumLimited( m_sphereY + speed * sin(angle), const_SphereClipMinY, const_SphereClipMaxY, false);
			speed = (speed - brake);
			if(!flag_fullspeed){
				if((speed + accel) > max_speed){ flag_fullspeed = true; }
				speed = REMY_GetNumMin(speed + accel, max_speed);
			}
			m_sphere_speed = REMY_GetNumMax(speed, m_sphere_speed);
			yield;
		}
		m_sphere_speed = 0;
	}

	//跳ね返り判定
	function check_reflection(){
		if(m_reflection_angle == NULL){ return; }
		angle = REMY_GetAngleLimited(angle + sin(m_reflection_angle-angle) * const_SphereReflectionChargeAngle * m_reflection_power);
		speed = REMY_GetNumMin(  speed + ((m_sphere_speed/4) * m_reflection_power * cos(m_reflection_angle-angle)), 15);
		if(speed < 0){
			angle = REMY_GetAngleLimited(2 * REMY_GetAngleLimited(m_reflection_angle+90) - angle );
			speed = REMY_GetNumMin( absolute(speed) * const_SphereReflectionVal, 15);
		}
		
	}

}//end function


//クラッシュ！
task system_TaskSphereCrash(){

	loop{

		while(m_flag_crash){yield;}
		while(!m_flag_crash){yield;}
		if(m_flag_crash && system_GetWeight() >= const_SphereWeightUnit[1]){ Crash(); }
		yield;
		m_flag_crash = false;

	}

	function Crash(){


		Slow(2);
		local{
			PlaySE(se_Move);
			Magic();
//			let check = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_CRASH",[]);
//			check = check ~ [[m_sphereX, m_sphereY ]];
//			SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_CRASH",check);
			loop(50){ system_TaskWeightItemLost(m_sphereX, m_sphereY, m_sphere_angle+rand(-90,90));}
		}

		loop(10){yield;}
		Slow(0);
//		system_AddWeight(-trunc((const_SphereWeightUnit[system_GetSphereLevel()+1]-const_SphereWeightUnit[system_GetSphereLevel()])/2));
		system_AddWeight(-trunc(system_GetWeight()/2));

		local{
			let num_min = 2 + system_GetSphereLevel();
			let num_max = 6 + system_GetSphereLevel()*3;
			ascent(i in num_min..num_max){
				PlaySE(se_Crash);
				let check = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_CRASH",[]);
				check = check ~ [[m_sphereX + (i * 10) *cos(360 * i/10), m_sphereY + (i * 10) *sin(360 * i/10) ]];
				check = check ~ [[m_sphereX + (i * 10) *cos(360 * i/10 + 90), m_sphereY + (i * 10) *sin(360 * i/10 + 90) ]];
				check = check ~ [[m_sphereX + (i * 10) *cos(360 * i/10 + 180), m_sphereY + (i * 10) *sin(360 * i/10 + 180) ]];
				check = check ~ [[m_sphereX + (i * 10) *cos(360 * i/10 + 270), m_sphereY + (i * 10) *sin(360 * i/10 + 270) ]];
				SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_CRASH",check);
				loop(20){ system_TaskWeightItemLost(m_sphereX, m_sphereY, m_sphere_angle+rand(-90,90));}
				loop(5){ yield; }
			}
		}

		loop(30){yield;}

//		loop(const_SphereWeightUnit){
//			system_TaskWeightItemLost(m_sphereX, m_sphereY, m_sphere_angle+rand(-90,90));
//		}

	}//end function

	task Magic(){
		let obj = REMY_CreateEffectObject(img_Magic, 4, 6);
		let rect = system_GetGraphicRect("MAGIC");
		REMY_SetObjectVertexSquare(obj, rect, 0, 0, 200, 180, 55, 185);

		ascent(i in 0..20){
			Obj_SetPosition(obj, m_sphereX, m_sphereY);
			ObjEffect_SetAngle(obj, 0, 0, 200 * (i/20));
			ObjEffect_SetScale(obj, (i/20),(i/20));
			REMY_SetObjectColorSquare(obj, 200*(1-(i/20)), 180, 55, 185);
			yield;
		}
		Obj_Delete(obj);
	}


}//end function

task system_TaskSphereLevelCheck(){

	let old_level = system_GetSphereLevel();

	loop{
		if(system_GetSphereLevel() > old_level){PlaySE(se_LvUp);}
		else if(system_GetSphereLevel() < old_level){PlaySE(se_LvDown);}
		old_level = system_GetSphereLevel();
		loop(5){yield;}
	}

}//end function


//スコアレート
//停止時はレート低下、離れていて速いほどレート上昇率高い
task system_TaskScoreRateCheck(){

	loop{
		while(system_IsGameOver()){yield;}
		while(m_flag_combo_countstop){yield; }

		m_score_combo_time = REMY_GetNumMax(m_score_combo_time-1 , 0);
		if(m_score_combo_time == 0){
			m_score_rate = REMY_GetNumMax(m_score_rate-2 , const_ScoreRateUnit * 1);
			m_score_combo_counter = REMY_GetNumMax(m_score_combo_counter-5 , 0);
		}
//		else if(m_sphere_roll_speed ==0 && m_sphere_speed ==0){
//			m_score_combo_time = REMY_GetNumMax(m_score_combo_time-1 , 0);
//			m_score_rate = REMY_GetNumMax(m_score_rate-1 , const_ScoreRateUnit * 1);
//		}

//DEBUG_STRING = ToString(m_score_combo_counter/10);
		yield;
	}

}//end task

//移動円
task system_MoveCircle(let x, let y, let angle){

	let obj = REMY_CreateEffectObject(img_Burst, 4, 5);
	ObjEffect_SetScale(obj, 0.2, 0.8);
	ObjEffect_SetAngle(obj, 0, 0, angle);
	Obj_SetPosition(obj, x, y);
	Obj_SetSpeed(obj, 2);
	Obj_SetAngle(obj, angle+180);

	ascent(i in 0..8){
		let rect = system_GetGraphicRect(["BURST_01","BURST_02","BURST_03","BURST_04","BURST_05","BURST_06","BURST_07","BURST_08"][i]);
		REMY_SetObjectVertexSquare(obj, rect, 0, 0, 50 + 150 * (1-i/8), 255, 255, 255);
		loop(3){ yield; }
	}
	Obj_Delete(obj);

}//end task


//スフィア描画
task system_TaskSphereDraw(){

	let counter = 0;

	DrawChain();
	DrawSphere();
	loop(5){
		DrawShadow();
		loop(5){yield;}
	}

	task DrawSphere(){


		loop{

			if(m_sphere_stone_num != system_GetSphereStoneNum()){
				m_sphere_stone_num = system_GetSphereStoneNum();

				ClearRenderTarget(img_SphereRender);
				let old_target = SetRenderTarget(img_SphereRender);
				local{
					SetTexture(img_Sphere);
					system_SetGraphicRect("SPHERE_MAIN01");
					let scale = 10/64; //m_sphere_radius/64 ;
					SetGraphicScale(scale, scale);
					DrawGraphic(128,128);
				}
				local{

					SetGraphicScale(1,1);
					system_SetGraphicRect("SPHERE_STONE");
					let num = system_GetSphereStoneNum();
					let lay = trunc((num ^0.5)+1);
					let max_lay = lay;
					let min = (max_lay*max_lay)-num;
					descent(i in min..(max_lay*max_lay)){
						if( i < (lay*lay)){ lay = REMY_GetNumMax(lay-1, 2); }
						let rad = (m_sphere_radius) * (lay/max_lay);
						let ang = (i * 43) %360;
						DrawGraphic(128 + rad * cos(ang), 128 + rad * sin(ang));
					}
				}

				local{
					SetTexture(img_CountNum);
					system_SetGraphicRect(["NUM_01TON","NUM_02TON","NUM_04TON","NUM_08TON","NUM_12TON","NUM_16TON"][system_GetSphereLevel()]);
					let scale = 0.5+(system_GetSphereLevel()/4);
					SetGraphicScale(scale, scale);
					DrawGraphic(128 ,128);

				}

				SetRenderTarget(old_target);
			}
			loop(5){ yield; }
		}
	}

	task DrawShadow(){
		let obj = REMY_CreateEffectObject(img_SphereRender, 4, 4);
		ObjEffect_SetScale(obj, 1.05,1.05);
		REMY_SetObjectVertexSquare(obj, [0, 0, 255, 255], 0, 0, 200, 55, 55, 255);
		loop{
			Obj_SetPosition(obj, m_sphereX, m_sphereY);
			descent(i in 1..10){
				REMY_SetObjectColorSquare(obj, 60 + 100 * (i/10), 55, 55, 55);
				loop(2){ yield; }
			}
		}
		Obj_Delete(obj);
	}

	task DrawChain(){
		let obj = REMY_CreateEffectObject(img_Sphere, 4, 4);
		//ObjEffect_SetRenderState(obj, ALPHA);
		let rect = system_GetGraphicRect("SPHERE_CHAIN01");
		
		loop{
			system_SphereLocationToAngle();
			let scale = 1 + system_GetSphereLevel()/5;
			ObjEffect_SetScale(obj, 1, scale);
			REMY_SetObjectVertexSquare(obj, [rect[0], rect[1], m_sphere_chain ,rect[3]], -m_sphere_chain/2, 0, 200, 255, 255, 255);
			ObjEffect_SetAngle(obj, 0,0,m_sphere_angle);
			Obj_SetPosition(obj, GetPlayerX(), GetPlayerY());
			yield;
		}
	}



}//end task

//スフィア位置と角度の変換
function system_SphereAngleToLocation(){
	m_sphereX = GetPlayerX() + m_sphere_chain * cos(m_sphere_angle);
	m_sphereY = GetPlayerY() + m_sphere_chain * sin(m_sphere_angle);
}
function system_SphereLocationToAngle(){
	let dist_x = m_sphereX - GetPlayerX();
	let dist_y = m_sphereY - GetPlayerY();
	m_sphere_angle = REMY_GetAngleLimited(atan2(dist_y,dist_x));
	m_sphere_chain = (dist_x * dist_x + dist_y * dist_y)^0.5;

}


//スフィアレベル
function system_GetSphereLevel(){
	descent(i in 0..6){
		if(system_GetWeight()>=const_SphereWeightUnit[i]){return i}
	}
	return 0;
}
function system_GetSphereLevelExp(){
	descent(i in 0..6){
		if(const_SphereWeightUnit[i]-system_GetWeight()>0){return const_SphereWeightUnit[i]-system_GetWeight();}
	}
	return const_SphereWeightUnit[6];
}

function system_GetSphereStoneNum(){
	return  trunc(system_GetWeight()/5);

}

//方向取得
function system_GetAngleToSphere(let x, let y){
	return REMY_GetAngleLimited( atan2(m_sphereY-y, m_sphereX-x) );
}

//距離取得
function system_GetDistanceToSphere(let x, let y){

	let dist_x = m_sphereX-x;
	let dist_y = m_sphereY-y;
	return (dist_x * dist_x + dist_y * dist_y)^0.5

}
//重なっているか判定
function system_IsIntersectWithSphere(let x, let y, let dist){

	let dist_x = m_sphereX-x;
	let dist_y = m_sphereY-y;
	return (dist_x * dist_x + dist_y * dist_y < dist * dist)
	 
}


//アクセサ
function system_SetSphereX(let val){
	m_sphereX = REMY_GetNumLimited( val, const_SphereClipMinX, const_SphereClipMaxX, false);
}
function system_SetSphereY(let val){
	m_sphereY = REMY_GetNumLimited( val, const_SphereClipMinY, const_SphereClipMaxY, false);
}
function system_GetSphereX(){return m_sphereX;}
function system_GetSphereY(){return m_sphereY;}

function system_GetScoreRate(){return REMY_GetNumMin(trunc(m_score_rate/const_ScoreRateUnit), 16);}
function system_AddScoreRate(let val){m_score_rate = REMY_GetNumMin(m_score_rate + val, const_ScoreRateUnit * 16);}
function system_SetScoreCombo(let val){m_score_combo_time = val;}
function system_GetScoreComboTime(){return m_score_combo_time;}
function system_GetScoreComboCount(){return trunc(m_score_combo_counter/10);}
function system_AddScoreComboCount(){m_score_combo_counter += 10;}

function system_GetChainLength(){return m_sphere_chain;}
function system_GetSpeedRate(){return REMY_GetNumMax((absolute(m_sphere_roll_speed)/ m_sphere_roll_speed_max), m_sphere_speed/default_SphereSpeed); }

function system_SetScoreComboStopFlag(let flag){m_flag_combo_countstop = flag;}


