#TouhouDanmakufu
#ScriptVersion[3]
#Title["Cursed Item \"Busby's Chair\""]

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

let ex;
let ey;
let mx;
let my;
let objEnemy;


	@Initialize{
		objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
		ObjEnemy_Regist(objEnemy);
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjMove_SetDestAtWeight(objEnemy,-50,-50,10,2);
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		EnglandMove;
		Magiccircle(objEnemy);
		AuraMake;
		mainTask;	
		}

	@MainLoop
	{
		ex = ObjMove_GetX(objEnemy);
		ey = ObjMove_GetY(objEnemy);
		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){
			loop(20){	CreateItemA1(ITEM_POINT,ex+rand(-50,50),ey+rand(-50,50),GetCommonData("PIValue",10000)); }
			Obj_Delete(objEnemy);
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			Reset2DCamera();
			CloseScript(GetOwnScriptID());
			}
		yield;
	}

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

task mainTask{
	yield;
	backCall;
	backCall2;
	cutin(GetCurrentScriptDirectory~"cutin.png",0,0,211,325);
	SpellName("Cursed Item \"Busby's Chair\"",100,80,120,120,80,100);
	score(1000000000,100,80,120,120,80,100);
	EnglandMove;	
	loop(180){yield;}
	FamSpawn;
	loop(240){yield;}
	yield;
	}

task FamSpawn(){
	Fam2(135);
	loop(218/4){
		CreateShotA1(0,0,1,45,56,20);
		CreateShotA1(GetStgFrameWidth,0,1,135,56,20);
		CreateShotA1(0,GetStgFrameHeight,1,315,56,20);
		CreateShotA1(GetStgFrameWidth,GetStgFrameHeight,1,225,56,20);
		loop(40){yield;}
	}
}
task backCall{
	let objBgOne = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgOne,GetCurrentScriptDirectory~"bg1.png",0,0,640,640,10000000,255,200,150,150,22,BLEND_ALPHA,-0.3,0,0);
	}
task backCall2{
	let objBgTwo = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgTwo,GetCurrentScriptDirectory~"bg2.png",0,0,960,600,10000000,100,0,0,0,22,BLEND_ALPHA,0,0,1);
	}

task background1(obj,imgbg1,r,t,l,b,time,alpha,R,G,B,prio,render,xmove,ymove,zangle){
let c = 0;
LoadTexture(imgbg1);
ObjPrim_SetTexture(obj,imgbg1);
Obj_SetRenderPriorityI(obj,prio);
ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
ObjRender_SetBlendType(obj,render);
ObjRender_SetAlpha(obj,alpha);
ObjSprite2D_SetSourceRect(obj,r,t,l,b);
loop(time){
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
	ObjSprite2D_SetSourceRect(obj,r+(c*xmove),t+(c*ymove),l+(c*xmove),b+(c*ymove));
	c++;
	yield;
}
Obj_Delete(obj);
}

