
//天子用システム


let m_sphereX = 0;
let m_sphereY = 0;
let m_sphere_radius = 30;
let m_enemy_weight = 0;
let m_enemy_radius = 12;
let m_enemy_type = 0;
let m_invincible_time = 0;
let m_flag_invisible = false;
let m_flag_reflection = false;


let default_LifeBase = 1000;
let const_ReflectionPowerLevel = [2.0, 1.0, 0.7, 0.4, 0.3, 0.2];
let const_ReflectionPowerType = [5,1,2,4,4,1,1];

function system_SetEnemyLife(let val){
	SetLife(default_LifeBase + val);
}//end function
function system_SetEnemyWeight(let val){
	m_enemy_weight = val;
}//end function
function system_SetEnemyRadius(let val){
	m_enemy_radius = val;
}//end function


function system_IsInvisible(){
	return m_flag_invisible;
}

task once_system_EnemyMain(){

	//once_system_TaskCrashHitTest();
	loop{
		SetCollisionB(GetX(), GetY(), m_enemy_radius);
		if(GetLife() <= default_LifeBase){ system_EnemyDefeated(); }
		if(REMY_IsOutOfClip(GetX(),GetY(),-60)){ VanishEnemy(); }
		//if(BeParentVanished()){ VanishEnemy(); }

		m_invincible_time = REMY_GetNumMax(m_invincible_time-1, 0);
		yield;
	}

}//end task

task once_system_TaskSphereParameterChecker(){

	loop{
		
		let pos = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_POSITION", [0,0]);
		m_sphereX = pos[0];
		m_sphereY = pos[1];
		m_sphere_radius = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_RADIUS", 0);
		yield;
	}
}


task once_system_TaskSphereHitTest(){

	let onmove = false;
	let level = 0;

	loop{
		let dist_x = GetX() - m_sphereX;
		let dist_y = GetY() - m_sphereY;
		let dist_rad = (m_sphere_radius + m_enemy_radius);
		if( (dist_x * dist_x) + (dist_y * dist_y) < (dist_rad * dist_rad) && m_invincible_time == 0){
			level = GetCommonDataDefaultEx("SPHERIA_SPHERE", "SPHERE_LEVEL", 0);
			AddLife(-(50));
			SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION", system_GetAngleToSphere(GetX(), GetY()));
			SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION_POWER", const_ReflectionPowerType[m_enemy_type] * const_ReflectionPowerLevel[level] );

			PlaySE(se_Hit);
			m_invincible_time = 21;
			if(GetLife() > default_LifeBase){
				onmove = true;
				move(25 + 3 * level, system_GetAngleToSphere(GetX(), GetY())+rand(120,240) );
			}
		}

		if(GetHitCount()>0 && !onmove){
			PlaySE(se_Hit);
			onmove = true;
			move(5 , system_GetAngleToSphere(GetX(), GetY())+rand(120,240) );
		}
		if(GetHitCount()>0 && m_enemy_weight>0){
			let chip = GetCommonDataDefaultEx("SPHERIA_ENEMY", "ENEMY_LIFEITEM", []);
			chip = chip ~ [[GetX(), GetY(), 1]];
			SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_LIFEITEM", chip);
			m_enemy_weight = REMY_GetNumMax(m_enemy_weight-5, 0);
		}

		yield;
	}


	task move(let power, let angle){
		let speed = GetSpeed();
		descent(i in 0..20){
			SetX(GetX() + power * (i/20) * cos(angle));
			SetY(GetY() + power * (i/20) * sin(angle));
			yield;
		}
		SetSpeed(speed);
		onmove = false;
	}//end task

}//end task


function system_CreateEnemyShot(let x, let y, let speed, let angle, let graphic){

	let array = GetCommonDataDefaultEx("SPHERIA_ENEMY", "ENEMY_SHOT",[]);
	array = array ~ [[0, x, y, speed, angle, graphic]];
	SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_SHOT",array);

}


function system_EnemyDefeated(){

	let weight = GetCommonDataDefaultEx("SPHERIA_ENEMY", "ENEMY_WEIGHT", []);
	weight = weight ~ [[GetX(), GetY(), m_enemy_weight]];
	SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_WEIGHT", weight);
	m_flag_invisible = true;

	alternative(m_enemy_type)
	case(1){ system_burst01(GetX(), GetY(), 0.6); } 
	case(0,2){ 
		system_burst01(GetX(), GetY(), 0.9);
		loop(2){ system_burst01(GetX() + rand(-30,30) , GetY() + rand(-30,30), 0.4); }
	}
	case(3,4){
		system_burst01(GetX(), GetY(), 1.5); 
		loop(4){ system_burst01(GetX() + rand(-50,50) , GetY() + rand(-50,50), 0.6);}
	} 

	loop(20){yield;}

	VanishEnemy();
}


task system_burst01(let x, let y, let scale){

	let obj = REMY_CreateEffectObject(img_Burst, 4, 5);
	ObjEffect_SetScale(obj,scale, scale);
	Obj_SetPosition(obj, x, y);
	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, 100,255,255,255);
		loop(3){ yield; }
	}
	Obj_Delete(obj);

}//end func



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

function system_GetSphereX(){return m_sphereX;}
function system_GetSphereY(){return m_sphereY;}

function system_SetEnemyType(let val){ m_enemy_type = val;}

