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

#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("Aura",0);
	SetCommonData("End",0);
	LoadShotData;
	animateBossM;
	mainTask;
	TEnd;

	LoadSound(sho);
	LoadSound(card);
	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);
	ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY-120,5,3);
	ObjCutin_SetSpellcardS4("Hunt for fruits \"Rare Catch\"", cutin2, "KANAKO", 255, 64, 0);
	ObjCutin_LaunchS3("KANAKO", cutin2, "normal");
	TSpellBG;wait(120);damage=1;let move = 0;
	TFruitSpawn;
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtWeight(bossObj,GetPlayerX+45*cos(move),GetPlayerY+45*sin(move),2,1);
	move+=2;yield;}}

task TFruitSpawn{
	while(!Obj_IsDeleted(bossObj)){
		let fruit = CreateFruit(bossX+rand(-45,45),bossY+rand(-45,45),0,rand(0,180)*2,round(rand(1,9)));
		let delay = CreateShotA1(ObjMove_GetX(fruit),ObjMove_GetY(fruit),0,0,round(rand(1,15)),30);
		TFruitExplosion(fruit,round(rand(1,4)));
		ObjShot_SetDeleteFrame(delay,0);
		TEnemyDelay(fruit,30,0.2);
	wait(15);}}

task TFruitExplosion(obj,c){
	wait(250);
	PlaySE(sho);
	let gr1;
	let gr2;
	if(c == 1){gr1=35; gr2=210;}
	if(c == 2){gr1=47; gr2=223;}
	if(c == 3){gr1=46; gr2=221;}
	if(c == 4){gr1=43; gr2=218;}
	loop(4){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(1,1.5),rand(0,180)*2,gr1,15);}
	loop(8){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(1,1.5),rand(0,180)*2,gr2,0);}
	wait(5);
	Obj_Delete(obj);}

task TEnemyDelay(obj,d,s){
	loop(d){
	ObjMove_SetSpeed(obj,0);
	Obj_SetVisible(obj,false);
	ObjEnemy_SetIntersectionCircleToPlayer(obj,-5,-5,0);
		yield;}
	ObjMove_SetSpeed(obj,s);
	Obj_SetVisible(obj,true);}

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);
	if(gr == 2){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	if(gr == 3){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	if(gr == 4){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	if(gr == 5){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	if(gr == 7){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	if(gr == 10){ObjRender_SetScaleXYZ(id,0.8,0.8,0);}
	while(!Obj_IsDeleted(id)){
		enemyX = ObjMove_GetX(id);
		enemyY = ObjMove_GetY(id);
		ObjSprite2D_SetDestCenter(id);
			if(gr == 1){ObjSprite2D_SetSourceRect(id,18,3,47,28);}//strawberry
			if(gr == 2){ObjSprite2D_SetSourceRect(id,149,253,215,300);}//grapes
			if(gr == 3){ObjSprite2D_SetSourceRect(id,234,50,270,92);}//red apple
			if(gr == 4){ObjSprite2D_SetSourceRect(id,190,50,230,90);}//yellow pear
			if(gr == 5){ObjSprite2D_SetSourceRect(id,2,85,35,117);}//orange
			if(gr == 6){ObjSprite2D_SetSourceRect(id,90,4,127,32);}//peach
			if(gr == 7){ObjSprite2D_SetSourceRect(id,178,103,224,172);}//ananas
			if(gr == 8){ObjSprite2D_SetSourceRect(id,70,185,96,206);}//avacado
			if(gr == 9){ObjSprite2D_SetSourceRect(id,262,1,292,23);}//plum
			if(gr == 10){ObjSprite2D_SetSourceRect(id,154,52,190,104);}//banana
		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;}}