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

#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 enep = GetCurrentScriptDirectory ~ "./../Sounds/se_msl.wav";
let sho = GetCurrentScriptDirectory ~ "./../Sounds/se_tan00b.wav";
let kira = GetCurrentScriptDirectory ~ "./../Sounds/se_kira00.wav";
#include"./DrawBoss.txt"

@Initialize{
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	SetAutoDeleteObject(true);
	ObjEnemy_Regist(bossObj);
	SetCommonData("Aura",0);
	SetCommonData("End",0);
	LoadShotData;
	animateBossM;
	mainTask;
	TEnd;

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

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(600);}
	case(EV_REQUEST_TIMER){SetScriptResult(60);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	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);

	if(damage==0){ObjEnemy_SetDamageRate(bossObj, 0, 0);}
	if(damage==1){ObjEnemy_SetDamageRate(bossObj, 15, 2);}

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	yield;}

@Finalize{}

task mainTask{wait(1);TEtama;PlaySE(card);
	THarvesterWreathC(0.7,1);
	THarvesterWreathC(1,-1);
	ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY-120,5,3);
	ObjCutin_SetSpellcardS4("Secret \"The Principle of Harvest growth\"", cutin2, "KANAKO", 255, 64, 0);
	ObjCutin_LaunchS3("KANAKO", cutin2, "normal");
	TSpellBG;wait(120);damage=1;
	while(!Obj_IsDeleted(bossObj)){
		TCorn;attack=1;
		wait(90);attack=0;
		wait(120);
	ObjMove_SetDestAtWeight(bossObj,CenterX+rand(-30,30),CenterY-120,5,5);
		wait(60);
		
		TTomato;attack=1;
		wait(90);attack=0;
		wait(120);
	ObjMove_SetDestAtWeight(bossObj,CenterX+rand(-30,30),CenterY-120,5,5);
		wait(60);
		
		TCarrot;attack=1;
		wait(90);attack=0;
		wait(120);
	ObjMove_SetDestAtWeight(bossObj,CenterX+rand(-30,30),CenterY-120,5,5);
		wait(60);}}

task TCorn{ let r = rand(0,45);
	ascent(i in 0..8){
	let a = CreateShotA1(bossX+60*cos(i*45),bossY+60*sin(i*45),3,i*45+r,281,30);
	ObjShot_SetSpellResist(a,true);TManage(a);}
	wait(30);PlaySE(sho);
	task TManage(obj){
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetX(obj)>=404){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),180);break;}
			if(ObjMove_GetX(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),0);break;}
			if(ObjMove_GetY(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),90);break;}
			if(ObjMove_GetY(obj)>=470){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),270);break;}
		yield;}}
	task TShot(x,y,ang){ let angle = 0;
		let shot = CreateVeg(x,y,3,ang,1);
		while(!Obj_IsDeleted(shot)){
			let s = CreateShotA1(ObjMove_GetX(shot),ObjMove_GetY(shot),0,0,27,15);
			ObjMove_AddPatternA1(s,90,0.8,angle);
			angle+=20;wait(6);}}}

task TTomato{ let r = rand(0,45);
	ascent(i in 0..8){
	let a = CreateShotA1(bossX+60*cos(i*45),bossY+60*sin(i*45),3,i*45+r,276,30);
	ObjShot_SetSpellResist(a,true);TManage(a);}
	wait(30);PlaySE(sho);
	task TManage(obj){
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetX(obj)>=404){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),180);break;}
			if(ObjMove_GetX(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),0);break;}
			if(ObjMove_GetY(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),90);break;}
			if(ObjMove_GetY(obj)>=470){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj),270);break;}
		yield;}}
	task TShot(x,y,ang){ let angle = 0;
		let shot = CreateVeg(x,y,4,ang,2);
		loop(45){
			ObjMove_SetSpeed(shot,ObjMove_GetSpeed(shot)-0.085);
			yield;}
		wait(30);
		while(GetObjectDistance(shot,GetPlayerObjectID) > 80){
			ObjMove_SetSpeed(shot,5);
			ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
			yield;}
			ObjMove_SetSpeed(shot,0.1);
		wait(30); PlaySE(kira); loop(10){
			CreateShotA1(ObjMove_GetX(shot),ObjMove_GetY(shot),rand(0.5,1),rand(0,180)*2,210,15);}
		wait(18);Obj_Delete(shot);}}

task TCarrot{ let r = rand(0,45);
	ascent(i in 0..8){
	let a = CreateShotA1(bossX+60*cos(i*45),bossY+60*sin(i*45),3,i*45+r,282,30);
	ObjShot_SetSpellResist(a,true);TManage(a);}
	wait(30);PlaySE(sho);
	task TManage(obj){
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetX(obj)>=404){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj));break;}
			if(ObjMove_GetX(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj));break;}
			if(ObjMove_GetY(obj)<=-20){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj));break;}
			if(ObjMove_GetY(obj)>=470){PlaySE(enep);
				TShot(ObjMove_GetX(obj),ObjMove_GetY(obj));break;}
		yield;}}
	task TShot(x,y){ let angle = 0;
		let shot = CreateVeg(x,y,0.8,0,3);
		loop(90){
			ObjMove_SetAngle(shot,GetAngleToPlayer(shot));yield;}
		loop(60){
			ObjMove_SetSpeed(shot,ObjMove_GetSpeed(shot)+0.05);yield;}
		while(!Obj_IsDeleted(shot)){
			if(ObjMove_GetX(shot)>=384){
				T2Shot(shot);break;}
			if(ObjMove_GetX(shot)<=0){
				T2Shot(shot);break;}
			if(ObjMove_GetY(shot)<=0){
				T2Shot(shot);break;}
			if(ObjMove_GetY(shot)>=450){
				T2Shot(shot);break;}
		yield;}}
	task T2Shot(obj){ loop(15){
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(1,1.5),ObjMove_GetAngle(obj)+180+rand(-15,15),372,15);}}}

function CreateVeg(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,4,118,24,160);}
			if(gr == 2){ObjSprite2D_SetSourceRect(id,76,211,106,239);}
			if(gr == 3){ObjSprite2D_SetSourceRect(id,21,159,33,201);
				ObjRender_SetScaleXYZ(id,1.2,1.2,0);}
		ObjEnemy_SetIntersectionCircleToPlayer(id,enemyX,enemyY,10);
		ObjRender_SetAngleZ(id,angleZ);
		if(GetCommonData("End",0)!=0){DisappearBulletEffect(enemyX,enemyY,id,255,25,25);}
		if(CheckOutsideScreenA2(enemyX,enemyY,64)){
			Obj_Delete(id);break;}
		if(gr == 1){angleZ=ObjMove_GetAngle(id)+90;}
		if(gr == 3){angleZ=ObjMove_GetAngle(id)+270;}
		angleZ+=2;yield;}}

function CheckOutsideScreenA2(x, y, distance){
	return x>GetStgFrameWidth+distance||x<-distance||y>GetStgFrameHeight+distance||y<-distance;}