#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 1"]
#Text["Wooooooop"]

let objBoss;
#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 90);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtSpeed(objBoss,384/2,150,6);
	
	mainTask;	
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(1500);
        }
	case(EV_REQUEST_TIMER){
		SetScriptResult(55);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

let BossX = 0;
let BossY = 0;

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,24);
	
	yield;
} 

@Finalize {

} 

task mainTask { 
	renderdiancie(objBoss);
	BottomEnemyMarker(objBoss, objBoss);
	end;
	action;
	BossShotSound(objBoss, 75, 300);
} 

task SpellCall {
	bspell = true;
	TCallBg2;
}
let Diancie = GetCurrentScriptDirectory ~ "Functions/img/Cutin/DiancieCutin.png";
task action{
wait(60);
ObjCutin_SetSpellcardS3("Compression &quot;Diamond Creation&quot;", 255, 15, 200);
ObjCutin_LaunchS3("KANAKO", Diancie,"Normal");
SpellCall;
let objScene = GetEnemyBossSceneObjectID();
ObjEnemyBossScene_StartSpell(objScene);
wait(180);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/charge.wav", 100);
ConcentrationA1(objBoss,"MAGIC",90,4,120,[255,150,255]);
wait(90);
Attack;
wait(210);
Attack2;
}

task Attack {
	let player = GetAngleToPlayer(objBoss);
	let angle = player+35;
	let angle2 = player-35;
	let speed = 1.5;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	loop(10){
	Shoot1(angle,angle2,speed);
	angle += 10;
	angle2 -= 10;
	wait(1)
	}
	player = GetAngleToPlayer(objBoss);
	angle = player+35;
	angle2 = player-35;
	if(speed>5){ObjEnemy_SetDamageRate(objBoss,40,30);}
	if(speed<5){speed += 1;}
	wait(60);
	}
}
task Attack2 {
	let player = GetAngleToPlayer(objBoss);
	let FancyAngle = 0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	let obj = CreateShotA1(ObjMove_GetX(objBoss)+30*cos(FancyAngle), ObjMove_GetY(objBoss)+30*sin(FancyAngle), 0, 0, ICE_RED,50);
	GOGOGO(obj);
	FancyAngle+=25;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(15);
	}
	task GOGOGO(tar){
	ObjMove_AddPatternA4(tar, 30, 0, GetAngleToPlayer(tar), 0.02, 0, 2, 0, ICE_RED);
	}
}

task Shoot1(ang,ang2,spd){
	let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), spd, ang, -0.04, 0, BALL_M_ROSE,0);
		ObjShot_SetSpellResist(obj, 1);
		ObjRender_SetAlpha(obj,75);
		ObjShot_SetAutoDelete(obj, 0);
		ObjShot_SetIntersectionEnable(obj, 0);
		REFLECT(obj);
	let obj2 = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), spd, ang2, -0.04, 0, BALL_M_ROSE,0);
		ObjShot_SetSpellResist(obj2, 1);
		ObjRender_SetAlpha(obj2,75);
		ObjShot_SetAutoDelete(obj, 0);
		ObjShot_SetIntersectionEnable(obj, 0);
		REFLECT(obj2);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 70);
		Trigger(obj,obj2);
}

task REFLECT(obj){
	while(!Obj_IsDeleted(obj)){		
		if(trunc(ObjMove_GetX(obj))>GetStgFrameWidth){ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));}
		if(trunc(ObjMove_GetX(obj))<0){ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));}
		if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight){ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));}
		if(trunc(ObjMove_GetY(obj))<0){ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));}
		yield;
	}
}

task Trigger(tar,tar2){
	while(ObjMove_GetSpeed(tar)&&ObjMove_GetSpeed(tar2) > 0){yield;}
	ObjMove_SetSpeed(tar,0);
	ObjMove_SetSpeed(tar2,0);
	let angle = GetAngleTo(tar2,tar);
	let lengh = GetObjectDistance(tar,tar2);
	let las = CreateStraightLaserA1(ObjMove_GetX(tar2),ObjMove_GetY(tar2), angle, lengh, 15, 200, BEAM_ROSE, 50);
	if(ObjMove_GetX(las)==0&&ObjMove_GetY(las)==0){Obj_Delete(las);}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser2.wav", 40);
	wait(200);
	ObjShot_FadeDelete(tar);
	ObjShot_FadeDelete(tar2);
}


task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
}

