//東方弾幕風
//てんし
#ScriptVersion[2]

script_enemy_main{

#include_function".\lib\include_std.txt"
#include_function".\system\include_enemy_boss.txt"

let animation_counter = 0;
let boss_state = _STATE_CONC;

@Initialize{

	SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_BOSS_FINAL", true);
	SetCommonData("KANAME_HIT", 0);

	SetDamageRate(0,0);
	system_SetEnemyTexture("KANAME_B");
	system_SetEnemyType(0);
	system_SetEnemyDamageRate(0.5);

	once_system_LoadResourceBoss();
	SetLife(2500);
	system_SetEnemyLife(1000);
	SetCommonDataEx("SPHERIA_ENEMY", "ENEMY_BOSSLIFE_MAX", 1000);

	system_SetEnemyWeight(16);
	system_SetEnemyRadius(48);
	LoadUserShotData(GetCurrentScriptDirectory() ~ "UserShot_tenshi.txt");
	once_MainRoutine();
}

@MainLoop{
	SetCollisionA(GetX(), GetY(), 24);
	animation_counter++;
	yield;
}

@DrawLoop{
//	if(system_IsInvisible()){return;}

	SetTexture(img_Enemy);
	SetGraphicScale(1.5,1.5);
	system_SetGraphicRect(["ROCK_MAIN01","ROCK_MAIN02","ROCK_MAIN03","ROCK_MAIN04"][trunc(animation_counter/5)%4]);
	DrawGraphic(GetX(), GetY());
}

@Finalize{

}



task once_MainRoutine(){

	boss_state = _STATE_CONC;

	once_system_TaskDrawTenshi();
	once_system_TaskSphereParameterChecker();
	once_system_TaskSphereHitTest();
	once_system_EnemyMain();
	once_MoveRoutine();
	once_HitCheckRoutine();
}

task once_MoveRoutine(){


	loop(120){yield;}
	system_SpellCard();
	loop(60){yield;}

	loop{
		boss_CreateKanameStoneA(GetCenterX()-150, 100);
		boss_CreateKanameStoneA(GetCenterX()+150, 100);
		loop(120){yield;}
		shot_laser();
		loop(90){yield;}
		shot_laser();
		loop(80){yield;}
		shot_laser();
		loop(150){yield;}
		if(system_GetEnemyLife() < 600){ break; }
	}

	loop{
		boss_CreateKanameStoneA(GetCenterX()-120, 90);
		boss_CreateKanameStoneA(GetCenterX()+120, 90);
		loop(160){yield;}
		shot_laser();
		loop(90){yield;}
		shot_laser();
		loop(70){yield;}
		boss_CreateKanameStoneA(GetCenterX()-180, 150);
		boss_CreateKanameStoneA(GetCenterX()+180, 150);
		shot_laser();
		loop(45){yield;}
		shot_laser();
		loop(30){yield;}
		boss_CreateKanameStoneA(GetCenterX()-120, 220);
		boss_CreateKanameStoneA(GetCenterX()+120, 220);
		shot_laser();
		loop(160){yield;}
	}



}//end task

task once_HitCheckRoutine(){

	loop{
		let hit = GetCommonData("KANAME_HIT");
		if(hit>0){
			PlaySE(se_RockHit);
			AddLife(- (hit * 180));
			SetCommonData("KANAME_HIT", 0)
		}
		loop(8){yield;}
	}

}

task boss_CreateKanameStoneA(let x, let y){

	system_CreateDanger(x, y);
	loop(60){yield;}
	PlaySE(se_Create);
	CreateEnemyFromScript("KANAME", x, y, 0, 0, 0);

}//

task shot_circle(){

	ascent(i in 0..3){
		ascent(j in 0..30){
			let id = 1;
			let angle = 360 * (j/30);
			CreateShotA(id, GetX() + cos(angle) * 40, GetY() + sin(angle) * 40, 10);
			SetShotDataA(id, 0, 1, angle, [-0.11,0.11][i%2], 0.02, 3, _SHOT_RED);
			FireShot(id);
		}
		loop(30){yield;}
	}

}


task shot_laser(){


	loop(20){yield;}

	SetColor(255,0,0);
	Concentration01(50);
	SetColor(255,255,255);
	
	loop(30){yield;}

	let angle = GetAngleToPlayer();
	let id2 = 0;
	CreateLaserA(id2, GetX(), GetY(), 500, 50, _SHOT_RED, 45);
	SetLaserDataA(id2, 00, angle, 0, 0, 0, 0);
	SetShotKillTime(id2, 40);
	FireShot(id2);

	REMY_TaskDelaySE(se_Laser, 36);
	loop(25){yield;}

		ascent(j in 0..80){
			ascent(i in 0..6){
				loop(2){
					let id = 1;
					CreateShotA(id, GetX() + cos(angle) * 40+ cos(angle-90) * 2 *(2-i), GetY() + sin(angle) * 40+ sin(angle-90) * 2 *(2-i), 10);
					SetShotDataA(id, 0, 13 + rand(-4,4), angle -6+ 12 * (i/6), 0.10 * (2-i), 0, 15, _SHOT_RED);
					FireShot(id);
				}
			}
			yield;
		}

	loop(60){yield;}


}//end task



}


