#TouhouDanmakufu
#ScriptVersion[3]
#Title["Non 2"]

#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,90,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(5000);
		}
		case(EV_REQUEST_TIMER)
		{
			SetScriptResult(60);//Sets the time limit to 60
		}
		case(EV_REQUEST_SPELL_SCORE)
		{
			SetScriptResult(0);//Sets spellcard bonus to 30000 points
		}
	}

task mainTask{
	yield;
	//EnemyMarker(objEnemy);
	/*SetAreaCommonData("SpellData", "Tries1", GetAreaCommonData("SpellData", "Tries1", 0)+1);
	backCall;
	backCall2;
	cutin(GetCurrentScriptDirectory~"img/cutin.png",0,0,0,0);
	SpellName("Echo Sign \"Echolocation\"",100,100,100,200,200,200,"name1");
	score(1200000,100,100,100,200,200,200,"Capture1","Tries1");*/
	loop(60){yield;}
	Spawn(ex,ey,0);
	loop(60){yield;}
	randombullet;
	loop(90){yield;}
	shoot;
	ObjEnemy_SetDamageRate(objEnemy, 100, 100);
	loop(240){yield;}
	move;
	yield;
	}


task shoot{
	let x = 0;
	let y = 0;
	let bullet = 0;
	let veloc = 0;
		charge(ex,ey,60);
		DelaySE("SCharge1",0,0,0);
	loop{
		loop(min((diff+1)*2,6)){
			DelaySE("SShot3",0,0,0);
			let a = CreateObjShotA2(ex,ey,7,x*10,-0.2,0,356,10);
			curve(a);
			loop(10){yield;}
			x++;
		}
		/*loop(10-(diff+1)*2){
			loop(5){yield;}
		}*/
		loop(5){yield;}
		x+=2;
	}
task curve(objA){
	let t = 0;
	let ang = ObjMove_GetAngle(objA);
	while(!Obj_IsDeleted(objA)){
		if(t==90){
			ObjMove_SetAngle(objA,GetAngleToPlayer(objA));
			ObjMove_SetAcceleration(objA, 0.1);
			ObjMove_SetMaxSpeed(objA,1.5);
			DelaySE("SShot1",0,0,0);
			return;
		}
		t+=1;
		yield;
	}
}

}
task randombullet{
	loop{
		let a = CreateObjShotA2(rand(10,GetStgFrameWidth-10),ey+rand(-50,50),4,0,-0.1,rand(1,2),75,10);
		ObjMove_SetAngle(a, GetAngleToPlayer(a)+rand(30,-30));
		CreateObjShotA2(rand(10,GetStgFrameWidth-10),ey+rand(-50,50),3,rand(100,80),-0.1,rand(0.8,1),77,10);
		CreateObjShotA2(rand(10,GetStgFrameWidth-10),ey+rand(-50,50),3,rand(260,280),-0.1,rand(0.8,1),77,10);
		loop(5+(3-diff)*2){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);
	}
task backCall2{
	let objBgTwo = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgTwo,GetCurrentScriptDirectory~"img/bg1.png",0,0,520,520,10000000,120,0,0,GetStgFrameWidth,GetStgFrameHeight(),22,BLEND_ALPHA,0,0.3,0);
	}

task background1(obj,imgbg1,r,t,l,b,time,alpha,rd,rt,rl,rb,prio,render,xmove,ymove,zangle){
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);
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,angle,delay,grap,blend){
	let objAnc = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let frame = 0;
	let n = 0.5;
	ObjMove_SetSpeed(objAnc,0);
	ObjMove_SetPosition(objAnc,posx,posy);
	ObjShot_SetGraphic(objAnc,1);
	ObjStLaser_SetAngle(objAnc,angle);
	ObjShot_SetSpellResist(objAnc,true);
	ObjShot_SetDelay(objAnc,delay);
	ObjLaser_SetLength(objAnc,600);
	ObjLaser_SetRenderWidth(objAnc,20);
	ObjLaser_SetIntersectionWidth(objAnc,10);
	ObjShot_Regist(objAnc);
	while(!Obj_IsDeleted(objAnc)){
		if(frame==60){
				let a = CreateLooseLaserA1(posx,posy,6,angle,200,20,grap,20);
				ObjRender_SetBlendType(a,blend);
				n++;
			loop(20){yield;}
			DelaySE("SLaser2",0,0,0);
			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||ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMER)<=0){
		Obj_Delete(obj);
		}
		ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(obj),ObjMove_GetY(obj),128);
		if(frame<60){
			ObjRender_SetScaleX(obj,frame/60);
		}
		ObjMove_SetPosition(obj,ex,ey);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,0,0,512,256); 
		frame++;
		yield;
	}	
}