
//天子用システム


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 m_enemy_damage_rate = 1.0;

let default_LifeBase = 1000;
let const_ReflectionPowerLevel = [2.0, 1.5, 1.2, 1.0, 1.0, 1.0];;
let const_ReflectionPowerType = 5;

function system_SetEnemyLife(let val){
	SetLife(default_LifeBase + val);
}//end function
function system_GetEnemyLife(){
	return (GetLife - default_LifeBase);
}//end function
function system_SetEnemyDamageRate(let val){
	m_enemy_damage_rate = 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(),-100)){ 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(-(20 + level * 4) * m_enemy_damage_rate);
			SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION", system_GetAngleToSphere(GetX(), GetY()));
			SetCommonDataEx("SPHERIA_SPHERE", "SPHERE_REFLECTION_POWER", const_ReflectionPowerType * const_ReflectionPowerLevel[level] );

			PlaySE(se_Hit);
			m_invincible_time = 21;

			local{
				let weight = GetCommonDataDefaultEx("SPHERIA_ENEMY", "ENEMY_WEIGHT", []);
				weight = weight ~ [[GetX(), GetY(), m_enemy_weight]];
				SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_WEIGHT", weight);
			}

//			if(GetLife() > default_LifeBase){
//				onmove = true;
//				move(5 + 5 * 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) );
//		}

		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;
	system_burst01(GetX(), GetY(), 2);

	ascent(i in 0..40){
		DeleteEnemyShotImmediatelyInCircle(ALL, GetX(), GetY(), i*10);
		loop(1){yield;}
	}

	SetLife(0);
}


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

task system_CreateDanger(let x, let y){

	let obj = REMY_CreateEffectObject(img_Text, 4, 6);
	let rect = system_GetGraphicRect("DANGER");
	REMY_SetObjectVertexSquare(obj, rect, 0, 0, 200, 220, 205, 25);
	Obj_SetPosition(obj,  x,  y);

	ascent(i in 0..50){
		ObjEffect_SetScale(obj, 0.3 + 0.1*(i/50), 0.6 + 0.1*(i/50));
		REMY_SetObjectColorSquare(obj, 220*(1-(i/50))*[1,2,1,0][i%4], 220, 205, 25);

		yield;
	}
	Obj_Delete(obj);

}//end task

//天子の描画
task once_system_TaskDrawTenshi(){

	yield;
	initialize_REMY_TaskLRMoveStatus(2, 6);

	let obj = REMY_CreateEffectObject(img_EnemyBoss, 4, 6);
	ObjEffect_SetRenderState(obj, ALPHA);
	REMY_SetObjectVertexSquare(obj,  [0, 0, 32*1, 32*2], 0, 0, 255, 255, 255, 255);

	let counter = 0;
	let old_state = boss_state;
	let rect = system_SetGraphicRect("BOSS_MAIN01");
	loop{

		REMY_SetLRMoveValue(trunc(GetSpeedX()));
		if(boss_state != old_state){
			counter = 0;
			old_state = boss_state;
		}

		alternative(boss_state)
		case(_STATE_STAND){
			rect = system_GetGraphicRect(["BOSS_LEFT02","BOSS_LEFT01",["BOSS_MAIN01","BOSS_MAIN02","BOSS_MAIN03","BOSS_MAIN04"][trunc(counter/4)%4],"BOSS_RIGHT01","BOSS_RIGHT02"][REMY_GetLRMoveStatus()+2]);
			counter++;
		}
		case(_STATE_JUMP){
			rect = system_GetGraphicRect("BOSS_JUMP01");
		}
		case(_STATE_JUMPEND){
			rect = system_GetGraphicRect(["BOSS_JUMP02", "BOSS_JUMP03"][ (counter>8) ]);
			counter++;
		}
		case(_STATE_CONC){
			rect = system_GetGraphicRect("BOSS_CONC01");
		}

		REMY_SetObjectVertexSquare(obj, rect, 0, 0, 255, 255, 255, 255);
		Obj_SetPosition(obj, GetX(), GetY()-60);
		yield;
	}

}//end task


task system_SpellCard(){

	let obj = REMY_CreateEffectObject(img_Spell, 52, 3);
	let rect = system_GetGraphicRect("SPELL");
	ObjEffect_SetRenderState(obj,SUBTRACT);
	Obj_SetPosition(obj,  GetX(),  GetY()-60);
	rotate();

	ascent(i in 0..52){
		let angle = 360 * (trunc(i/2)/25);
		let radius = [100,120][i%2];
		ObjEffect_SetVertexXY(obj, i, radius * cos(angle), radius * sin(angle));
		ObjEffect_SetVertexUV(obj, i, 128 * trunc(i/2), [rect[1],rect[3]][i%2]);
//		ObjEffect_SetVertexColor(obj, i, 250, 250, 010, 250);
		ObjEffect_SetVertexColor(obj, i, 250, 10, 250, 10);
	}

	ascent(j in 0..31){
		let alpha = 250 * (j/30);
		REMY_SetObjectColorMultiple(obj, 54, alpha, 220, 205, 25);
		ObjEffect_SetScale(obj, j/30, j/30);
		yield;
	}

	while(!m_flag_invisible){
		yield;
	}

	ascent(j in 0..31){
		let alpha = 250 * (1-j/30);
		REMY_SetObjectColorMultiple(obj, 54, alpha, 220, 205, 25);
		ObjEffect_SetScale(obj, 1 + 3*(j/30), 1 + 3*(j/30));
		yield;
	}
	Obj_Delete(obj);

	task rotate(){
		let angle = 0;
		loop{
			angle = REMY_GetAngleLimited(angle + 2.5);
			ObjEffect_SetAngle(obj, 0, 0, angle);
			yield;
		}

	}


}//end task






//方向取得
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;}

