//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ZT Spell 8"]
#Text["Spell 8"]
#include "script/default_system/Default_ShotConst.txt"
#include "script/ZT/Library.txt"
#include "script/default_system/Default_Effect.txt"
#include "script/ZT/Cutin.txt"

let objBoss;
let bossX = 0;
let bossY = 0;
let angleT = 0;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
let overlay = ObjPrim_Create(OBJ_SPRITE_2D);
let alphaFade1 = 0;
let alphaFade2 = 0;

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(3000); 
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(101);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(2000000);
	}
}

@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	BottomEnemyMarker(objBoss, objBoss);
        ObjEnemy_Regist(objBoss);
        let ZeroTwoPixel = GetCurrentScriptDirectory() ~ "img/ZeroTwoPixel.png";
        ObjPrim_SetTexture(objBoss, ZeroTwoPixel);
        ObjSprite2D_SetSourceRect(objBoss, 0, 0, 20, 49);
        ObjSprite2D_SetDestCenter(objBoss);
        ObjMove_SetDestAtWeight(objBoss, GetCenterX(), 125, 5, 4);
		mainTask;
}

@MainLoop {
	bossX = ObjMove_GetX(objBoss);
	bossY = ObjMove_GetY(objBoss);
	angleT = GetAngleToPlayer(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,bossX,bossY,32);
	yield;
}

task mainTask {
	mainShot;
	bossInvincibility;
	objMove;
	Aura;
	angleSource;
	TEnd;
	wait(120);
	renderBG;
	fadeObj;
	fadeOverlay;
	StartSpell;
}

task Aura {
	let frameAura = 0;
	let objAura = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objAura, GetCurrentScriptDirectory~"./img/spellcircle.png");
	ObjSprite2D_SetSourceRect(objAura,0, 0, 132, 132);
	ObjRender_SetScaleXYZ(objAura,1,1,1);
	Obj_SetRenderPriorityI(objAura,25);
	ObjSprite2D_SetDestCenter(objAura);
	ObjRender_SetBlendType(objAura,BLEND_ADD_ARGB);
	ObjRender_SetPosition(objAura,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	ObjRender_SetAngleXYZ(objAura,0,0,0);
	while(!Obj_IsDeleted(objBoss)){
		frameAura++;
		ObjRender_SetScaleXYZ(objAura,1.5+0.2*cos(frameAura*3),1.5+0.2*cos(frameAura*3),1);
		ObjRender_SetAngleXYZ(objAura,0,0,frameAura*3);
		ObjRender_SetAlpha(objAura,205-50*cos(frameAura*3));
		ObjRender_SetPosition(objAura,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
		yield;
	}
	Obj_Delete(objAura);
}

task angleSource {
	let dir;
	let speed;
	while(!Obj_IsDeleted(objBoss)) {
	dir = ObjMove_GetAngle(objBoss);
	speed = ObjMove_GetSpeed(objBoss);
	if(speed == 0) { ObjSprite2D_SetSourceRect(objBoss,0,0,20,49); }
	else if(cos(dir) < 0) { ObjSprite2D_SetSourceRect(objBoss,20,0,0,49); }
	else if(cos(dir) > 0) { ObjSprite2D_SetSourceRect(objBoss,0,0,20,49); }
		yield;
	}
}

task mainShot {
	wait(275);
	SE_Play(cus3,60);
	shot1(0.01, 165);
	shot1(0.02, 171);
	shot2(0.01, 165);
	shot2(0.02, 171);
	wait(100);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, 2, 1.2, 40, 20, 121);
	SE_Play(kira,60);
	wait(350);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, 6, 1.2, 40, 20, 122);
	SE_Play(kira,60);
	wait(350);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, 1.2, 1.2, 40, 20, 123);
	SE_Play(kira,60);
	wait(350);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, -2, -1.2, 80, 20, 129);
	SE_Play(kira,60);
	wait(350);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, -6, -1.2, 80, 20, 130);
	SE_Play(kira,60);
	wait(350);
	SE_Play(ngumpulin2,70);
	wait(50);
	createShiki(1, -1.2, -1.2, 80, 20, 131);
	SE_Play(kira,60);
}

