#TouhouDanmakufu
#ScriptVersion[3]
#Title["Spring Sign \"Screaming Skulls in Spring\""]

#include"./../ZUNShot_Const.txt"
#include"./../../drawstuff.txt"
#include "script/default_system/Default_Effect.txt"

let ex;
let ey;
let mx;
let my;
let l = 0;
let objEnemy;
let diff = GetCommonData("Difficulty", 0);
	@Initialize{
		objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
		ObjEnemy_Regist(objEnemy);
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,75,10,10);
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		EnglandMove(GetCurrentScriptDirectory ~ "img/enm1.png");
		AuraMakeBoss(objEnemy,120,150,200);
		mainTask;	
		}

	@MainLoop
	{
		ex = ObjMove_GetX(objEnemy);
		ey = ObjMove_GetY(objEnemy);
		l+=1;
		ObjEnemy_SetIntersectionCircleToShot(objEnemy, ex, ey, 32);//Sets the hitbox against player bullets
		ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ex, ey, 24);//Sets the hitbox against the player
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||!GetEnemyBossObjectID()){
			Obj_Delete(objEnemy);
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			Reset2DCamera();
			CloseScript(GetOwnScriptID());
			}
		yield;
	}
	

	@Event
	{
		alternative(GetEventType())
		case(EV_REQUEST_LIFE)
		{
			SetScriptResult(650);
		}
		case(EV_REQUEST_TIMER)
		{
			SetScriptResult(87);//Sets the time limit to 60
		}
		case(EV_REQUEST_SPELL_SCORE)
		{
			SetScriptResult(0);//Sets spellcard bonus to 30000 points
		}
	}

task mainTask{
	yield;
	//EnemyMarker(objEnemy);
		Spawn(0,0,0);
	loop(90){yield;}
	SetAreaCommonData("SpellData", "Tries1", GetAreaCommonData("SpellData", "Tries1", 0)+1);
	backCall;
	backCall2;
	cutin(GetCurrentScriptDirectory~"img/cutin.png",0,0,520,350);
	if(diff<2){SpellName("Spring Sign \"Screaming Skulls in Spring\"",100,100,100,200,200,200,"name1");} 
	else{
		SpellName("Spring Sign \"Screaming Skulls in Spring\"",100,100,100,200,200,200,"name1");
	}
	score(2200000,100,100,100,200,200,200,"Capture1","Tries1");
	loop(120){yield;}
	moveRomano;
	loop(60){yield;}
	shoot;
	loop(240){yield;}
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	//butterfly;
	move;
	yield;
	}


task moveRomano{

	//loop(60){yield;}
	//loop{
		//blackness(240);
		//DelaySE("SBoon1",0,0,0);
		//loop(120){yield;}
	//}
}

task shoot{
	let x = 0;
	let y = 0;
	let bullet = 0;
	let veloc = 0;
		DelaySE("SCharge1",0,0,0);
		charge(ex,ey,60);
		loop(60){yield;}
		loop{
			loop{
				DelaySE("SBoon2",0,0,0);
				let posix = rand(10,GetStgFrameWidth-10);
				let posiy = ey+rand(-20,20);
				butterfly(posix,posiy,4,2);
				Laser(posix,posiy);
				y++;
				if(y>20){break;}
				loop(max(15,40-y*2)){yield;}
			}
			loop(180){yield;}
			charge(ex,ey,60);
			DelaySE("SCharge1",0,0,0);
			loop(60){yield;}
			DelaySE("SBoon2",0,0,0);
			butterfly(ex,ey,15,4+diff*2);
			loop(10){
				Laser(ex,ey);
				yield;
			}
			loop(180){yield;}
			y=0;
		}
	}
