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

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

task mainTask{
	yield;
	Spawn(0,0,0);
	loop(60){yield;}
	loop(90){yield;}
	shoot;
	ObjEnemy_SetDamageRate(objEnemy, 100, 100);
	loop(240){yield;}
	randombullet;
	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(60){yield;}
	loop{
		loop(360/10){
			ascent(i in 0..5){
				CreateObjShotA1(ex,ey,1.5+diff/3*i/5,x+i*10,103,10);
				CreateObjShotA1(ex,ey,1.5+diff/3*i/5,x+180-i*10,98,10);
			}
		DelaySE("SShot1",0,0,0);
		loop(5){yield;}
		x+=10;
		}
		x+=90;
		loop(60){yield;}
	}

}
task randombullet{
	loop{
		DelaySE("SLaser2",0,0,0);
		Laser(CreateLooseLaserA1(ex+rand(50,-50), ey+rand(20,-20), 5, GetAngleToPlayer(objEnemy), 200, 50, 38, 0));
		loop(70-diff*10){yield;}
	}
}
task move{
	let ang = 270;
	loop{
		ObjMove_SetDestAtWeight(objEnemy, GetStgFrameWidth/2+150*cos(ang), GetStgFrameHeight/2+150*sin(ang), 10, 5);
		ang+=0.5;
		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(objAnc){
	let frame = 0;
	ObjMove_SetAngle(objAnc,GetAngleToPlayer(objAnc));
	while(!Obj_IsDeleted(objAnc)){
		if(frame%(10-diff)==0){
			CreateObjShotA2(ObjMove_GetX(objAnc),ObjMove_GetY(objAnc),0,ObjMove_GetAngle(objAnc)+rand(5,-5),rand(0.1,0.2),5,312,10);
		}
		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);
		ObjMove_SetPosition(obj,ex,ey);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,0,0,512,256); 
		frame++;
		yield;
	}	
}