#ScriptVersion[3]
#Title["S3"]
#Background["./background.txt"]
#Text["Spell 3"]

#include "./ZUNShot_Const.dnh"
#include"./function.txt"

let frame=0;
let rand1 = 0;
let objBoss;
let objScene = GetEnemyBossSceneObjectID;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

@Initialize {
   DeleteShotAll(TYPE_ALL,TYPE_ITEM);
   SetAutoDeleteObject(true);
   objBoss=ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(objBoss);
   ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2, 10, 5);
   ObjEnemyBossScene_StartSpell(objScene);
   mainTask;
}

@Event {
   alternative(GetEventType())
   case(EV_REQUEST_LIFE){
   	SetScriptResult(225);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(60);
   }
   case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(15000000);
   }
}

@MainLoop {
   frame++; 
   let bossx=ObjMove_GetX(objBoss);
   SetCommonData(bossx,ObjMove_GetX(objBoss));
   let bossy=ObjMove_GetY(objBoss);
   SetCommonData(bossy,ObjMove_GetY(objBoss));
   ObjEnemy_SetIntersectionCircleToShot(objBoss,bossx,bossy,24);
   ObjEnemy_SetIntersectionCircleToPlayer(objBoss,bossx,bossy,24);
   if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){DeleteShotAll(TYPE_ALL,TYPE_ITEM); Obj_Delete(objBoss); CloseScript(GetOwnScriptID()); DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);}
   yield;   
}

@Finalize {
}

task mainTask{
PlaySound("se_spell");
renderSpellBG;
renderBoss;
Circle(objBoss);
SpellText("Hypnosis ''Illusionary Trance''");
ObjEnemy_SetDamageRate(objBoss, 0, 0);
loop(60){yield;}
loop(60){yield;}
ObjEnemy_SetDamageRate(objBoss, 2.5, 2.5);
while(!Obj_IsDeleted(objBoss)){
		loop(2){
		ascent(a in 0..24){
		DelayCloudBullet1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0.5+0.25*sin(360/24*a*6),360/24*a+frame,307,0.5);
		PlaySound("se_kira00");		
		}
		loop(45){yield;}
		}
		loop(120){yield;}
		loop(5){
		rand1=rand(-30,30);
		loop(3){
		ascent(i in 0..5){DelayCloudBullet2(GetStgFrameWidth/4*i+rand1,0,1.5,90,267,2,0); DelayCloudBullet2(GetStgFrameWidth/4*i,0,1.5,90,267,-2,0);}
		PlaySound("se_tan02");
		loop(20){yield;}
		}
		loop(30){yield;}
		}
		loop(120){yield;}
	}
}

task DelayCloudBullet1(x,y,speed,angle,graphic,angvel){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){return;}
	let radius = 0;
	let bullet = CreateShotA1(x,y,speed,angle,graphic,0);
	Obj_SetRenderPriority(bullet,0.6);
	let saveSpeed = speed;
	let i = 0;
	ObjMove_SetSpeed(bullet, 0);
	while(i<=10){
		i+=1;
		ObjShot_SetIntersectionEnable(bullet, false);
		ObjRender_SetScaleXYZ(bullet, 2 - 1/10*i, 2-1/10*i,0);
		ObjRender_SetAlpha(bullet, 255/10*i);
		yield;
	}
	i=0;
	ObjShot_SetIntersectionEnable(bullet, true);
	ObjMove_SetSpeed(bullet, 0);
	while(!Obj_IsDeleted(bullet)){
		radius+=speed;
		speed=saveSpeed-1.25*sin(2*i);
		ObjRender_SetColorHSV(bullet,angle/4+i,128+128*sin(i),255);
		ObjMove_SetAngle(bullet, angle+angvel*(i/4));
		ObjMove_SetPosition(bullet,x+radius*cos(angle+angvel*(i/4)),y+radius*sin(angle+angvel*(i/4)));
		i++;
		yield;
	}
}

task DelayCloudBullet2(x,y,speed,angle,graphic,angvel,hue){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){return;}
	let bullet = CreateShotA1(x,y,speed,angle,graphic,0);
	let saveSpeed = speed;
	let i = 0;
	ObjMove_SetSpeed(bullet, 0);
	while(i<=10){
		i+=1;
		ObjShot_SetIntersectionEnable(bullet, false);
		ObjRender_SetScaleXYZ(bullet, 2 - 1/10*i, 2-1/10*i,0);
		ObjRender_SetAlpha(bullet, 255/10*i);
		yield;
	}
	i=0;
	ObjShot_SetIntersectionEnable(bullet, true);
	ObjMove_SetSpeed(bullet, saveSpeed);
	while(!Obj_IsDeleted(bullet)){
		ObjRender_SetColorHSV(bullet,hue+i,128,255);
		ObjMove_SetAngle(bullet,angle+45*sin(angvel*i));
		i++;
		yield;
	}
}



task renderBoss{
	let i = 0;
	ObjPrim_SetTexture(objBoss,imgBoss);
	ObjSprite2D_SetSourceRect(objBoss,6,7,76,102);
	ObjSprite2D_SetDestRect(objBoss,6,7,76,102);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss,1,1,0);
	while(!Obj_IsDeleted(objBoss)){
		i++;
		if(i%3==0){glowBoss;}
		yield;
	}
}