task shot1(acc, shot) {
	let angleM;
	let num = 10;
	angleM = rand(0,360);
	loop(num){
		let shots = CreateShotA2(bossX,bossY,0.5,angleM,acc,10,shot,10);
		ObjMove_SetAngularVelocity(shots,0.6);
		angleM+=360/num;
	}
}

task shot2(acc, shot) {
	let angleM;
	let num = 10;
	angleM = rand(0,360);
	loop(num){
		let shots = CreateShotA2(bossX,bossY,0.5,angleM,acc,10,shot,10);
		ObjMove_SetAngularVelocity(shots,-0.6);
		angleM+=360/num;
	}
}

task createShiki(ID, num, dir, rad, rate, shot) {
    let objcount = 0;
    let angleO = ID*360/num;
	let graphic = GetCurrentScriptDirectory() ~ "img/familiar.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj, 26);
        while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
            angleO = ID*360/num + objcount*dir;
            ObjRender_SetPosition(obj, ObjMove_GetX(objBoss) + rad*cos(angleO), ObjMove_GetY(objBoss) + rad*sin(angleO), 1); 
            ObjRender_SetAngleZ(obj, angleO); 
            if (objcount % rate == 0) {
                let currx = ObjRender_GetX(obj);
                let curry = ObjRender_GetY(obj);
                ascent(i in 0..3) {
                    CreateShotA1(currx, curry, 1.5, angleO+180+i*120, shot, 5);
					SE_Play(cus,60);
                }
            }
            objcount++;
            yield;
        }
        Obj_Delete(obj);
}

task bossInvincibility {
	ObjEnemy_SetDamageRate(objBoss,0,0);
	wait(240);
	ObjEnemy_SetDamageRate(objBoss,20,20);
}

task objMove {
	wait(375);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	ObjMove_SetDestAtWeight(objBoss, rand(GetCenterX()+100, GetCenterX()-100), rand(70,130), 25, 3);
	wait(200);
	}
}

task TEnd {
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		yield;
	}
	SetPlayerInvincibilityFrame(180);
	StartSlow(TARGET_ALL, 15);
	ExplosionB2(objBoss,"Flow",[0,175,255],[0,175,255]);
	wait(180);
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	StopSlow(TARGET_ALL);
	wait(100);	
	CloseScript(GetOwnScriptID());
}

task ExplosionB2(objBoss,type,circlecolor,starcolor) {
	let angle = rand(0,360);
	ObjEnemy_SetLife(objBoss, 1);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	SE_Play(bigexplode,70);
	DefaultCircle(objBoss,3.0,circlecolor,0,0,0);
	ObjMove_SetSpeed(objBoss,0.2);
	ObjMove_SetAngle(objBoss,195);
	loop(90/4) {
		ExplodeObject02(type, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(1.5,4.0), rand(0,360), [255,255,255]);
		ExplodeObject02(type, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(1.5,4.0), rand(0,360), starcolor);
		loop(4) { yield; }
	}
	SE_Play(bigexplode,100);
	DefaultCircle(objBoss,3.0,circlecolor,0,0,0);
	ScreenShake(90,10);
	loop(40) {
		ExplodeObject02(type, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(2,6), angle, [255,255,255]);
		angle+=360/80;
		ExplodeObject02(type, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(2,6), angle, starcolor);
		angle+=360/80;
	}
	ObjEnemy_SetLife(objBoss, 0);
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
}

task DefaultCircle(objEnemy,maxscale,color,x,y,z) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let RGB = color;
	
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,41);
	ObjPrim_SetTexture(obj, CSD ~ "img/mythicalcircle.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj);

	let alpha = 255;
	let scale = 0;
	let dscale = 0.4;
	
	ObjRender_SetPosition(obj, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 0);
	ObjRender_SetAngleXYZ(obj,x,y,z);
	ascent(i in 0..20) {
		scale = maxscale*sin(90*i/20);
		alpha -= 255/20;
		ObjRender_SetColor(obj,(alpha/255)*RGB[0],(alpha/255)*RGB[1],(alpha/255)*RGB[2]);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		yield;
	}
	Obj_Delete(obj);
}

