#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Minoriko Spell 05"]

#include"./../Functions/Cutin/Cutin.txt"
#include"./LoadGraphics.txt"

let bossObj;
let bossX;
let bossY;
let damage = 0;
let global = 0;

let card = GetCurrentScriptDirectory ~ "./../Sounds/se_cat00.wav";
let kira = GetCurrentScriptDirectory ~ "./../Sounds/se_kira02.wav";
let sho = GetCurrentScriptDirectory ~ "./../Sounds/se_tan00b.wav";
#include"./DrawBoss.txt"

@Initialize{
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	SetAutoDeleteObject(true);
	ObjEnemy_Regist(bossObj);
	SetCommonData("ForceLifebar",1);
	SetCommonData("Timer",0);
	SetCommonData("End",0);
	LoadShotData;
	mainTask;
	TLastEnd;

	LoadSound(sho);
	LoadSound(card);
	LoadSound(kira);}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(100);}
	case(EV_REQUEST_TIMER){SetScriptResult(110);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(1000000);}
	case(EV_GAIN_SPELL){SetScriptResult(1000000);}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
	case(EV_START_BOSS_SPELL){
		let path = GetCurrentScriptDirectory ~ "./../System/Minoriko_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);yield;}}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	yield;}

@Finalize{}

task mainTask{wait(1);TEtama;PlaySE(card);
	THarvesterWreathC(0.7,0.5);
	THarvesterWreathC(1,-1);
	ObjMove_SetDestAtWeight(bossObj,192,192,5,3);
	ObjCutin_SetSpellcardS4("Harvest Crisis \"Morning prayer to Gods\"", cutin2, "KANAKO", 255, 64, 0);
	ObjCutin_LaunchS3("KANAKO", cutin2, "normal");
	TSpellBG;wait(120);let move = 0;
	TLaserCutter;
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtWeight(bossObj,GetPlayerX,GetPlayerY,5,5);
		yield;}}

task TLaserCutter{
	wait(90);let ang = 0;let num = 15;
	let objlaser1 = ObjectLaser(ang+num);ang+=45;
	let objlaser2 = ObjectLaser(ang+num);ang+=45;
	let objlaser3 = ObjectLaser(ang+num);ang+=45;
	let objlaser4 = ObjectLaser(ang+num);ang+=45;
	let objlaser5 = ObjectLaser(ang+num);ang+=45;
	let objlaser6 = ObjectLaser(ang+num);ang+=45;
	let objlaser7 = ObjectLaser(ang+num);ang+=45;
	let objlaser8 = ObjectLaser(ang+num);ang+=45;
	ObjShot_SetSpellResist(objlaser1,true);
	ObjShot_SetSpellResist(objlaser2,true);
	ObjShot_SetSpellResist(objlaser3,true);
	ObjShot_SetSpellResist(objlaser4,true);
	ObjShot_SetSpellResist(objlaser5,true);
	ObjShot_SetSpellResist(objlaser6,true);
	ObjShot_SetSpellResist(objlaser7,true);
	ObjShot_SetSpellResist(objlaser8,true);
	wait(120);let count = 0;
	while(!Obj_IsDeleted(bossObj)){
	let objmeal = ObjectMeal(1);
		CheckCollision(objlaser1,objmeal,1);
		CheckCollision(objlaser2,objmeal,1);
		CheckCollision(objlaser3,objmeal,1);
		CheckCollision(objlaser4,objmeal,1);
		CheckCollision(objlaser5,objmeal,1);
		CheckCollision(objlaser6,objmeal,1);
		CheckCollision(objlaser7,objmeal,1);
		CheckCollision(objlaser8,objmeal,1);
	if(count >= 30){
	let objmeal = ObjectMeal(2);
		CheckCollision(objlaser1,objmeal,2);
		CheckCollision(objlaser2,objmeal,2);
		CheckCollision(objlaser3,objmeal,2);
		CheckCollision(objlaser4,objmeal,2);
		CheckCollision(objlaser5,objmeal,2);
		CheckCollision(objlaser6,objmeal,2);
		CheckCollision(objlaser7,objmeal,2);
		CheckCollision(objlaser8,objmeal,2);
		}
	if(count >= 60){
	let objmeal = ObjectMeal(3);
		CheckCollision(objlaser1,objmeal,3);
		CheckCollision(objlaser2,objmeal,3);
		CheckCollision(objlaser3,objmeal,3);
		CheckCollision(objlaser4,objmeal,3);
		CheckCollision(objlaser5,objmeal,3);
		CheckCollision(objlaser6,objmeal,3);
		CheckCollision(objlaser7,objmeal,3);
		CheckCollision(objlaser8,objmeal,3);
		}
	if(count >= 90){
	let objmeal = ObjectMeal(4);
		CheckCollision(objlaser1,objmeal,4);
		CheckCollision(objlaser2,objmeal,4);
		CheckCollision(objlaser3,objmeal,4);
		CheckCollision(objlaser4,objmeal,4);
		CheckCollision(objlaser5,objmeal,4);
		CheckCollision(objlaser6,objmeal,4);
		CheckCollision(objlaser7,objmeal,4);
		CheckCollision(objlaser8,objmeal,4);
		}
		count++;
		wait(60);}
	
	task CheckCollision(obj1,obj2,type){
	while(!Obj_IsDeleted(obj2)){
		if(collision(obj1,obj2)){PlaySE(sho);
			if(type == 1){
				ascent(i in 0..10){CreateShotA1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),2,i*36,210,15);}}
			if(type == 2){
				ascent(i in 0..15){CreateShotA1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),1,i*24,19,15);}}
			if(type == 3){
				ascent(i in 0..15){CreateShotA1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),0.5,i*24,278,15);}}
			if(type == 4){
				CreateShotA1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),2,GetAngleToPlayer(obj2),325,15);}
		Obj_Delete(obj2);break;}
	yield;}
	return;}}