task butterfly(xpos,ypos,amount,bullet){
		DelaySE("SShot2",0,0,0);
		//DelaySE("SShot1",30,0,0);
		let x = rand(0,360);
		ascent(j in 0..amount){
			ascent(i in 1..bullet){
				let butter = CreateObjShotA2(xpos,ypos,0,180-j*360/(amount),0.1,1,319,10);
				ObjMove_AddPatternA2(butter, 30, NO_CHANGE, NO_CHANGE, 0.1, 0, 1+i/bullet);
			}
			yield;
		}
}
task move{
	loop{
		if((GetPlayerX>ex||ex<40)&&ex<GetStgFrameWidth-40){
			ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)+rand(30,50),rand(90,125),10,1);
		} else{
			ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)-rand(30,50),rand(90,125),10,1);			
		}
		loop(300){yield;}
	}
}
task backCall{
	let objBgOne = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgOne,GetCurrentScriptDirectory~"img/bg2.png",0,0,674,674,10000000,255,-145,0,529,674,22,BLEND_ALPHA,0,0,0,0,0);
	}
task backCall2{
	let objBgTwo = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgTwo,GetCurrentScriptDirectory~"img/bg1.png",0,0,600,600,10000000,255,-300,-300,300,300,21,BLEND_ALPHA,0,0,0.1,GetStgFrameWidth()/2,GetStgFrameHeight()/2);
	}

task background1(obj,imgbg1,r,t,l,b,time,alpha,rd,rt,rl,rb,prio,render,xmove,ymove,zangle,x,y){
let c = 0;
LoadTexture(imgbg1);
ObjPrim_SetTexture(obj,imgbg1);
Obj_SetRenderPriorityI(obj,prio);
ObjSprite2D_SetDestRect(obj,rd,rt,rl,rb);
ObjRender_SetBlendType(obj,render);
ObjRender_SetAlpha(obj,alpha);
ObjSprite2D_SetSourceRect(obj,r,t,l,b);
ObjRender_SetPosition(obj, x, y, 0);
loop(time){
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
	ObjRender_SetAlpha(obj,c*12);
	ObjSprite2D_SetSourceRect(obj,r+(c*xmove),t+(c*ymove),l+(c*xmove),b+(c*ymove));
	ObjRender_SetAngleXYZ(obj,0,0,c*zangle);
	c++;
	yield;
}
Obj_Delete(obj);
}
task blackness(time){
let c = 0;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/black.png");
Obj_SetRenderPriorityI(obj,70);
ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
ObjRender_SetAlpha(obj,255);
ObjSprite2D_SetSourceRect(obj,0,0,5,5);
loop(time){
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
	ObjRender_SetAlpha(obj,255-c);
	c+=255/time;
	yield;
}
Obj_Delete(obj);
}

task Laser(posx,posy){
	let objAnc = CreateObjShotA1(posx, posy, rand(4.5,5.5), 90+rand(-20,20), 403, 10);
	let frame = 0;
	let n = 0.5;
	ObjMove_SetAcceleration(objAnc, -0.3/(diff+1));
	ObjMove_SetMaxSpeed(objAnc,0.3);
	ObjMove_SetPosition(objAnc,posx,posy);
	ObjShot_Regist(objAnc);
	while(!Obj_IsDeleted(objAnc)){
		if(frame==180){
			DelaySE("SShot1",0,0,0);
			ascent(i in 0..diff+3){
				ascent(j in 0..4){
					CreateObjShotA1(ObjMove_GetX(objAnc),ObjMove_GetY(objAnc),1.5+j/2,GetAngleToPlayer(objAnc) + 360*i/(diff+3)+rand(20,-20),94,10);
				}
			}
			Obj_Delete(objAnc);
		}
		frame++;
		yield;
	}
}
task Spawn(x,y,v){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let a = round(rand(60,89));
	let familiar = GetCurrentScriptDirectory~"img/enmeffect01b.png";
	Obj_SetRenderPriorityI(obj,23);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,0);
	ObjMove_SetSpeed(obj,v);
	while(!Obj_IsDeleted(obj)){

	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(obj),ObjMove_GetY(obj),128);
		if(frame<60){
			ObjRender_SetScaleX(obj,1);
		}
		ObjMove_SetPosition(obj,ex,ey);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,0,0,512,256); 
		frame++;
		yield;
	}	
}