task famshoot2(obj){
	let n = 0;
	let y = 0;
	let dir = 0;
		DelaySE(SShot1,0,0);
		while(n<30){
			Light(ObjMove_GetX(obj),ObjMove_GetY(obj),3.2,obj,dir);
			dir+=360/30;
			n++;
		}
		loop(120){yield;}
		n = 0;
	loop(20){
		DelaySE(SShot2,0,0);
		while(n<13){
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,dir,50,90);
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,dir,50,90);
			dir+=360/13;
			n++;
		}
		dir = rand(0,360);
		n = 0;
		loop(240-y){yield;}
		if(y<180){y+=10;}
	}
	Fam(GetStgFrameWidth/2-75,100);
	Fam(GetStgFrameWidth/2+75,100);
	loop(20){yield;}
	Fam(GetStgFrameWidth/2+150,125);
	Fam(GetStgFrameWidth/2-150,125);
	loop(20){yield;}
	Fam(GetStgFrameWidth/2,150);
	loop{
		DelaySE(SShot2,0,0);
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,GetAngleToPlayer(obj)+20,244,20);
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,GetAngleToPlayer(obj)+10,244,20);
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,GetAngleToPlayer(obj),244,20);
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,GetAngleToPlayer(obj)-20,244,20);
		CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,GetAngleToPlayer(obj)-10,244,20);
		loop(120){yield;}
	}
}
task famshoot(obj){
	let n = 0;
	let dir = 0;
	loop{
		DelaySE(SShot1,0,0);
		while(n<10){
			let wave1 = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,dir,54,30);
			ObjMove_SetAngularVelocity(wave1,0.2);
			dir+=360/10;
			n++;
		}
		n = 0;
		dir = rand(0,360);
		loop(60){yield;}
		DelaySE(SShot1,0,0);
		while(n<10){
			let wave2 = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,dir,54,30);
			ObjMove_SetAngularVelocity(wave2,-0.2);			
			dir+=360/10;
			n++;
		}
		n = 0;
		loop(60){yield;}
	}
}
task fammove(obj){
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4,GetStgFrameHeight/4,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4*3,GetStgFrameHeight/4*3,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4*3,GetStgFrameHeight/4,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4,GetStgFrameHeight/4*3,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4*2,GetStgFrameHeight/4*2,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4*2,GetStgFrameHeight/4,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/4*2,GetStgFrameHeight/4*3,20,1);
	loop(240){yield;}
	ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,20,1);
	}
task Light(x,y,v,obj,dir){
	let objAnc = ObjShot_Create(OBJ_SHOT);
	let frame = 0;
	let a = 0;
	let ox = 0;
	let oy = 0;
	ObjMove_SetSpeed(objAnc,0);
	ObjMove_SetPosition(objAnc,x,y);
	ObjShot_SetGraphic(objAnc,52);
	ObjMove_SetAngle(objAnc,dir);
	ObjShot_SetDelay(objAnc,20);
	ObjShot_SetAutoDelete(objAnc,false);
	ObjShot_SetSpellResist(objAnc,true);
	ObjShot_Regist(objAnc);
	while(!Obj_IsDeleted(objAnc)){
		while(frame<(150)){
			ObjMove_SetPosition(objAnc,ObjMove_GetX(obj)+(300-frame)*cos(dir),ObjMove_GetY(obj)+(300-frame)*sin(dir));
			yield;
			frame++;
			dir+=2;
		}	
		ObjMove_SetPosition(objAnc,ObjMove_GetX(obj)+150*cos(dir),ObjMove_GetY(obj)+150*sin(dir));
		if(frame%10==0){CreateShotA1(ObjMove_GetX(objAnc),ObjMove_GetY(objAnc),3,dir,52,30);}
		yield;
		dir+=2;
		frame++;
		if(frame>2000){oy = ObjMove_GetY(obj); ox = ObjMove_GetX(obj);		loop(240){yield;}}
		while(frame>(2000)){
		ObjMove_SetPosition(objAnc,ox+(150-a)*cos(dir),oy+(150-a)*sin(dir));
		if(frame%90==0){DelaySE(SShot1,0,0); loop(3){CreateShotA1(ObjMove_GetX(objAnc),ObjMove_GetY(objAnc),1,dir,52,30); dir+=360/5;}}
		dir+=3;
		a+=0.02;
		frame++;
		yield;
		}
	}
}
task Fam2(i){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,255,0,0);
	ObjMove_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2);
	ObjMove_SetAngle(obj,i);
	ObjMove_SetSpeed(obj,0);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(obj),ObjMove_GetY(obj),32);
		if(frame==120){famshoot2(obj); }
		if(frame==500){fammove(obj);}
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
			Obj_Delete(obj);
		}
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,62,159); 
		if(frame<=128){ObjRender_SetAlpha(obj,frame*2);}
		frame++;
		yield;
	}	
}
task Fam(x,y){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,0,150,255);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,0);
	ObjMove_SetSpeed(obj,0);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(obj),ObjMove_GetY(obj),32);
		if(frame==120){famshoot(obj); }
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
			Obj_Delete(obj);
		}
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,62,159); 
		if(frame<=128){ObjRender_SetAlpha(obj,frame*2);}
		frame++;
		yield;
	}	
}
