//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ZT Spell 1"]
#Text["Spell 1"]
#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(1000); 
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(61);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(1000000);
	}
}

@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;
	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*1.5),1.5+0.2*cos(frameAura*1.5),1);
		ObjRender_SetAngleXYZ(objAura,0,0,frameAura*1.5);
		ObjRender_SetAlpha(objAura,205-50*cos(frameAura*1.5));
		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(130);
	createShiki(-1);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	wait(250);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
	CreateShotA1(bossX, bossY, 1.5, angleT, 166, 20);
	wait(20);
	SE_Play(cus3,60);
	}
}

task createShiki(dir){
	let obj = CreateShotA1(bossX, bossY, 0, 0, 25, 2);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtFrame(obj, GetCenterX() + 180*dir, 16, 60);
	wait(60);
	let objcount = 0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	    ObjMove_SetX(obj, GetCenterX() + 180*cos(objcount*2)*dir);
	    if(objcount % 9 == 0){
		ascent(i in 0..5){
		    CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3 + i/4, 90, 59, 7);
			SE_Play(cus,60);
		}
	    }
	    objcount++;
	    yield;
	}
        Obj_Delete(obj);
}

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

task objMove {
	wait(400);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
	ObjMove_SetDestAtWeight(objBoss, rand(GetCenterX()+85, GetCenterX()-85), rand(80,120), 15, 2);
	wait(270);
	}
}

task TEnd {
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		yield;
	}
	TExplosionA(bossX,bossY,10,0.5);
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	wait(40);
	CloseScript(GetOwnScriptID());
}

task fadeObj {
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,flame2); 
	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);
	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;
	let scrollTex2 = 0;
	
	while(!Obj_IsDeleted(objBoss)) {
		ObjSprite2D_SetSourceRect(obj,0,0,266,375);
		scrollTex2 += 1;
		ObjSprite2D_SetSourceRect(overlay,0,0-scrollTex,384,433-scrollTex);
		scrollTex += 2;
		yield;
	}
	Obj_Delete(overlay);
	Obj_Delete(obj);
}

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