#“Œ•û’e–‹•—[Single]
#ScriptVersion[3]
#Title["Spell6"]
#Text["Spell6"]
#Background["./BG.txt"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]

#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"script/Marisa/Shot/Default_ShotConst.txt"
#include"script/Default_system/Default_Effect.Txt"
#Player["./MoF_Reimu/Reimu.txt"]
let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let way = 1;
let s = 1;
let objScene=GetEnemyBossSceneObjectID();
let arr = [1,6,4,2,3,5,0,7];
let shotarr = [31,32,33,34,35,37,38];
let shotnum = 2;
let dir = 0;

@Initialize
{
SetBoss;
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3000000);
	}
}

//----------------------------------------------------
task mainTask{
	magiccircle(objBoss,0.8,true);
	wait(45);
	Cutin("Love Sign\"Fatal Spark\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	AccelMove(objBoss,stgx-60,90,0.05,90);
	wait(90);
	TSpark;
	wait(180);
	ObjEnemy_SetDamageRate(objBoss,50,25);
	}
//----------------------------------------------------

task TSpark{
	let t = 1;
	while(!Obj_IsDeleted(objBoss)){
	PlaySound("Charge5",100,0);
	PlaySound("Charge4",100,60);
	PlaySound("Laser7",100,120);
	MasterSpark;
	ascent(n in 0..18){
	SCharge01(objBoss,2.5+n/9,30,255,0,255);
	loop(5){etama(250,10,0,0,1);}
	wait(5);
	}
	NCharge01(objBoss,4.5,30,255,0,255);
	wait(20);
	attack(270,2);
	InvCircle(objBoss);
	TShot1;
	wait(270);
	alternative(t)
	case(0){AccelMove(objBoss,stgx-60,90,0.05,90);}
	case(1){AccelMove(objBoss,stgx+120,120,0.05,90);}
	case(2){AccelMove(objBoss,stgx-120,120,0.05,90);}
	case(3){AccelMove(objBoss,stgx+60,90,0.05,90);}
	wait(30);
	TShot2(t%2);
	wait(195);
	t = (t+1)%4;
	}
	}
	
task TShot2(type){
			if(type==0){
				ascent(n in 0..12){
					ascent(i in 0..5){
					PlaySound("Shot7",90,0);
					OvalShot(0.5+n/3,angleToPlayer-90+i*30);
					}
					wait(5);
					}
					wait(30);
					ascent(i in 0..18){
						ascent(a in 0..4){
						PlaySound("Shot7",90,0);
						let shot = ShotA2(GetEnemyX,GetEnemyY,0,i*20,0.01+a/100,6,227,0,0);
						DelayShot(shot,0,3,0,10,0);
						shot = ShotA2(GetEnemyX,GetEnemyY,0,i*20+3,0.01+a/100,6,227,0,0);
						DelayShot(shot,0,3,0,10,0);
						}
					}
					
				}
			else if(type==1){
					ascent(m in 0..2){
					ascent(i in 0..24){
						ascent(a in 0..4){
						PlaySound("Shot7",90,0);
						let shot = ShotA2(GetEnemyX,GetEnemyY,0,i*15,0.01+a/100,6,227,0,0);
						DelayShot(shot,0,3,0,10,0);
						}
					}
					wait(90);
					}
					}
			
		task OvalShot(v,dir){
			let shot = ShotOA1(objBoss,4,dir,227,0,0);
			DelayShot(shot,0,3,0,10,0);
			wait(10);
			ascent(a in 0..2){
			ShotOA1(shot,v+a*1,dir,227,0,0);
			}
			Obj_Delete(shot);
		}
	}
	
task TShot1{
	let ang = 0;
	let v = 0;
	let shot;
	descent(n in 0..54){
	ascent(a in 0..3){
	v = 1+a+n/120;
	if(n%2==0 || n%3==0){
	ang = dir+180+180*sin(n*41);
	shot = ShotA2(GetEnemyX,GetEnemyY,v,ang,(-v)/120,0,137,0,8);
	ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.01,0.1*sin(n*11+a*120),5);
	ang = dir+180+180*sin(-n*41);
	shot = ShotA2(GetEnemyX,GetEnemyY,v,ang,(-v)/120,0,137,0,8);
	ObjMove_AddPatternA2(shot,60,NO_CHANGE,NO_CHANGE,0.01,0.1*sin(n*11+a*120),5);
	}
	if(n%1==0){
	ang = dir+180+90*sin(n*77);
	shot = ShotA1(GetEnemyX+0*cos(ang),GetEnemyY+0*sin(ang),v+1,ang+a*3,27,0,8);

	ang = dir+180+90*sin(-n*77);
	shot = ShotA1(GetEnemyX+0*cos(ang),GetEnemyY+0*sin(ang),v+1,ang-a*3,27,0,8);
	}
	}
	if(n%3==0){
		ascent(i in 0..6){
		StarShot(v,n*s*2.5+i*60);
		StarShot(v,n*s*2.5*-1+i*60);
		}
		}
	wait(5);
			task StarShot(v,n){
			ShotA2(GetEnemyX,GetEnemyY,6,n,-0.1,3.5,257,0,0);
			}
	}
	s = -s;
	}
	
