#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 9 (Lost Jack)"]
#Text["Final Spell Card"]
#include"./Index.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let AnimFrame = 0;
let AnimFrame2 = 0;

let imgBoss = CSD ~ "/Texture/Lost Jack.png";
let AttackSFX = CSD ~ "/SFX/TWINKLE.wav";
let Attack2SFX = CSD ~ "/SFX/TWINKLE3.wav";
let Attack3SFX = CSD ~ "/SFX/ATTACK.wav";
let CHARGESFX = CSD ~ "/SFX/CHARGE.wav";
let DEFEATSFX = CSD ~ "/SFX/DEFEATED.wav";
let WOODSFX = CSD ~ "/SFX/Wood.mp3";

@Initialize {

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	ObjMove_SetPosition(bossObj,190,-100);
	ObjMove_SetPosition(bossObj,190,100);
	
	
	Cutin(bossObj,"Stray Phantasma",9);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	LoadSound(Attack3SFX);
	LoadSound(CHARGESFX);
	LoadSound(DEFEATSFX);
	LoadSound(WOODSFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(4444);
	}		
 }

@MainLoop {
	
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,15);
		
	yield;
 }

@Finalize {

 }

task mainTask{
	renderBoss;
	renderBG("JACK");
	callbackboss;
	//Fire2;
	//Fire3;
	movement;
	TEnd;
 }
 
  task fog(x,y,size){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=CSD~"/System/Orin.png";
	let xspd = 0;
	let yspd = 0;
	let s= rand(-10,10);
	let alpha = 300;
	let Z = 0;
	let xspeed = rand(-size*s,-0.2*size);
	let yspeed = rand(-size*2,size);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleXYZ(obj,rand(0,360),rand(0,360),rand(0,360));
	ObjRender_SetColor(obj,rand(100,255),rand(100,255),rand(100,255));
	Obj_SetRenderPriorityI(obj,60);
	loop(150){
	ObjRender_SetPosition(obj,x+yspd,y-xspd*0.8,0);
	yspd += yspeed*-s;
	xspd += xspeed*s/2;
	ObjRender_SetAngleXYZ(obj,-0,-0,xspd+yspd);
	
	ObjRender_SetAlpha(obj,alpha);
	alpha-=2;
	
	ObjRender_SetScaleXYZ(obj,size-Z,size-Z,0);
	Z+=0.001;
	wait(1);
	yield;
	}
	Obj_Delete(obj);
	}
 
task movement{
	wait(40);
	let angle= 0;
	loop{
		Fire;
		Fire2;
		Fire3;
		while (angle<360){
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
			fog(bossX+30*sin(angle),bossY-30*cos(angle),rand(0.1,0.3));
			angle+=360/36;
		}
		angle= 0;
		ObjMove_SetDestAtFrame(bossObj,GetPlayerX,rand(170,201),30);
		wait(120);
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),-50,60);
		PlaySound(WOODSFX);
		while (angle<360){
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
			fog(bossX+30*sin(angle),bossY-30*cos(angle),rand(0.1,0.3));
			angle+=360/36;
		}
		angle= 0;
		wait(120);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
	}
}
 
task renderBoss{
	let Z=0;
	let X=0;
	let dir;
	
	ObjPrim_SetTexture(bossObj, imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,600,589);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.5,0.5,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		dir = ObjMove_GetAngle(bossObj);
		
		if(cos(dir) > 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.2+0.02*sin(Z),0.2-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X>0){X-=2;}
				wait(1);
			}
		}
		else if(cos(dir) < 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.2+0.02*sin(Z),0.2-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X<180){X+=2;}
		wait(1);
			}}
		yield;
	}

 }
 
task Fire{
	let B = rand(360,0);
	let angle= 0;
	loop(40){
		PlaySound(Attack2SFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),1,angle+B-90,0.05,3,37,12);
			ObjMove_AddPatternB2(obj,30,NO_CHANGE,NO_CHANGE,0,0.16,1,2);
			angle+=360/6;
		}
		angle=0;
		B-=1.6*5;
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(2);
		yield;
	}
}
 
task Fire2{
	let B = rand(360,0);
	let angle= 0;
	let X =  bossX;
	let Y =  bossY;
	PlaySound(Attack3SFX);
	loop(60){
		CreateShotA2(X+rand(-50,50),Y+rand(-25,50),6,rand(-10,191),-0.1,rand(2.5,3.7),238,rand(0,18));
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
	}
}
 
task Fire3{
	let B = 0;
	let X= rand(-50,51);
	let Y= rand(-50,51);
	let angle= 0;
	loop(20){
		PlaySound(AttackSFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+X+10*sin(angle+B),bossY+Y-10*cos(angle+B),4,angle+B-90,-0.1,1,108,12);
			angle+=360/20;
		}
		angle=0;
		B+= 1.6;
		X= rand(-50,51);
		Y= rand(-50,51);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(5);
		yield;
	}
}

 task TEnd {
	let A=0;
	let X = ObjMove_GetX(bossObj);
	let Y = ObjMove_GetY(bossObj);
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	let A2=20;
	let B=0;
	let C=75;
	loop(180){
		CreateItemA1( ITEM_POINT, bossX+C*sin(A2), bossY+C*cos(A2),100);
		CreateItemA1( ITEM_POINT, bossX+(C/2)*sin(A2), bossY+(C/2)*cos(A2),100);
		B++;
		A2+=2;
		C+=5*cos(B*10);
	}
	Obj_Delete(bossObj);
	loop(30){
		PlaySound(CHARGESFX);
		TChargeDark(X,Y);
		wait(1);
	}
	wait(30);
	PlaySound(DEFEATSFX);
	loop(51){
		TExplosionB(X,Y,5,0.3,A,A,A);
		A+=5;
		wait(3);
	}
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}