function collision(obj,fruit){
	let angle = GetAngleToPlayer(obj) + 180;
	let col = IsIntersected_Line_Circle(
		ObjMove_GetX(obj),ObjMove_GetY(obj),
		ObjMove_GetX(obj)+560*cos(angle),ObjMove_GetY(obj)+560*sin(angle),15,
		ObjMove_GetX(fruit),ObjMove_GetY(fruit),10);
	return col;}

function ObjectLaser(ang){
	let laser = CreateStraightLaserA1(0,0,ang,560,15,7200,1030,120);
	TMovement(laser);
	task TMovement(obj){
	let angle = ObjStLaser_GetAngle(obj);
		while(!Obj_IsDeleted(bossObj)){
			ObjMove_SetPosition(obj,bossX+70*cos(angle),bossY+70*sin(angle));
			ObjStLaser_SetAngle(obj,angle);
		angle+=0.5;yield;}}
	return laser;}

function ObjectMeal(gr){
	let ang = rand(0,180)*2;
	let fruit = CreateFruit(CenterX+250*cos(ang),CenterY+250*sin(ang),2.5,0,gr);
	ObjMove_AddPatternA1(fruit,0,NO_CHANGE,GetAngleToPlayer(fruit));
	return fruit;}

function CreateFruit(x,y,speed,angle,graphic){
	let spirit = CreateEnemy(100);
	ObjMove_SetPosition(spirit,x,y);
	ObjMove_SetSpeed(spirit,speed);
	ObjMove_SetAngle(spirit,angle);
	DrawEnemy(spirit,graphic);
	return spirit;}

function CreateEnemy(objlife){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjEnemy_SetLife(obj,objlife);
	return obj;}

task DrawEnemy(id,gr){
	let enemyX;
	let enemyY;
	let angleZ = rand(0,180);
	ObjPrim_SetTexture(id,akishot);
	while(!Obj_IsDeleted(id)){
		enemyX = ObjMove_GetX(id);
		enemyY = ObjMove_GetY(id);
		ObjSprite2D_SetDestCenter(id);
			if(gr == 1){ObjSprite2D_SetSourceRect(id,37,215,72,239);}
			if(gr == 2){ObjSprite2D_SetSourceRect(id,76,211,106,239);}
			if(gr == 3){ObjSprite2D_SetSourceRect(id,34,73,83,123);}
			if(gr == 4){ObjSprite2D_SetSourceRect(id,38,241,71,268);}
		ObjEnemy_SetIntersectionCircleToPlayer(id,enemyX,enemyY,10);
		ObjRender_SetAngleZ(id,angleZ);
		if(GetCommonData("End",0)!=0){
			DisappearBulletEffect(enemyX,enemyY,id,255,25,25);}
		angleZ+=0.2;yield;}}