task EffBullet{
	let ra = 0;
	ascent(n in 0..80){
	ra = rand(-20,20);
	let shot = ShotA1(GetEnemyX+64*cos(dir),GetEnemyY+64*sin(dir),12,dir+ra,217,0,0);
	Manage(shot);
	wait(3);
	}
		task Manage(shot){
		let s = 3;
			while(!Obj_IsDeleted(shot)){
			ObjRender_SetScaleXYZ(shot,s,s*2,0);
			s+=0.01;
			yield;
			}
		}
	}

task MasterSpark{
		dir = angleToPlayer;
		ascent(i in 0..4){
		RealSpark(i);
		}
		wait(125);
		//EffBullet;
		ascent(i in 0..20){
		RingSpark;
		wait(12);
		}
		task RealSpark(type){
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=GCSD~"img/pl01b.png";
		let sx = 0;
		let sy = 0;
		let value = 1.5+type/10;
		if(type>1){sy = 0.2;}
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,0,0,256,128);
		ObjSprite2D_SetDestCenter(obj);
		if(type==0){
		Obj_SetRenderPriorityI(obj,23);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
		ObjRender_SetColor(obj,128,0,128);
		}
		if(type==1){
		Obj_SetRenderPriorityI(obj,23);
		ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
		ObjRender_SetColor(obj,64,0,64);
		}
		if(type==2){
		Obj_SetRenderPriorityI(obj,21);
		ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
		ObjRender_SetColor(obj,64,0,64);
		}
		if(type==3){
		Obj_SetRenderPriorityI(obj,20);
		ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
		ObjRender_SetColor(obj,32,0,32);
		}
		Stable;
		while(sx<value){
		sx += 0.05;
		yield;
		}
		wait(90);
		ScreenShake(240);
		while(sy<value){
		sy+=(1.5+type/10)/10;
		yield;
		}
		let dir_d = 0;
		let count = 0;
		loop(240){
		if(GetPlayerState == STATE_NORMAL){
        let dir_d = atan2(GetPlayerY - ObjMove_GetY(objBoss),
                        GetPlayerX - ObjMove_GetX(objBoss));

        let diff = dir_d - dir;
		let max = (|diff/2000|);
        while(diff >= 180) { diff -= 360; }    // adjust the range
        while(diff < -180) { diff += 360; }

        let diffAbs = (|diff|);
        if(diffAbs <max) {
            dir = dir_d;
        } else {
            dir += max* diff / diffAbs;
        }
		}
		yield;
		}
			while(sy>0){
			sy-=value/10;
			yield;
			}
			Obj_Delete(obj);
			task Stable{
			let c = 0;
			while(!Obj_IsDeleted(obj)){
				if(sy>0.2){
				ascent(i in 0..60){
				SetShotIntersectionLine(GetEnemyX+i*2.5*cos(dir),GetEnemyY+i*2.5*sin(dir),GetEnemyX+600*cos(dir),GetEnemyY+600*sin(dir),sy*i*55/60);
				}
				SetShotIntersectionLine(GetEnemyX+150*cos(dir),GetEnemyY+150*sin(dir),GetEnemyX+600*cos(dir),GetEnemyY+600*sin(dir),sy*55);
				}
				ObjRender_SetPosition(obj,GetEnemyX+128*sx*1.2*cos(dir),GetEnemyY+128*sx*1.2*sin(dir),0);
				ObjRender_SetAngleXYZ(obj,0,0,dir);
				ObjRender_SetScaleXYZ(obj,sx*1.2,sy,0);
				ObjRender_SetColor(obj,128+32*sin(c),0,128+32*sin(c));
				c+=30;
				yield;
				}
			}
		}
		task RingSpark{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=GCSD~"img/pl01b2.png";
		let px = GetEnemyX-32*cos(dir);
		let py = GetEnemyY-32*sin(dir);
		let scale = 0.1;
		let c = 0;
		let acc = 0.085;
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,0,0,96,256);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj,px,py,0);
		ObjRender_SetAngleXYZ(obj,0,0,dir);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjRender_SetColor(obj,255,192,255);
			loop(30){
			ObjRender_SetPosition(obj,px+c*cos(dir),py+c*sin(dir),0);
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			c+=15;
			if(scale<1.1){
			scale+=acc;
			if(acc>=0){acc -= 0.0015;}
			}
			yield;
			}
			Obj_Delete(obj);
		}
				task ScreenShake(time){
				loop(time/2){
				Set2DCameraFocusX(Get2DCameraX + rand(-3, 3));
				Set2DCameraFocusY(Get2DCameraY + rand(-3, 3));
				yield;
				Reset2DCamera;
				yield;
				}
				Reset2DCamera;
			}
	}
	
	
function InvCircle(boss){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/charge.png";
	let s = 0;
	let acc = 0.015;
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,ObjMove_GetX(boss),ObjMove_GetY(boss),0);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjRender_SetColor(obj,64,0,64);
	ObjRender_SetAlpha(obj,255);
	TScale;
		task TScale{
			loop(55){
			ObjRender_SetScaleXYZ(obj,s,s,0);
			s += acc;
			acc += 0.005;
			yield;
			}
			acc = 0;
			wait(180);
			loop(59){
			ObjRender_SetScaleXYZ(obj,s,s,0);
			s -= acc;
			acc += 0.005;
			yield;
			}
			Obj_Delete(obj);
		}
	}
//----------------------------------------------------

task TRender
{
	DrawBoss;
}

//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
		{AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));}
		EndBoss(1);
		wait(60);
		CloseScript(GetOwnScriptID());
		return;
	}