task ScreenShake(duration,range) {
let shake = range;
	loop(duration) {
		Set2DCameraFocusX(Get2DCameraX + rand(-shake, shake));
		Set2DCameraFocusY(Get2DCameraY + rand(-shake, shake));
		shake-=range/duration;
		yield;
		Reset2DCamera;
	}
}

task ExplodeObject02(type,x,y,speed,dir,color) {
	let objStar = ObjPrim_Create(OBJ_SPRITE_2D);

	let alpha = 200;
	let spinA = rand(0,180);
	let spinB = rand(0,180);
	let RED = color[0];
	let GREEN = color[1];
	let BLUE = color[2];
	let scale = 0;
	
	ObjRender_SetBlendType(objStar, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(objStar,45);
	ObjPrim_SetTexture(objStar, CSD ~ "img/shine.png");
	ObjSprite2D_SetSourceRect(objStar, 0, 0, 100, 100);
	ObjSprite2D_SetDestCenter(objStar);
	
	while(!Obj_IsDeleted(objStar)) {
		ObjRender_SetPosition(objStar, x, y, 0);
		ObjRender_SetScaleXYZ(objStar, scale, scale, scale);
		ObjRender_SetAngleXYZ(objStar, spinA, spinB, spinA);
		ObjRender_SetAlpha(objStar, alpha);
		ObjRender_SetColor(objStar, RED*(alpha/255), GREEN*(alpha/255), BLUE*(alpha/255));
		if(scale<0.38 && speed>0 && GetCommonData("FREEZE",false)==false) { scale+=0.03; }
		if(speed>1.0 && GetCommonData("FREEZE",false)==false) { speed-=0.038; }
		if(speed<=1.0 && alpha>0 && GetCommonData("FREEZE",false)==false) { alpha-=15; scale-=0.035; }
		if(alpha<=0 && GetCommonData("FREEZE",false)==false) { Obj_Delete(objStar); }
		if(GetCommonData("FREEZE",false)==false){spinA+=rand(1,2);}
		if(GetCommonData("FREEZE",false)==false){spinB+=rand(-1,-2);}
		if(GetCommonData("FREEZE",false)==false){x+=speed*cos(dir);}
		if(GetCommonData("FREEZE",false)==false){y+=speed*sin(dir);}
		yield;
	}
}

task fadeObj {
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,flame8); 
	ObjSprite2D_SetSourceRect(obj,0,0,266,375);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	ObjRender_SetAlpha(obj,0);
	
	while(alphaFade1 < 256){
		ObjRender_SetAlpha(obj,0+alphaFade1);
		alphaFade1 += 2;
		yield;
	}
}

task fadeOverlay {
	Obj_SetRenderPriorityI(overlay,21);
	ObjPrim_SetTexture(overlay,sfell);
	ObjRender_SetBlendType(overlay,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlay,60);
	ObjSprite2D_SetSourceRect(overlay,0,0,384,433);
	ObjSprite2D_SetDestRect(overlay,0,0,GetStgFrameWidth,GetStgFrameHeight);
	ObjRender_SetAlpha(overlay,0);
	
	while(alphaFade2 < 100){
		ObjRender_SetAlpha(overlay,0+alphaFade2);
		alphaFade2 += 1;
		yield;
	}
}

task renderBG {
	let scrollTex = 0;
	
	while(!Obj_IsDeleted(objBoss)) {
		ObjSprite2D_SetSourceRect(overlay,0,0-scrollTex,384,433-scrollTex);
		scrollTex += 2.5;
		yield;
	}
	Obj_Delete(overlay);
	Obj_Delete(obj);
}

task StartSpell{
	let objEnemy = GetEnemyBossObjectID[0];
	let her = GetCurrentScriptDirectory~"img/cutin.png";
	ObjCutin_SetSpellcardS4("Tanda Klaxosaur \"Jurus Terakhir\"", her, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(YABUSAME, her, "Normal");
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
}