
//スフィア

let m_sphereX;
let m_sphereY;
let m_sphere_radius;
let m_move_stop = 0;

task once_p01_CheckSphereParameter(){
//CreateDebugWindow();
	spped_marker();

	loop{

		let check_state = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_STATE", 0);
		if(check_state == _SPHERE_CHARGE){SetSpeed(0,0); m_move_stop = 2;}
		else if(check_state == _SPHERE_ROLLING){SetSpeed(1,1); m_move_stop = 1;}
		else{SetSpeed(3,3); m_move_stop = 0;}

		let check_pos = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_POSITION", []);
		m_sphereX = check_pos[0];
		m_sphereY = check_pos[1];

		m_sphere_radius = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_RADIUS", 0);
		
		if(p01_IsIntersectWithSphere(GetPlayerX(), GetPlayerY(), (m_sphere_radius-5))){
			let angle = system_GetAngleToSphere(GetPlayerX(), GetPlayerY()) + 180;
			SetX(m_sphereX + (m_sphere_radius-5) * cos(angle));
			SetY(m_sphereY + (m_sphere_radius-5) * sin(angle));
		}

		yield;
	}

	task spped_marker(){

		let obj = PRIBA_CreateEffectObject(img_Player, 4, 6);
		ObjEffect_SetScale(obj, 2, 2);
		let rect = p01_GetGraphicRect("PLAYER_STOP");
		PRIBA_SetObjectVertexSquare(obj, rect, 0, 0, 0, 0, 0, 0);

		let angle = 0;
		let counter = 0;
		loop{
			if(m_move_stop != 2){
				counter = PRIBA_GetNumMax(counter-24, 0);
			}else{
				counter = PRIBA_GetNumMin(counter+16, 255);
			}
			angle = PRIBA_GetAngleLimited( angle + 5 );
			ObjEffect_SetAngle(obj, 0, 0, angle);
			Obj_SetPosition(obj, GetPlayerX(), GetPlayerY());
			PRIBA_SetObjectColorSquare(obj, counter, 155, 155, 155);
			let scale = 1 + 2 * (1 - counter/255);
			ObjEffect_SetScale(obj, scale, scale);
			yield;

		}

	}


}



task once_p01_CheckSphereCrash(){

	loop{
		let check = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_CRASH",[]);
		
		while(length(check) != 0){
			let array = check[0];
			p01_TaskCreateBurstShot(array[0], array[1]);
			check = erase(check, 0);
		}
		SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_CRASH", []);
		
		yield;
	}

}//end task


task p01_TaskCreateBurstShot(let x, let y){


	Effect();
	Shot();

	task Effect(){
		let obj = PRIBA_CreateEffectObject(img_BurstBlue, 4, 6);
		ObjEffect_SetScale(obj, 1, 1);
		Obj_SetPosition(obj, x, y);

		ascent(i in 0..8){
			let rect = p01_GetGraphicRect(["BURST_01","BURST_02","BURST_03","BURST_04","BURST_05","BURST_06","BURST_07","BURST_08"][i]);
			PRIBA_SetObjectVertexSquare(obj, rect, 0, 0, 50 + 150 * (1-i/8), 255, 255, 255);
			DeleteEnemyShotImmediatelyInCircle(ALL, x, y, 32);
			loop(6){ yield; }
		}
		Obj_Delete(obj);
	}

	task Shot(){

		let obj = PRIBA_CreateShotObject(x, y, 0, 0, 2, 10000, 1, 0);
		Obj_SetPosition(obj, x, y);
		loop(48){
			yield;
		}
		Obj_Delete(obj);
	}

}//end task





//重なっているか判定
function p01_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_GetAngleToSphere(let x, let y){
	return PRIBA_GetAngleLimited( atan2(m_sphereY-y, m_sphereX-x) );
}

function system_GetMoveType(){
	return m_move_stop;
}//end function