////------------------------------------------
script_enemy KANAME{


#include_function".\lib\include_std.txt"
#include_function".\system\include_enemy_child.txt"

let animation_counter = 0;
let initialize_counter = 0;

@Initialize{
	SetDamageRate(100,100);
	system_SetEnemyTexture("KANAME_B");
	system_SetEnemyType(3);
	once_system_LoadResource();
	system_SetEnemyLife(10000);
	system_SetEnemyWeight(128);
	system_SetEnemyRadius(20);
	once_MainRoutine();
}

@MainLoop{
	SetCollisionA(GetX(), GetY(), 10);
	animation_counter++;
	if(initialize_counter < 10){ initialize_counter++; }
	
	yield;
}

@DrawLoop{
	if(system_IsInvisible()){return;}
	SetTexture(img_Enemy);
	let scale_x = 0.4 * (initialize_counter/10);
	let scale_y = 0.4 + (1-initialize_counter/10);
	SetGraphicScale(scale_x, scale_y);
	system_SetGraphicRect(["ROCK_MAIN01","ROCK_MAIN02","ROCK_MAIN03","ROCK_MAIN04"][trunc(animation_counter/4)%4]);
	DrawGraphic(GetX(), GetY());
}

@Finalize{

}


task once_MainRoutine(){
	once_system_TaskSphereParameterChecker();
	once_system_TaskSphereHitTest();
	once_system_EnemyMain();
	once_MoveRoutine();
	once_HitCheckRoutine();
}

task once_MoveRoutine(){

	loop(40){yield;}

	loop{

		let to_x = GetPlayerX();
		let to_y = GetPlayerY();
		let flag = false;
		loop(10){
			if(Collision_Line_Circle(GetX(), GetY(), to_x, to_y, 1, GetEnemyX(), GetEnemyY(), 68)){
				to_x += rand(-30,30);
				to_y += rand(-30,30);
			}
			else{ flag = true; break; }
		}

		if(flag){
			Concentration01(30);
			loop(50){yield;}
			SetMovePosition03(to_x, to_y, 16, 8);
		} 
		loop(10){yield;}
		while(GetSpeed()>0){ yield; }

		loop(120){ yield; }
	}

}


task once_HitCheckRoutine(){

	loop{
		if( check_distance() ){ break; }
		loop(3){ yield; }
	}
	PlaySE(se_Hit);
	SetCommonData("KANAME_HIT", GetCommonData("KANAME_HIT")+1 );

	system_EnemyDefeated();

}

task create_shot(let speed, let angle){

	let obj = REMY_CreateMoveLaserObject(GetX(), GetY(), 32,  14, angle, speed, angle+180 ,_SHOT_RED, 10);
	while(!Obj_BeDeleted(obj)){
		if(	m_flag_invisible ){break;}
		yield;
	}

	system_burst01(Obj_GetX(obj),Obj_GetY(obj), 0.3);
	Obj_Delete(obj);

}

function check_distance(){
	return (GetEnemyX()-GetX())*(GetEnemyX()-GetX()) + (GetEnemyY()-GetY()) * (GetEnemyY()-GetY()) < (60*60);

}


}//end script