task renderSpellBG{
	let ct = 0;
	let texture1 = GetCurrentScriptDirectory~"img/sbg1.png";
	let bg1 = ObjPrim_Create(OBJ_SPRITE_2D);
	let texture2 = GetCurrentScriptDirectory~"img/sbg2.png";
	let bg2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let bg3 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(bg1, 0.224);
	ObjPrim_SetTexture(bg1, texture1);
	ObjSprite2D_SetSourceRect(bg1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(bg1);
	ObjRender_SetPosition(bg1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	Obj_SetRenderPriority(bg2, 0.224);
	ObjPrim_SetTexture(bg2, texture2);
	ObjSprite2D_SetSourceRect(bg2, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(bg2);
	ObjRender_SetPosition(bg2, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	Obj_SetRenderPriority(bg3, 0.224);
	ObjPrim_SetTexture(bg3, texture2);
	ObjSprite2D_SetSourceRect(bg3, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(bg3);
	ObjRender_SetPosition(bg3, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	while(!Obj_IsDeleted(objBoss)){
		ct++;
		ObjRender_SetAngleXYZ(bg1, 0, 0, 5*sin(ct/4));
		ObjSprite2D_SetSourceRect(bg3, -ct/4, -256*sin(ct/4)*sin(ct/4)*cos(ct/4), 1024-ct/4, 512-256*sin(ct/4)*sin(ct/4)*cos(ct/4));
		ObjSprite2D_SetSourceRect(bg2, ct, 256*sin(ct/4)*sin(ct/4)*cos(ct/4), 512+ct, 512+256*sin(ct/4)*sin(ct/4)*cos(ct/4));
		ObjRender_SetScaleXYZ(bg2, 1+0.1*sin(ct/4), 1+0.1*sin(ct/4), 1);
		ObjRender_SetColorHSV(bg2, 180+180*sin(ct/2), 128, 255);
		ObjRender_SetColorHSV(bg3, 180+180*sin(-ct/2), 128, 255);
		if(ct<60){ObjRender_SetAlpha(bg1, ct*(255/60)); ObjRender_SetAlpha(bg2, ct*(50/60)); ObjRender_SetAlpha(bg3, ct*(50/60));}
		yield;
	}
	ct=0;
	loop(60){ct++; ObjRender_SetAlpha(bg1, 255-ct*(255/60)); yield;}
	Obj_Delete(bg1);
	Obj_Delete(bg2);
	Obj_Delete(bg3);
}

task SpellText(String){
	let localx=GetStgFrameWidth/2;
	let localy=GetStgFrameHeight;
	let localcount=0;
	let spellName=ObjText_Create();
	ObjText_SetText(spellName, String);
	ObjText_SetFontSize(spellName,12);
	ObjText_SetFontColorTop(spellName,255,255,255);
	ObjText_SetFontColorBottom(spellName,255,0,0);
	ObjText_SetFontBorderWidth(spellName,2);
	ObjText_SetFontBorderColor(spellName,0,0,128);
	ObjText_SetFontBorderType(spellName,BORDER_FULL);
	ObjText_SetHorizontalAlignment(spellName, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(spellName, GetStgFrameWidth);
	localx=80;
	while(localcount<90){
		ObjRender_SetPosition(spellName,localx,localy,0);
		localy=500-465*sin(localcount);
		localx=80-localcount;
		localcount+=1;
		yield;
	}
	while(!Obj_IsDeleted(spellName)){
		if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){Obj_Delete(spellName);}
			yield;
		}
}

task charge{
	PlaySound("se_ch00");
	let x = ObjMove_GetX(objBoss);
	let y = ObjMove_GetY(objBoss);
	let angle = rand(0, 360);
	let randRadius = rand(200, 100);
	let randSquare = 1+rand(-0.25,1);
	let inc = 0;
	let light = ObjPrim_Create(OBJ_SPRITE_2D);
	let texture = GetCurrentScriptDirectory~"img/chg.png";
	ObjPrim_SetTexture(light, texture);
	ObjSprite2D_SetSourceRect(light,0,0,32,32);
	ObjSprite2D_SetDestCenter(light);
	ObjRender_SetScaleXYZ(light, randSquare, randSquare, 0);
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetAlpha(light, 255*sin(inc));
		ObjRender_SetBlendType(light, BLEND_ALPHA);
		ObjRender_SetPosition(light, x+(randRadius-inc)*cos(angle+inc*2), y+(randRadius-inc)*sin(angle+inc*2), 0);
		ObjRender_SetAngleXYZ(light, 0, 0, angle+inc*2);
		inc++;
		if((randRadius-inc)<=0){Obj_Delete(light);}
		yield;
	}
}

task glowBoss{
	let objx = ObjMove_GetX(objBoss);
	let objy = ObjMove_GetY(objBoss);
	let texture = GetCurrentScriptDirectory~"/img/boss.png";
	let objCircle = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objCircle, 0.225);
	ObjPrim_SetTexture(objCircle, texture);
	ObjSprite2D_SetSourceRect(objCircle,6,7,76,102);
	ObjSprite2D_SetDestCenter(objCircle);
	let ct = 0;
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetColorHSV(objCircle, frame, 128, 255);
		ObjRender_SetPosition(objCircle, objx, objy, 0);
		ObjRender_SetAlpha(objCircle,255-(ct*255/50));
		ObjRender_SetBlendType(objCircle, BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ(objCircle, 1+ct/100, 1+ct/100, 0);
		ct++;
		yield;
		if(ct==50){Obj_Delete(objCircle);}
	}
}